forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.h
63 lines (51 loc) · 1.59 KB
/
Factory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_FACTORY_H
#define MATERIALX_FACTORY_H
/// @file
/// Class instantiator factory helper class
#include <MaterialXCore/Library.h>
namespace MaterialX
{
/// @class Factory
/// Factory class for creating instances of classes given their type name.
template<class T> class Factory
{
public:
using Ptr = shared_ptr<T>;
using CreatorFunction = Ptr(*)();
using CreatorMap = std::unordered_map<string, CreatorFunction>;
/// Register a new class given a unique type name
/// and a creator function for the class.
void registerClass(const string& typeName, CreatorFunction f)
{
_creatorMap[typeName] = f;
}
/// Determine if a class has been registered for a type name
bool classRegistered(const string& typeName) const
{
return _creatorMap.find(typeName) != _creatorMap.end();
}
/// Unregister a registered class
void unregisterClass(const string& typeName)
{
auto it = _creatorMap.find(typeName);
if (it != _creatorMap.end())
{
_creatorMap.erase(it);
}
}
/// Create a new instance of the class with given type name.
/// Returns nullptr if no class with given name is registered.
Ptr create(const string& typeName) const
{
auto it = _creatorMap.find(typeName);
return (it != _creatorMap.end() ? it->second() : nullptr);
}
private:
CreatorMap _creatorMap;
};
} // namespace MaterialX
#endif // MATERIALX_FACTORY_H