X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcollection.h;h=9f5ddfcf7f78e5ba988636cc501edf05a87247a2;hb=01de40aa67599511f7843ab5b5aa24ec59a2768f;hp=66c50fe69d2c86e85ce38ac1e2b4bb73fd8672b3;hpb=495920e971324d29bd4933b8a319fb5668ca5562;p=libs%2Fdatafile.git diff --git a/source/collection.h b/source/collection.h index 66c50fe..9f5ddfc 100644 --- a/source/collection.h +++ b/source/collection.h @@ -55,34 +55,27 @@ public: template friend class CollectionItemType; private: - template::value> - struct Add; - Collection &coll; public: Loader(Collection &); Collection &get_object() const { return coll; } private: - template - void coll_item(const std::string &n) - { - RefPtr it = new T; - load_sub(*it, dynamic_cast(coll)); - coll.add(n, it.get()); - it.release(); - } - template void item(const std::string &n) { RefPtr it = new T; - load_sub(*it); + ItemLoader ldr(*it, coll); + load_sub_with(ldr); coll.add(n, it.get()); it.release(); } }; +protected: + template::value> + class ItemLoader; + private: typedef std::map ItemMap; typedef std::list TypeList; @@ -137,8 +130,14 @@ protected: insert_unique(items, name, ptr); } + /** Adds the name of a future object, guessing its type. If a type matching + the name can't be found, nothing is done. */ void add_future(const std::string &name); + /** Adds the name of a future object, using a keyword to determine its type. + The keyword must be known to the collection. */ + void add_future_with_keyword(const std::string &name, const std::string &); + public: /// Gets a typed object from the collection. template @@ -146,7 +145,7 @@ public: { typedef typename RemoveConst::Type NCT; - T *ptr = get_item(items, name).value >(); + T *ptr = get_item(items, name).value >().get(); if(!ptr) throw key_error(typeid(ItemMap)); return *ptr; @@ -159,7 +158,7 @@ public: private: template - void collect_items(std::list *objects, std::list *names, std::list *future_names) + void collect_items(std::list *objects, std::list *names, std::list *future_names) const { typedef RefPtr::Type> RPNCT; @@ -272,19 +271,22 @@ protected: CollectionItemType &add_type(); }; - -template -struct Collection::Loader::Add +template +class Collection::ItemLoader: public T::Loader { - static void add(Loader &loader, const std::string &kwd) - { loader.add(kwd, &Loader::item); } +public: + ItemLoader(T &o, Collection &): + T::Loader(o) + { } }; -template -struct Collection::Loader::Add +template +class Collection::ItemLoader: public T::Loader { - static void add(Loader &loader, const std::string &kwd) - { loader.add(kwd, &Loader::coll_item); } +public: + ItemLoader(T &o, Collection &c): + T::Loader(o, dynamic_cast(c)) + { } }; @@ -312,11 +314,12 @@ public: virtual ~CollectionItemTypeBase(); void set_keyword(const std::string &); + const std::string &get_keyword() const { return kwd; } void add_suffix(const std::string &); + bool match_name(const std::string &) const; virtual void add_to_loader(Collection::Loader &) const = 0; virtual bool can_create() const = 0; virtual void create_item(Collection &, const std::string &) const = 0; - bool match_name(const std::string &) const; virtual Variant create_future() const = 0; template @@ -383,7 +386,7 @@ private: { return RefPtr(0); } virtual void add_to_loader(Collection::Loader &loader, const std::string &kwd) - { Collection::Loader::Add::add(loader, kwd); } + { loader.add(kwd, &Collection::Loader::item); } }; CreatorBase *creat;