Smal refactoring.

This commit is contained in:
DH
2026-02-27 20:27:30 -05:00
parent c9c2bc11ae
commit 6d7cfc486e

View File

@@ -47,8 +47,8 @@
* typefactory.registerType<Derived1>("one"); * typefactory.registerType<Derived1>("one");
* typefactory.registerType<Derived2>("2"); * typefactory.registerType<Derived2>("2");
* *
* auto d1 = typefactory.create ("one")(10); * auto d1 = typefactory.creator ("one")(10);
* auto d2 = typefactory.create ("2")(10); * auto d2 = typefactory.creator ("2")(10);
* *
* d1->get(); * d1->get();
* d2->get(); * d2->get();
@@ -79,24 +79,25 @@ class TypeFactory {
template<class Derived> template<class Derived>
void registerType (const ClassId& id) { void registerType (const ClassId& id) {
/// Register Derived type by storing its constructor wrapper /// Register Derived type by storing its constructor wrapper
static_assert(std::is_base_of_v<BaseClass, Derived>, static_assert(std::is_base_of_v<BaseClass, Derived>,
"Derived must inherit from BaseClass"); "Derived must inherit from BaseClass");
static_assert(std::is_constructible_v<Derived, Args...>, static_assert(std::is_constructible_v<Derived, Args...>,
"Derived must be constructible from Args..."); "Derived must be constructible from Args...");
classes[id] = &typefactory<Derived>; classes[id] = &typefactory<Derived>;
} }
/** /**
* @brief create - Returns creator function for id. * @brief creator - Returns creator function for id.
* @param id - unique class identification * @param id - unique class identification
* @return shared pointer to new class. * @returns creator function.
* @throws std::out_of_range when id not found in map * @throws std::out_of_range when id not found in map
*/ */
typefactoryFunction create (const ClassId& id) const { typefactoryFunction creator (const ClassId& id) const {
return classes.at (id); return classes.at (id);
} }
BaseClass_SP create(const ClassId& id, Args... args) const {
return creator(id)(args...);
}
protected: protected:
/** /**
* @brief classes - Not thread-safe. Register types during initialization phase only. * @brief classes - Not thread-safe. Register types during initialization phase only.
@@ -105,7 +106,7 @@ class TypeFactory {
private: private:
/** /**
* @brief typefactory - main functionality, create registered object * @brief typefactory - main functionality, creator registered object
* @param Derived - class type * @param Derived - class type
* @param args - constructor arguments * @param args - constructor arguments
* @return shared pointer to register Derived class * @return shared pointer to register Derived class