OpenVDB  9.0.1
FunctionRegistry.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-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 #include "Types.h"
17 
18 #include "../compiler/CompilerOptions.h"
19 
20 #include <openvdb/version.h>
21 
22 #include <unordered_map>
23 
24 namespace openvdb {
26 namespace OPENVDB_VERSION_NAME {
27 
28 namespace ax {
29 namespace codegen {
30 
31 /// @brief The function registry which is used for function code generation.
32 /// Each time a function is visited within the AST, its identifier is used as
33 /// a key into this registry for the corresponding function retrieval and
34 /// execution. Functions can be inserted into the registry using insert() with
35 /// a given identifier and pointer.
37 {
38 public:
40  using Ptr = std::shared_ptr<FunctionRegistry>;
41  using UniquePtr = std::unique_ptr<FunctionRegistry>;
42 
43  /// @brief An object to represent a registered function, storing its
44  /// constructor, a pointer to the function definition and whether it
45  /// should only be available internally (i.e. to a developer, not a user)
46  ///
48  {
49  /// @brief Constructor
50  /// @param creator The function definition used to create this function
51  /// @param internal Whether the function should be only internally accessible
52  RegisteredFunction(const ConstructorT& creator, const bool internal = false)
53  : mConstructor(creator), mFunction(), mInternal(internal) {}
54 
55  /// @brief Create a function object using this creator of this function
56  /// @param op The current function options
57  inline void create(const FunctionOptions& op) { mFunction = mConstructor(op); }
58 
59  /// @brief Return a pointer to this function definition
60  inline const FunctionGroup* function() const { return mFunction.get(); }
61 
62  /// @brief Check whether this function should be only internally accesible
63  inline bool isInternal() const { return mInternal; }
64 
65  private:
66  const ConstructorT mConstructor;
67  FunctionGroup::Ptr mFunction;
68  const bool mInternal;
69  };
70 
71  using RegistryMap = std::unordered_map<std::string, RegisteredFunction>;
72 
73  /// @brief Insert and register a function object to a function identifier.
74  /// @note Throws if the identifier is already registered
75  ///
76  /// @param identifier The function identifier to register
77  /// @param creator The function to link to the provided identifier
78  /// @param internal Whether to mark the function as only internally accessible
79  void insert(const std::string& identifier,
80  const ConstructorT creator,
81  const bool internal = false);
82 
83  /// @brief Insert and register a function object to a function identifier.
84  /// @note Throws if the identifier is already registered
85  ///
86  /// @param identifier The function identifier to register
87  /// @param creator The function to link to the provided identifier
88  /// @param op FunctionOptions to pass the function constructor
89  /// @param internal Whether to mark the function as only internally accessible
90  void insertAndCreate(const std::string& identifier,
91  const ConstructorT creator,
92  const FunctionOptions& op,
93  const bool internal = false);
94 
95  /// @brief Return the corresponding function from a provided function identifier
96  /// @note Returns a nullptr if no such function identifier has been
97  /// registered or if the function is marked as internal
98  ///
99  /// @param identifier The function identifier
100  /// @param op FunctionOptions to pass the function constructor
101  /// @param allowInternalAccess Whether to look in the 'internal' functions
102  const FunctionGroup* getOrInsert(const std::string& identifier,
103  const FunctionOptions& op,
104  const bool allowInternalAccess);
105 
106  /// @brief Return the corresponding function from a provided function identifier
107  /// @note Returns a nullptr if no such function identifier has been
108  /// registered or if the function is marked as internal
109  ///
110  /// @param identifier The function identifier
111  /// @param allowInternalAccess Whether to look in the 'internal' functions
112  const FunctionGroup* get(const std::string& identifier,
113  const bool allowInternalAccess) const;
114 
115  /// @brief Force the (re)creations of all function objects for all
116  /// registered functions
117  /// @param op The current function options
118  /// @param verify Checks functions are created and have valid identifiers/symbols
119  void createAll(const FunctionOptions& op, const bool verify = false);
120 
121  /// @brief Return a const reference to the current registry map
122  inline const RegistryMap& map() const { return mMap; }
123 
124  /// @brief Return whether or not the registry is empty
125  inline bool empty() const { return mMap.empty(); }
126 
127  /// @brief Clear the underlying function registry
128  inline void clear() { mMap.clear(); }
129 
130 private:
131  RegistryMap mMap;
132 };
133 
134 } // namespace codegen
135 } // namespace ax
136 } // namespace OPENVDB_VERSION_NAME
137 } // namespace openvdb
138 
139 #endif // OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
140 
std::unordered_map< std::string, RegisteredFunction > RegistryMap
Definition: FunctionRegistry.h:71
std::unique_ptr< FunctionRegistry > UniquePtr
Definition: FunctionRegistry.h:41
todo
Definition: FunctionTypes.h:790
Consolidated llvm types for most supported types.
bool isInternal() const
Check whether this function should be only internally accesible.
Definition: FunctionRegistry.h:63
void create(const FunctionOptions &op)
Create a function object using this creator of this function.
Definition: FunctionRegistry.h:57
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
The function registry which is used for function code generation. Each time a function is visited wit...
Definition: FunctionRegistry.h:36
std::unique_ptr< FunctionGroup > UniquePtr
Definition: FunctionTypes.h:793
void clear()
Clear the underlying function registry.
Definition: FunctionRegistry.h:128
Definition: Exceptions.h:13
bool empty() const
Return whether or not the registry is empty.
Definition: FunctionRegistry.h:125
FunctionGroup::UniquePtr(*)(const FunctionOptions &) ConstructorT
Definition: FunctionRegistry.h:39
Options that control how functions behave.
Definition: CompilerOptions.h:24
std::shared_ptr< FunctionRegistry > Ptr
Definition: FunctionRegistry.h:40
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
RegisteredFunction(const ConstructorT &creator, const bool internal=false)
Constructor.
Definition: FunctionRegistry.h:52
An object to represent a registered function, storing its constructor, a pointer to the function defi...
Definition: FunctionRegistry.h:47
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
std::shared_ptr< FunctionGroup > Ptr
Definition: FunctionTypes.h:792
const RegistryMap & map() const
Return a const reference to the current registry map.
Definition: FunctionRegistry.h:122