Spicy
operator-registry.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <map>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/expressions/resolved-operator.h>
11 #include <hilti/ast/expressions/unresolved-operator.h>
12 #include <hilti/ast/operator.h>
13 #include <hilti/ast/types/struct.h>
14 
15 namespace hilti {
16 namespace operator_ {
17 
19 class Registry {
20 public:
21  using OperatorMap = std::map<Kind, std::vector<Operator>>;
22 
24  const OperatorMap& all() const { return _operators; }
25 
27  const std::vector<Operator>& allOfKind(Kind kind) const {
28  if ( auto x = _operators.find(kind); x != _operators.end() )
29  return x->second;
30 
31  logger().internalError("unregistered operator requested in allOfKind()");
32  }
33 
35  void register_(Kind kind, Operator info) { _operators[kind].push_back(std::move(info)); }
36 
37  void printDebug() {
38 #if 0
39  // Can't print this at registratin time as that's happening through
40  // global constructors.
41  for ( auto a : _operators ) {
42  for ( const auto& info : a.second ) {
43  int status;
44  auto n = abi::__cxa_demangle(info.typename_().c_str(), nullptr, nullptr, &status);
45  HILTI_DEBUG(logging::debug::Overloads, hilti::util::fmt("registered %s for operator '%s'", (n ? n : info.typename_().c_str()), to_string(info.kind())));
46  }
47  }
48 #endif
49  }
50 
52  static auto& singleton() {
53  static Registry instance;
54  return instance;
55  }
56 
57 private:
58  OperatorMap _operators;
59 };
60 
62 class Register {
63 public:
64  Register(Kind k, Operator c) { Registry::singleton().register_(k, std::move(c)); }
65 };
66 
67 inline auto registry() { return Registry::singleton(); }
68 
69 } // namespace operator_
70 } // namespace hilti
const OperatorMap & all() const
Definition: operator-registry.h:24
const std::vector< Operator > & allOfKind(Kind kind) const
Definition: operator-registry.h:27
Definition: operator-registry.h:19
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80
Definition: operator-registry.h:62
static auto & singleton()
Definition: operator-registry.h:52
void register_(Kind kind, Operator info)
Definition: operator-registry.h:35