]> git.tdb.fi Git - libs/datafile.git/commitdiff
Allow modifying existing types in a collection
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 17:27:17 +0000 (20:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 17:27:17 +0000 (20:27 +0300)
source/collection.h

index bc84d624b2948c4c9bb46eb1716581de22b8da62..005abd0db39e3c54c8b934ea784e5431164d70e4 100644 (file)
@@ -237,6 +237,11 @@ protected:
        template<typename T>
        CollectionItemType<T> &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<typename T>
+       CollectionItemType<T> &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<T> &Collection::add_type()
        return *type;
 }
 
+template<typename T>
+CollectionItemType<T> &Collection::modify_type()
+{
+       for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+               if(CollectionItemType<T> *t = dynamic_cast<CollectionItemType<T> *>(*j))
+                       return *t;
+
+       throw std::logic_error("type not found in collection");
+}
+
 template<typename T>
 CollectionItemTypeBase *Collection::get_type(const std::string &name) const
 {