OpenVDB 13.0.0

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
17#include "openvdb_ax/compiler/CompilerOptions.h"
18
19#include <openvdb/version.h>
20
21#include <unordered_map>
22
23 namespace llvm {
24 class LLVMContext;
25}
26
27 namespace openvdb {
28 OPENVDB_USE_VERSION_NAMESPACE
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.
39 class OPENVDB_AX_API FunctionRegistry
40{
41 public:
42 using ConstructorT = FunctionGroup::UniquePtr(*)(const FunctionOptions&);
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 ///
50 struct RegisteredFunction
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
141 InsertMappedFunctionRegistry(
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
CompilerOptions.h
OpenVDB AX Compiler Options.
FunctionTypes.h
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
OPENVDB_AX_API
#define OPENVDB_AX_API
Definition Platform.h:312
openvdb::v13_0::ax::codegen::FunctionRegistry
The function registry which is used for function code generation. Each time a function is visited wit...
Definition FunctionRegistry.h:40
openvdb::v13_0::ax::codegen::FunctionRegistry::Ptr
std::shared_ptr< FunctionRegistry > Ptr
Definition FunctionRegistry.h:43
openvdb::v13_0::ax::codegen::FunctionRegistry::ConstructorT
FunctionGroup::UniquePtr(*)(const FunctionOptions &) ConstructorT
Definition FunctionRegistry.h:42
openvdb::v13_0::ax::codegen::FunctionRegistry::getOrInsert
const FunctionGroup * getOrInsert(const std::string &identifier, const FunctionOptions &op, const bool allowInternalAccess)
Return the corresponding function from a provided function identifier.
openvdb::v13_0::ax::codegen::FunctionRegistry::empty
bool empty() const
Return whether or not the registry is empty.
Definition FunctionRegistry.h:128
openvdb::v13_0::ax::codegen::FunctionRegistry::RegistryMap
std::unordered_map< std::string, RegisteredFunction > RegistryMap
Definition FunctionRegistry.h:74
openvdb::v13_0::ax::codegen::FunctionRegistry::map
const RegistryMap & map() const
Return a const reference to the current registry map.
Definition FunctionRegistry.h:125
openvdb::v13_0::ax::codegen::FunctionRegistry::createAll
void createAll(const FunctionOptions &op, const bool verify=false)
Force the (re)creations of all function objects for all registered functions.
openvdb::v13_0::ax::codegen::FunctionRegistry::insertAndCreate
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.
openvdb::v13_0::ax::codegen::FunctionRegistry::get
const FunctionGroup * get(const std::string &identifier, const bool allowInternalAccess) const
Return the corresponding function from a provided function identifier.
openvdb::v13_0::ax::codegen::FunctionRegistry::insert
void insert(const std::string &identifier, const ConstructorT creator, const bool internal=false)
Insert and register a function object to a function identifier.
openvdb::v13_0::ax::codegen::FunctionRegistry::clear
void clear()
Clear the underlying function registry.
Definition FunctionRegistry.h:131
openvdb::v13_0::ax::codegen::FunctionRegistry::UniquePtr
std::unique_ptr< FunctionRegistry > UniquePtr
Definition FunctionRegistry.h:44
llvm
Definition FunctionRegistry.h:23
openvdb::v13_0::ax::codegen::internal
Definition FunctionRegistry.h:138
openvdb::v13_0::ax::codegen::internal::RemoveMappedFunctionRegistry
OPENVDB_AX_API bool RemoveMappedFunctionRegistry(llvm::LLVMContext *CPtr)
openvdb::v13_0::ax::codegen::internal::InsertMappedFunctionRegistry
OPENVDB_AX_API void InsertMappedFunctionRegistry(llvm::LLVMContext *CPtr, FunctionRegistry *reg, const FunctionOptions &opts)
openvdb::v13_0::ax::codegen::internal::GetMappedFunctionRegistry
OPENVDB_AX_API std::pair< FunctionRegistry *, FunctionOptions > GetMappedFunctionRegistry(llvm::LLVMContext *CPtr)
openvdb
Definition Exceptions.h:13
openvdb::v13_0::ax::FunctionOptions
Options that control how functions behave.
Definition CompilerOptions.h:25
openvdb::v13_0::ax::codegen::FunctionGroup
A group of functions which all have the same name but different signatures. For example: float abs(fl...
Definition FunctionTypes.h:1396
openvdb::v13_0::ax::codegen::FunctionGroup::Ptr
std::shared_ptr< FunctionGroup > Ptr
Definition FunctionTypes.h:1397
openvdb::v13_0::ax::codegen::FunctionGroup::UniquePtr
std::unique_ptr< FunctionGroup > UniquePtr
Definition FunctionTypes.h:1398
openvdb::v13_0::ax::codegen::FunctionRegistry::RegisteredFunction::RegisteredFunction
RegisteredFunction(const ConstructorT &creator, const bool internal=false)
Constructor.
Definition FunctionRegistry.h:55
openvdb::v13_0::ax::codegen::FunctionRegistry::RegisteredFunction::function
const FunctionGroup * function() const
Return a pointer to this function definition.
Definition FunctionRegistry.h:63
openvdb::v13_0::ax::codegen::FunctionRegistry::RegisteredFunction::create
void create(const FunctionOptions &op)
Create a function object using this creator of this function.
Definition FunctionRegistry.h:60
openvdb::v13_0::ax::codegen::FunctionRegistry::RegisteredFunction::isInternal
bool isInternal() const
Check whether this function should be only internally accesible.
Definition FunctionRegistry.h:66
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218