]> git.tdb.fi Git - libs/datafile.git/commitdiff
Move most of Collection::get implementation to collection.cpp
authorMikko Rasa <tdb@tdb.fi>
Mon, 3 Dec 2012 21:20:02 +0000 (23:20 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 3 Dec 2012 21:20:02 +0000 (23:20 +0200)
source/collection.cpp
source/collection.h

index bf1c5edc211e93b33f1dacc833e0ac1a38657a5e..87982e8f4baa5df80ec1dbe257e33ba4b83af594 100644 (file)
@@ -11,6 +11,30 @@ Collection::~Collection()
                delete *i;
 }
 
+const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
+{
+       ItemMap::iterator i = items.find(name);
+       if(i!=items.end())
+               return i->second;
+
+       if(type)
+       {
+               bool loaded = false;
+               if(type->can_create())
+               {
+                       type->create_item(*this, name);
+                       loaded = items.count(name);
+               }
+               for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
+               {
+                       (*j)->load(*this, *type, name);
+                       loaded = items.count(name);
+               }
+       }
+
+       return get_item(items, name);
+}
+
 void Collection::add_source(CollectionSource &s)
 {
        sources.push_back(&s);
index ec7cde31786adda2049631226231d75731757346..be9ae88c491595f925db46e30aeff2daa67deb5f 100644 (file)
@@ -109,16 +109,27 @@ public:
        template<typename T>
        T &get(const std::string &name) const
        {
-               return *get_item(items, name).value<RefPtr<typename RemoveConst<T>::Type> >();
+               return extract<T>(get_item(items, name));
        }
 
        /** Gets a typed object from the collection.  If the name is not found,
        automatic creation with the type's creator function (if defined) or from
        sources (if present) is attempted. */
        template<typename T>
-       T &get(const std::string &);
+       T &get(const std::string &name)
+       {
+               return extract<T>(get_var(name, get_type<T>()));
+       }
 
 private:
+       const Variant &get_var(const std::string &, const CollectionItemTypeBase *);
+
+       template<typename T>
+       T &extract(const Variant &var) const
+       {
+               return *var.value<RefPtr<typename RemoveConst<T>::Type> >();
+       }
+
        template<typename T>
        void collect_items(std::list<T *> *objects, std::list<std::string> *names, std::list<std::string> *future_names) const
        {
@@ -450,33 +461,6 @@ public:
 };
 
 
-template<typename T>
-T &Collection::get(const std::string &name)
-{
-       typedef RefPtr<typename RemoveConst<T>::Type> RPNCT;
-
-       ItemMap::iterator i = items.find(name);
-       if(i!=items.end())
-               return *i->second.value<RPNCT>();
-
-       if(CollectionItemTypeBase *type = get_type<T>())
-       {
-               bool loaded = false;
-               if(type->can_create())
-               {
-                       type->create_item(*this, name);
-                       loaded = items.count(name);
-               }
-               for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
-               {
-                       (*j)->load(*this, *type, name);
-                       loaded = items.count(name);
-               }
-       }
-
-       return *get_item(items, name).value<RPNCT>();
-}
-
 template<typename T>
 CollectionItemType<T> &Collection::add_type()
 {