OpenVDB  12.1.0
FunctionRegistry.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 
4 /// @file codegen/FunctionRegistry.h
5 ///
6 /// @authors Nick Avramoussis
7 ///
8 /// @brief Contains the global function registration definition which
9 /// described all available user front end functions
10 ///
11 
12 #ifndef OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
14 
15 #include "FunctionTypes.h"
16 
18 
19 #include <openvdb/version.h>
20 
21 #include <unordered_map>
22 
23 namespace llvm {
24 class LLVMContext;
25 }
26 
27 namespace openvdb {
29 namespace OPENVDB_VERSION_NAME {
30 
31 namespace ax {
32 namespace codegen {
33 
34 /// @brief The function registry which is used for function code generation.
35 /// Each time a function is visited within the AST, its identifier is used as
36 /// a key into this registry for the corresponding function retrieval and
37 /// execution. Functions can be inserted into the registry using insert() with
38 /// a given identifier and pointer.
40 {
41 public:
43  using Ptr = std::shared_ptr<FunctionRegistry>;
44  using UniquePtr = std::unique_ptr<FunctionRegistry>;
45 
46  /// @brief An object to represent a registered function, storing its
47  /// constructor, a pointer to the function definition and whether it
48  /// should only be available internally (i.e. to a developer, not a user)
49  ///
51  {
52  /// @brief Constructor
53  /// @param creator The function definition used to create this function
54  /// @param internal Whether the function should be only internally accessible
55  RegisteredFunction(const ConstructorT& creator, const bool internal = false)
56  : mConstructor(creator), mFunction(), mInternal(internal) {}
57 
58  /// @brief Create a function object using this creator of this function
59  /// @param op The current function options
60  inline void create(const FunctionOptions& op) { mFunction = mConstructor(op); }
61 
62  /// @brief Return a pointer to this function definition
63  inline const FunctionGroup* function() const { return mFunction.get(); }
64 
65  /// @brief Check whether this function should be only internally accesible
66  inline bool isInternal() const { return mInternal; }
67 
68  private:
69  ConstructorT mConstructor;
70  FunctionGroup::Ptr mFunction;
71  bool mInternal;
72  };
73 
74  using RegistryMap = std::unordered_map<std::string, RegisteredFunction>;
75 
76  /// @brief Insert and register a function object to a function identifier.
77  /// @note Throws if the identifier is already registered
78  ///
79  /// @param identifier The function identifier to register
80  /// @param creator The function to link to the provided identifier
81  /// @param internal Whether to mark the function as only internally accessible
82  void insert(const std::string& identifier,
83  const ConstructorT creator,
84  const bool internal = false);
85 
86  /// @brief Insert and register a function object to a function identifier.
87  /// @note Throws if the identifier is already registered
88  ///
89  /// @param identifier The function identifier to register
90  /// @param creator The function to link to the provided identifier
91  /// @param op FunctionOptions to pass the function constructor
92  /// @param internal Whether to mark the function as only internally accessible
93  void insertAndCreate(const std::string& identifier,
94  const ConstructorT creator,
95  const FunctionOptions& op,
96  const bool internal = false);
97 
98  /// @brief Return the corresponding function from a provided function identifier
99  /// @note Returns a nullptr if no such function identifier has been
100  /// registered or if the function is marked as internal
101  ///
102  /// @param identifier The function identifier
103  /// @param op FunctionOptions to pass the function constructor
104  /// @param allowInternalAccess Whether to look in the 'internal' functions
105  const FunctionGroup* getOrInsert(const std::string& identifier,
106  const FunctionOptions& op,
107  const bool allowInternalAccess);
108 
109  /// @brief Return the corresponding function from a provided function identifier
110  /// @note Returns a nullptr if no such function identifier has been
111  /// registered or if the function is marked as internal
112  ///
113  /// @param identifier The function identifier
114  /// @param allowInternalAccess Whether to look in the 'internal' functions
115  const FunctionGroup* get(const std::string& identifier,
116  const bool allowInternalAccess) const;
117 
118  /// @brief Force the (re)creations of all function objects for all
119  /// registered functions
120  /// @param op The current function options
121  /// @param verify Checks functions are created and have valid identifiers/symbols
122  void createAll(const FunctionOptions& op, const bool verify = false);
123 
124  /// @brief Return a const reference to the current registry map
125  inline const RegistryMap& map() const { return mMap; }
126 
127  /// @brief Return whether or not the registry is empty
128  inline bool empty() const { return mMap.empty(); }
129 
130  /// @brief Clear the underlying function registry
131  inline void clear() { mMap.clear(); }
132 
133 private:
134  RegistryMap mMap;
135 };
136 
137 namespace internal
138 {
139 
140 OPENVDB_AX_API void
142  llvm::LLVMContext* CPtr,
143  FunctionRegistry* reg,
144  const FunctionOptions& opts);
145 
146 OPENVDB_AX_API std::pair<FunctionRegistry*, FunctionOptions>
147 GetMappedFunctionRegistry(llvm::LLVMContext* CPtr);
148 
149 OPENVDB_AX_API bool RemoveMappedFunctionRegistry(llvm::LLVMContext* CPtr);
150 
151 }
152 
153 } // namespace codegen
154 } // namespace ax
155 } // namespace OPENVDB_VERSION_NAME
156 } // namespace openvdb
157 
158 #endif // OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
159 
OPENVDB_AX_API std::pair< FunctionRegistry *, FunctionOptions > GetMappedFunctionRegistry(llvm::LLVMContext *CPtr)
Definition: FunctionRegistry.h:23
#define OPENVDB_AX_API
Definition: Platform.h:312
bool isInternal() const
Check whether this function should be only internally accesible.
Definition: FunctionRegistry.h:66
Options that control how functions behave.
Definition: CompilerOptions.h:24
void create(const FunctionOptions &op)
Create a function object using this creator of this function.
Definition: FunctionRegistry.h:60
std::unique_ptr< FunctionGroup > UniquePtr
Definition: FunctionTypes.h:1392
OutGridT XformOp & op
Definition: ValueTransformer.h:139
FunctionGroup::UniquePtr(*)(const FunctionOptions &) ConstructorT
Definition: FunctionRegistry.h:42
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
OPENVDB_AX_API void InsertMappedFunctionRegistry(llvm::LLVMContext *CPtr, FunctionRegistry *reg, const FunctionOptions &opts)
std::shared_ptr< FunctionGroup > Ptr
Definition: FunctionTypes.h:1391
std::unordered_map< std::string, RegisteredFunction > RegistryMap
Definition: FunctionRegistry.h:74
A group of functions which all have the same name but different signatures. For example: float abs(fl...
Definition: FunctionTypes.h:1389
bool empty() const
Return whether or not the registry is empty.
Definition: FunctionRegistry.h:128
Definition: Exceptions.h:13
OPENVDB_AX_API bool RemoveMappedFunctionRegistry(llvm::LLVMContext *CPtr)
OpenVDB AX Compiler Options.
The function registry which is used for function code generation. Each time a function is visited wit...
Definition: FunctionRegistry.h:39
void clear()
Clear the underlying function registry.
Definition: FunctionRegistry.h:131
std::unique_ptr< FunctionRegistry > UniquePtr
Definition: FunctionRegistry.h:44
An object to represent a registered function, storing its constructor, a pointer to the function defi...
Definition: FunctionRegistry.h:50
const RegistryMap & map() const
Return a const reference to the current registry map.
Definition: FunctionRegistry.h:125
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
std::shared_ptr< FunctionRegistry > Ptr
Definition: FunctionRegistry.h:43
RegisteredFunction(const ConstructorT &creator, const bool internal=false)
Constructor.
Definition: FunctionRegistry.h:55
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218