xref: /llvm-project/mlir/include/mlir/Query/Matcher/Registry.h (revision 02d9f4d1f128e17e04ab6e602d3c9b9942612428)
1 //===--- Registry.h - Matcher Registry --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Registry class to manage the registry of matchers using a map.
10 //
11 // This class provides a convenient interface for registering and accessing
12 // matcher constructors using a string-based map.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRY_H
17 #define MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRY_H
18 
19 #include "Marshallers.h"
20 #include "llvm/ADT/StringMap.h"
21 #include <string>
22 
23 namespace mlir::query::matcher {
24 
25 using ConstructorMap =
26     llvm::StringMap<std::unique_ptr<const internal::MatcherDescriptor>>;
27 
28 class Registry {
29 public:
30   Registry() = default;
31   ~Registry() = default;
32 
constructors()33   const ConstructorMap &constructors() const { return constructorMap; }
34 
35   template <typename MatcherType>
registerMatcher(const std::string & name,MatcherType matcher)36   void registerMatcher(const std::string &name, MatcherType matcher) {
37     registerMatcherDescriptor(name,
38                               internal::makeMatcherAutoMarshall(matcher, name));
39   }
40 
41 private:
42   void registerMatcherDescriptor(
43       llvm::StringRef matcherName,
44       std::unique_ptr<internal::MatcherDescriptor> callback);
45 
46   ConstructorMap constructorMap;
47 };
48 
49 } // namespace mlir::query::matcher
50 
51 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRY_H
52