HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Factory.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_FACTORY_H
7 #define MATERIALX_FACTORY_H
8 
9 /// @file
10 /// Class instantiator factory helper class
11 
12 #include <MaterialXCore/Library.h>
13 
15 
16 /// @class Factory
17 /// Factory class for creating instances of classes given their type name.
18 template <class T> class Factory
19 {
20  public:
21  using Ptr = shared_ptr<T>;
22  using CreatorFunction = Ptr (*)();
23  using CreatorMap = std::unordered_map<string, CreatorFunction>;
24 
25  /// Register a new class given a unique type name
26  /// and a creator function for the class.
27  void registerClass(const string& typeName, CreatorFunction f)
28  {
29  _creatorMap[typeName] = f;
30  }
31 
32  /// Determine if a class has been registered for a type name
33  bool classRegistered(const string& typeName) const
34  {
35  return _creatorMap.find(typeName) != _creatorMap.end();
36  }
37 
38  /// Unregister a registered class
39  void unregisterClass(const string& typeName)
40  {
41  auto it = _creatorMap.find(typeName);
42  if (it != _creatorMap.end())
43  {
44  _creatorMap.erase(it);
45  }
46  }
47 
48  /// Create a new instance of the class with given type name.
49  /// Returns nullptr if no class with given name is registered.
50  Ptr create(const string& typeName) const
51  {
52  auto it = _creatorMap.find(typeName);
53  return (it != _creatorMap.end() ? it->second() : nullptr);
54  }
55 
56  private:
57  CreatorMap _creatorMap;
58 };
59 
61 
62 #endif // MATERIALX_FACTORY_H
Ptr create(const string &typeName) const
Definition: Factory.h:50
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
shared_ptr< ShaderNodeImpl > Ptr
Definition: Factory.h:21
std::unordered_map< string, CreatorFunction > CreatorMap
Definition: Factory.h:23
GLfloat f
Definition: glcorearb.h:1926
Ptr(*)( CreatorFunction)
Definition: Factory.h:22
bool classRegistered(const string &typeName) const
Determine if a class has been registered for a type name.
Definition: Factory.h:33
void unregisterClass(const string &typeName)
Unregister a registered class.
Definition: Factory.h:39
void registerClass(const string &typeName, CreatorFunction f)
Definition: Factory.h:27
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26