From: Mikko Rasa Date: Tue, 5 Oct 2021 22:00:48 +0000 (+0300) Subject: Add support for storing the loaded object in DynamicObjectLoader X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=616f6fa9d983da10bf323c45d7a44d94c5a09f1e;p=libs%2Fdatafile.git Add support for storing the loaded object in DynamicObjectLoader This allows the object to be stored in the collection using its actual type rather than the base class. --- diff --git a/source/dynamicobjectloader.h b/source/dynamicobjectloader.h index c83d914..b5830b8 100644 --- a/source/dynamicobjectloader.h +++ b/source/dynamicobjectloader.h @@ -2,6 +2,7 @@ #define MSP_DATAFILE_DYNAMICOBJECTLOADER_H_ #include +#include "collection.h" #include "except.h" #include "loader.h" @@ -33,6 +34,7 @@ protected: private: T *object; Loader *obj_loader; + std::function store_func; static ActionMap shared_actions; @@ -41,10 +43,14 @@ protected: public: ~DynamicObjectLoader() { delete object; delete obj_loader; } - T *get_object() { T *o = object; object = 0; return o; } private: virtual void init_actions(); +public: + T *get_object() { T *o = object; object = 0; return o; } + void store_object(Collection &, const std::string &); + +private: void type(const Symbol &); template @@ -76,6 +82,16 @@ void DynamicObjectLoader::init_actions() add("type", &DynamicObjectLoader::type); } +template +void DynamicObjectLoader::store_object(Collection &c, const std::string &name) +{ + if(!store_func) + throw std::logic_error("no store function"); + + store_func(c, name, object); + object = 0; +} + template void DynamicObjectLoader::type(const Symbol &t) { @@ -110,6 +126,7 @@ void DynamicObjectLoader::CreateObject::operator()(const std::string &, ldr.object = obj; ldr.obj_loader = ldr.create_object_loader(*obj); ldr.add_auxiliary_loader(*ldr.obj_loader); + ldr.store_func = [&ldr](Collection &c, const std::string &n, T *o){ c.add(n, static_cast(o)); }; } } // namespace DataFile