OpenVDB: openvdb_ax/codegen/FunctionRegistry.h Source File

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