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