Loading...
Searching...
No Matches
FunctionRegistry.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file codegen/FunctionRegistry.h
///
/// @authors Nick Avramoussis
///
/// @brief Contains the global function registration definition which
/// described all available user front end functions
///
#ifndef OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
#define OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
#include "FunctionTypes.h"
#include "openvdb_ax/compiler/CompilerOptions.h"
#include <openvdb/version.h>
#include <unordered_map>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace ax {
namespace codegen {
/// @brief The function registry which is used for function code generation.
/// Each time a function is visited within the AST, its identifier is used as
/// a key into this registry for the corresponding function retrieval and
/// execution. Functions can be inserted into the registry using insert() with
/// a given identifier and pointer.
class OPENVDB_AX_API FunctionRegistry
{
public:
using ConstructorT = FunctionGroup::UniquePtr(*)(const FunctionOptions&);
using Ptr = std::shared_ptr<FunctionRegistry>;
using UniquePtr = std::unique_ptr<FunctionRegistry>;
/// @brief An object to represent a registered function, storing its
/// constructor, a pointer to the function definition and whether it
/// should only be available internally (i.e. to a developer, not a user)
///
struct RegisteredFunction
{
/// @brief Constructor
/// @param creator The function definition used to create this function
/// @param internal Whether the function should be only internally accessible
RegisteredFunction(const ConstructorT& creator, const bool internal = false)
: mConstructor(creator), mFunction(), mInternal(internal) {}
/// @brief Create a function object using this creator of this function
/// @param op The current function options
inline void create(const FunctionOptions& op) { mFunction = mConstructor(op); }
/// @brief Return a pointer to this function definition
inline const FunctionGroup* function() const { return mFunction.get(); }
/// @brief Check whether this function should be only internally accesible
inline bool isInternal() const { return mInternal; }
private:
ConstructorT mConstructor;
FunctionGroup::Ptr mFunction;
bool mInternal;
};
using RegistryMap = std::unordered_map<std::string, RegisteredFunction>;
/// @brief Insert and register a function object to a function identifier.
/// @note Throws if the identifier is already registered
///
/// @param identifier The function identifier to register
/// @param creator The function to link to the provided identifier
/// @param internal Whether to mark the function as only internally accessible
void insert(const std::string& identifier,
const ConstructorT creator,
const bool internal = false);
/// @brief Insert and register a function object to a function identifier.
/// @note Throws if the identifier is already registered
///
/// @param identifier The function identifier to register
/// @param creator The function to link to the provided identifier
/// @param op FunctionOptions to pass the function constructor
/// @param internal Whether to mark the function as only internally accessible
void insertAndCreate(const std::string& identifier,
const ConstructorT creator,
const FunctionOptions& op,
const bool internal = false);
/// @brief Return the corresponding function from a provided function identifier
/// @note Returns a nullptr if no such function identifier has been
/// registered or if the function is marked as internal
///
/// @param identifier The function identifier
/// @param op FunctionOptions to pass the function constructor
/// @param allowInternalAccess Whether to look in the 'internal' functions
const FunctionGroup* getOrInsert(const std::string& identifier,
const FunctionOptions& op,
const bool allowInternalAccess);
/// @brief Return the corresponding function from a provided function identifier
/// @note Returns a nullptr if no such function identifier has been
/// registered or if the function is marked as internal
///
/// @param identifier The function identifier
/// @param allowInternalAccess Whether to look in the 'internal' functions
const FunctionGroup* get(const std::string& identifier,
const bool allowInternalAccess) const;
/// @brief Force the (re)creations of all function objects for all
/// registered functions
/// @param op The current function options
/// @param verify Checks functions are created and have valid identifiers/symbols
void createAll(const FunctionOptions& op, const bool verify = false);
/// @brief Return a const reference to the current registry map
inline const RegistryMap& map() const { return mMap; }
/// @brief Return whether or not the registry is empty
inline bool empty() const { return mMap.empty(); }
/// @brief Clear the underlying function registry
inline void clear() { mMap.clear(); }
private:
RegistryMap mMap;
};
} // namespace codegen
} // namespace ax
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_AX_CODEGEN_FUNCTION_REGISTRY_HAS_BEEN_INCLUDED
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::v10_0::internal
Definition openvdb.h:114
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::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