lundi 2 septembre 2019

Interfaced deserializations and serializations with independent storage

I have a serialization/deserialization library. I would like it to be independent on the storage (XML, database, etc.) and the configurations supporting the serializations to be interfaced. This leads to the runtime/compile time polymorphism with the member templated functions. Now I use the interface as a wrapper, have a complicated checks that the methods of ser/deser are implemented and then a dynamic_cast to the actual configuration. Example like so:

IConfig
{
   ~IConfig{} = default;
}

FooConfig : public IConfig
{
public:
    template<typename Storage>
    void ser(Storage& storage) {}
    template<typename Storage>
    void deser(Storage& storage) {}
}

Just so I can store the instance of any configuration and then when it comes to the actual ser/deser use somethig like.

// being inside templated (type T) class holder
// which handles the actual type of storage (in this case XML)
void save(std::shared_ptr<IConfig> config)
{
    XMLStorage xmlStorage;
    auto cnfg = std::dynamic_pointer_cast<T>(config);
    cnfg->serialize(xmlStorage);
}

I would like to avoid the dynamic_cast and the complicated checks of implemented member methods (ser/deser). I have thought of type-erasure approach, but that does not work for the independence of the storage type. And I don't know if CRTP would be of any help either. Is there pattern that would solve this issue?

Aucun commentaire:

Enregistrer un commentaire