#ifndef AISTATE_H #define AISTATE_H #include "aistatefwd.hpp" #include "aitemporarybase.hpp" #include namespace MWMechanics { /** \brief stores one object of any class derived from Base. * Requesting a certain derived class via get() either returns * the stored object if it has the correct type or otherwise replaces * it with an object of the requested type. */ template class DerivedClassStorage { private: std::unique_ptr mStorage; public: /// \brief returns reference to stored object or deletes it and creates a fitting template Derived& get() { Derived* result = dynamic_cast(mStorage.get()); if (result == nullptr) { auto storage = std::make_unique(); result = storage.get(); mStorage = std::move(storage); } // return a reference to the (new allocated) object return *result; } template void copy(DerivedClassStorage& destination) const { Derived* result = dynamic_cast(mStorage.get()); if (result != nullptr) destination.store(*result); } template void store(const Derived& payload) { mStorage = std::make_unique(payload); } /// \brief takes ownership of the passed object template void moveIn(std::unique_ptr&& storage) { mStorage = std::move(storage); } }; } #endif // AISTATE_H