MaterialX 1.39.1
Loading...
Searching...
No Matches
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
11
13
14MATERIALX_NAMESPACE_BEGIN
15
18template <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
27 void registerClass(const string& typeName, CreatorFunction f)
28 {
29 _creatorMap[typeName] = f;
30 }
31
33 bool classRegistered(const string& typeName) const
34 {
35 return _creatorMap.find(typeName) != _creatorMap.end();
36 }
37
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
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
60MATERIALX_NAMESPACE_END
61
62#endif // MATERIALX_FACTORY_H
Library-wide includes and types.
Factory class for creating instances of classes given their type name.
Definition: Factory.h:19
void unregisterClass(const string &typeName)
Unregister a registered class.
Definition: Factory.h:39
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:27
bool classRegistered(const string &typeName) const
Determine if a class has been registered for a type name.
Definition: Factory.h:33
Ptr create(const string &typeName) const
Create a new instance of the class with given type name.
Definition: Factory.h:50