11 #ifndef RETDEC_LLVMIR2HLL_SUPPORT_FACTORY_H
12 #define RETDEC_LLVMIR2HLL_SUPPORT_FACTORY_H
45 #define REGISTER_AT_FACTORY(objectId, objectIdVarName, factory, createFunc) \
46 static const std::string objectIdVarName = [] { \
47 factory::getInstance().registerObject(objectId, createFunc); \
63 template<
typename ObjectIDType,
class AbstractObject>
118 class AbstractObject,
119 typename ObjectIDType,
121 template<typename, class>
124 class Factory:
public FactoryErrorPolicy<ObjectIDType, AbstractObject> {
146 return associations.insert(
typename IDToObjectMap::value_type(
147 id, creator)).second;
157 return associations.erase(
id) == 1;
167 template<
typename... Args>
169 Args &&... args)
const {
170 auto i = associations.find(
id);
171 if (i != associations.end()) {
173 return (i->second)(std::forward<Args>(args)...);
175 return this->onUnknownType(
id);
182 std::vector<ObjectIDType> regObjects;
183 for (
const auto &item : associations) {
184 regObjects.push_back(item.first);
194 return associations.find(
id) != associations.end();
Handles the "Unknown Type" error in an object factory.
Definition: factory.h:64
static ShPtr< AbstractObject > onUnknownType(ObjectIDType id)
Reaction to the "Unknown Type" error.
Definition: factory.h:72
DefaultFactoryError()=default
Default constructor.
~DefaultFactoryError()=default
Destructor.
Implementation of a generic object factory.
Definition: factory.h:124
IDToObjectMap associations
Container used to map an object ID to its creator function.
Definition: factory.h:199
bool registerObject(ObjectIDType id, ObjectCreator creator)
Registers the given object.
Definition: factory.h:145
bool unregisterObject(const ObjectIDType &id) const
Unregisters the given object.
Definition: factory.h:156
ShPtr< AbstractObject > createObject(const ObjectIDType &id, Args &&... args) const
Creates an instance of the given object with the given arguments.
Definition: factory.h:168
bool isRegistered(const ObjectIDType &id) const
Returns true if there is an object registered with the selected ID, false otherwise.
Definition: factory.h:193
std::map< ObjectIDType, ObjectCreator > IDToObjectMap
Type of a container used to map an object ID to its creator function.
Definition: factory.h:127
Factory()
Default constructor.
Definition: factory.h:133
std::vector< ObjectIDType > getRegisteredObjects() const
Returns a vector of all registered object IDs.
Definition: factory.h:181
A library providing API for working with back-end IR.
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
Definition: archive_wrapper.h:19
Declarations, aliases, macros, etc. for the use of smart pointers.