From: Mikko Rasa Date: Wed, 26 Oct 2016 21:12:46 +0000 (+0300) Subject: Prioritize matching suffixes when looking for a type X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=01dfbb71bc10eddf5496e899e87e2b0d25951aa6;p=libs%2Fdatafile.git Prioritize matching suffixes when looking for a type --- diff --git a/source/collection.h b/source/collection.h index 06b8e34..89e801f 100644 --- a/source/collection.h +++ b/source/collection.h @@ -131,7 +131,7 @@ public: T &get(const std::string &name) { typedef typename RemoveConst::Type NCT; - return extract(get_var(name, get_type())); + return extract(get_var(name, get_type(name))); } private: @@ -251,9 +251,10 @@ protected: CollectionItemType &add_type(); private: - /// Returns the descriptor for a type, or null if one isn't defined. + /** Returns the descriptor for a type, or null if one isn't defined. An + optional name can be given to prioritize matching types. */ template - CollectionItemTypeBase *get_type() const; + CollectionItemTypeBase *get_type(const std::string & = std::string()) const; /// Gets a descriptor with the same type as another descriptor. CollectionItemTypeBase *get_type(const CollectionItemTypeBase &) const; @@ -500,15 +501,20 @@ CollectionItemType &Collection::add_type() } template -CollectionItemTypeBase *Collection::get_type() const +CollectionItemTypeBase *Collection::get_type(const std::string &name) const { for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) if(dynamic_cast *>(*j)) return *j; + CollectionItemTypeBase *type = 0; for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) if((*j)->can_extract()) - return *j; - return 0; + { + if(!name.empty() && (*j)->match_name(name)) + return *j; + type = *j; + } + return type; } } // namespace DataFile