MaterialX 1.38.2
Factory.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_FACTORY_H
7#define MATERIALX_FACTORY_H
8
11
13
14namespace MaterialX
15{
16
19template<class T> class Factory
20{
21 public:
22 using Ptr = shared_ptr<T>;
23 using CreatorFunction = Ptr(*)();
24 using CreatorMap = std::unordered_map<string, CreatorFunction>;
25
28 void registerClass(const string& typeName, CreatorFunction f)
29 {
30 _creatorMap[typeName] = f;
31 }
32
34 bool classRegistered(const string& typeName) const
35 {
36 return _creatorMap.find(typeName) != _creatorMap.end();
37 }
38
40 void unregisterClass(const string& typeName)
41 {
42 auto it = _creatorMap.find(typeName);
43 if (it != _creatorMap.end())
44 {
45 _creatorMap.erase(it);
46 }
47 }
48
51 Ptr create(const string& typeName) const
52 {
53 auto it = _creatorMap.find(typeName);
54 return (it != _creatorMap.end() ? it->second() : nullptr);
55 }
56
57 private:
58 CreatorMap _creatorMap;
59};
60
61} // namespace MaterialX
62
63#endif // MATERIALX_FACTORY_H
Library-wide includes and types.
Factory class for creating instances of classes given their type name.
Definition: Factory.h:20
void unregisterClass(const string &typeName)
Unregister a registered class.
Definition: Factory.h:40
void registerClass(const string &typeName, CreatorFunction f)
Register a new class given a unique type name and a creator function for the class.
Definition: Factory.h:28
bool classRegistered(const string &typeName) const
Determine if a class has been registered for a type name.
Definition: Factory.h:34
Ptr create(const string &typeName) const
Create a new instance of the class with given type name.
Definition: Factory.h:51