X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcollection.h;h=1f4b5562107a338190cf221355692aaa4c64d04f;hb=aa88b0343c2a02d13b166a789e453ce1a056039b;hp=ec7cde31786adda2049631226231d75731757346;hpb=b1bc25649c1f22abf940a807d934f1e9bb780c28;p=libs%2Fdatafile.git diff --git a/source/collection.h b/source/collection.h index ec7cde3..1f4b556 100644 --- a/source/collection.h +++ b/source/collection.h @@ -109,16 +109,27 @@ public: template T &get(const std::string &name) const { - return *get_item(items, name).value::Type> >(); + return extract(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 - T &get(const std::string &); + T &get(const std::string &name) + { + return extract(get_var(name, get_type())); + } private: + const Variant &get_var(const std::string &, const CollectionItemTypeBase *); + + template + T &extract(const Variant &var) const + { + return *var.value::Type> >(); + } + template void collect_items(std::list *objects, std::list *names, std::list *future_names) const { @@ -273,16 +284,13 @@ public: class CollectionItemTypeBase { protected: - class TagBase + struct TagBase { - protected: - TagBase() { } - public: virtual ~TagBase() { } }; template - class Tag: public TagBase + struct Tag: TagBase { }; std::string kwd; @@ -316,37 +324,28 @@ template class CollectionItemType: public CollectionItemTypeBase { private: - class CreatorBase + struct CreatorBase { - protected: - CreatorBase() { } - public: virtual ~CreatorBase() { } virtual T *create(Collection &, const std::string &) const = 0; }; template - class Creator: public CreatorBase + struct Creator: CreatorBase { - public: typedef T *(C::*FuncPtr)(const std::string &); - private: FuncPtr func; - public: Creator(FuncPtr f): func(f) { } virtual T *create(Collection &coll, const std::string &name) const { return (static_cast(coll).*func)(name); } }; - class StoreBase + struct StoreBase { - protected: - StoreBase() { } - public: virtual ~StoreBase() { } virtual void store(Collection &, const std::string &, T *) = 0; @@ -355,9 +354,8 @@ private: }; template - class Store: public StoreBase + struct Store: StoreBase { - public: virtual void store(Collection &coll, const std::string &name, T *obj) { coll.add(name, static_cast(obj)); } @@ -450,33 +448,6 @@ public: }; -template -T &Collection::get(const std::string &name) -{ - typedef RefPtr::Type> RPNCT; - - ItemMap::iterator i = items.find(name); - if(i!=items.end()) - return *i->second.value(); - - if(CollectionItemTypeBase *type = get_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).value(); -} - template CollectionItemType &Collection::add_type() {