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 auto& all() const { return _operators; }
25 
27  const auto& allOfKind(Kind kind) const { return _operators.at(kind); }
28 
30  void register_(Kind kind, Operator info) { _operators[kind].push_back(std::move(info)); }
31 
32  void printDebug() {
33 #if 0
34  // Can't print this at registratin time as that's happening through
35  // global constructors.
36  for ( auto a : _operators ) {
37  for ( const auto& info : a.second ) {
38  int status;
39  auto n = abi::__cxa_demangle(info.typename_().c_str(), nullptr, nullptr, &status);
40  HILTI_DEBUG(logging::debug::Overloads, hilti::util::fmt("registered %s for operator '%s'", (n ? n : info.typename_().c_str()), to_string(info.kind())));
41  }
42  }
43 #endif
44  }
45 
47  static auto& singleton() {
48  static Registry instance;
49  return instance;
50  }
51 
52 private:
53  OperatorMap _operators;
54 };
55 
57 class Register {
58 public:
59  Register(Kind k, Operator c) { Registry::singleton().register_(k, std::move(c)); }
60 };
61 
62 inline const auto& registry() { return Registry::singleton(); }
63 
64 } // namespace operator_
65 } // namespace hilti
Definition: operator-registry.h:19
const auto & allOfKind(Kind kind) const
Definition: operator-registry.h:27
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80
const auto & all() const
Definition: operator-registry.h:24
Definition: operator-registry.h:57
static auto & singleton()
Definition: operator-registry.h:47
void register_(Kind kind, Operator info)
Definition: operator-registry.h:30