X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fcollection.h;h=0d2dc6be2fd7a6c4043ff4db18bfdc32a04bb270;hp=d5853f2ea6cb4a3fba658dd5912e148f419cb8e4;hb=9b1656018f783eb4aad2fbdc1de1404691e89bb1;hpb=bcbe8b90de68e18260313b8f0e8a8dd9c7661903 diff --git a/source/collection.h b/source/collection.h index d5853f2..0d2dc6b 100644 --- a/source/collection.h +++ b/source/collection.h @@ -45,6 +45,11 @@ method for details. Collections can have sources for loading objects on demand. Automatic loading only works on a non-const Collection. See class CollectionSource for details. + +A fallback collection can be designated as another way of loading items that +are not present. Items retrieted from the fallback collection are shared +between the collections, and are only deleted when all collections in the chain +have been destroyed. */ class Collection { @@ -75,7 +80,6 @@ public: } }; -protected: template::value> class ItemLoader; @@ -87,11 +91,12 @@ private: TypeList types; ItemMap items; SourceList sources; + Collection *fallback; Collection(const Collection &); Collection &operator=(const Collection &); public: - Collection() { } + Collection(); virtual ~Collection(); /** Adds an object into the collection. The name must not pre-exist. The @@ -119,7 +124,7 @@ public: template T &get(const std::string &name) const { - return extract(get_item(items, name)); + return extract::Type>(get_item(items, name)); } /** Gets a typed object from the collection. If the name is not found, @@ -128,7 +133,8 @@ public: template T &get(const std::string &name) { - return extract(get_var(name, get_type())); + typedef typename RemoveConst::Type NCT; + return extract(get_var(name, get_type())); } private: @@ -247,6 +253,7 @@ protected: template CollectionItemType &add_type(); +private: /// Returns the descriptor for a type, or null if one isn't defined. template CollectionItemTypeBase *get_type() const; @@ -254,11 +261,20 @@ protected: /// Returns the descriptor for an item, or null if it's of an unknown type. CollectionItemTypeBase *get_type_for_item(const Variant &) const; +protected: void add_source(CollectionSource &); + IO::Seekable *open_from_sources(const std::string &); + +private: void gather_names_from_sources(std::list &, const CollectionItemTypeBase &) const; void load_items_from_sources(const CollectionItemTypeBase &); + +protected: + /** Sets a fallback collection, which will be consulted if an item is not + found. */ + void set_fallback(Collection *); }; template @@ -306,6 +322,7 @@ public: const std::string &get_keyword() const { return kwd; } void add_suffix(const std::string &); bool match_name(const std::string &) const; + virtual bool is_same_type(const CollectionItemTypeBase &) const = 0; virtual bool check_item_type(const Variant &) const = 0; virtual void add_to_loader(Collection::Loader &) const = 0; virtual bool can_create() const = 0; @@ -420,6 +437,9 @@ public: return *this; } + virtual bool is_same_type(const CollectionItemTypeBase &other) const + { return dynamic_cast *>(&other); } + virtual bool check_item_type(const Variant &var) const { return var.check_type >(); } @@ -452,14 +472,12 @@ public: template T &Collection::extract(const Variant &var) const { - typedef RefPtr::Type> RPNCT; - - if(!var.check_type()) + if(!var.check_type >()) if(CollectionItemTypeBase *type = get_type_for_item(var)) if(T *item = type->extract(var)) return *item; - return *var.value(); + return *var.value >(); } template @@ -474,7 +492,7 @@ template CollectionItemTypeBase *Collection::get_type() const { for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) - if(dynamic_cast::Type> *>(*j)) + if(dynamic_cast *>(*j)) return *j; for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) if((*j)->can_extract())