]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.h
Add facility for classes to specify what type they should be loaded as
[libs/datafile.git] / source / collection.h
index 82339d8859902f725308b347f9f3de3fd39ff244..b2239905edb4d824cd14e1d763ff62d234672032 100644 (file)
@@ -46,7 +46,7 @@ public:
                template<typename T> friend class CollectionItemType;
 
        private:
-               template<typename T, typename S, bool = NeedsCollection<T>::value >
+               template<typename T, typename S, bool = NeedsCollection<typename T::Loader>::value>
                struct Add;
 
                Collection &coll;
@@ -58,7 +58,7 @@ public:
                template<typename T, typename S, typename C>
                void coll_item(const std::string &n)
                {
-                       RefPtr<T> it=new T;
+                       RefPtr<T> it = new T;
                        load_sub(*it, dynamic_cast<C &>(coll));
                        coll.add<S>(n, it.get());
                        it.release();
@@ -67,13 +67,11 @@ public:
                template<typename T, typename S>
                void item(const std::string &n)
                {
-                       RefPtr<T> it=new T;
+                       RefPtr<T> it = new T;
                        load_sub(*it);
                        coll.add<S>(n, it.get());
                        it.release();
                }
-
-               template<typename, typename, bool> friend class ItemKeyword;
        };
 
 private:
@@ -292,17 +290,17 @@ private:
                { Collection::Loader::Add<T, S>::add(loader, kwd); }
        };
 
-       CreatorBase *creator;
+       CreatorBase *creat;
        StoreBase *store;
 
 public:
        CollectionItemType():
-               creator(0), store(new Store<T>)
+               creat(0), store(new Store<T>)
        { tag = new Tag<T>; }
 
        ~CollectionItemType()
        {
-               delete creator;
+               delete creat;
                delete store;
        }
 
@@ -313,10 +311,10 @@ public:
        }
 
        template<typename C>
-       CollectionItemType &create(T *(C::*func)(const std::string &))
+       CollectionItemType &creator(T *(C::*func)(const std::string &))
        {
-               delete creator;
-               creator = new Creator<C>(func);
+               delete creat;
+               creat = new Creator<C>(func);
                return *this;
        }
 
@@ -334,14 +332,15 @@ public:
        { store->add_to_loader(loader, kwd); }
 
        virtual bool can_create() const
-       { return creator!=0; }
+       { return creat!=0; }
 
        virtual void create_item(Collection &coll, const std::string &name) const
        {
-               if(!creator)
+               if(!creat)
                        throw std::runtime_error("no creator");
-               T *obj = creator->create(coll, name);
-               store->store(coll, name, obj);
+               T *obj = creat->create(coll, name);
+               if(obj)
+                       store->store(coll, name, obj);
        }
 };