From 01dfbb71bc10eddf5496e899e87e2b0d25951aa6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 27 Oct 2016 00:12:46 +0300 Subject: [PATCH] Prioritize matching suffixes when looking for a type --- source/collection.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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 -- 2.43.0