From: Mikko Rasa Date: Fri, 7 Jun 2019 17:27:17 +0000 (+0300) Subject: Allow modifying existing types in a collection X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=7f49649f70becbb4345ad0b936c5e00194d9dc82 Allow modifying existing types in a collection --- diff --git a/source/collection.h b/source/collection.h index bc84d62..005abd0 100644 --- a/source/collection.h +++ b/source/collection.h @@ -237,6 +237,11 @@ protected: template CollectionItemType &add_type(); + /** Returns a mutable reference to an existing type descriptor. This can be + used to e.g. override the creator function of a type added by a base class. */ + template + CollectionItemType &modify_type(); + private: /** Returns the descriptor for a type, or null if one isn't defined. An optional name can be given to prioritize matching types. */ @@ -488,6 +493,16 @@ CollectionItemType &Collection::add_type() return *type; } +template +CollectionItemType &Collection::modify_type() +{ + for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) + if(CollectionItemType *t = dynamic_cast *>(*j)) + return *t; + + throw std::logic_error("type not found in collection"); +} + template CollectionItemTypeBase *Collection::get_type(const std::string &name) const {