]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.h
Add a getter for fallback in Collection
[libs/datafile.git] / source / collection.h
index 24be69dd2f70cd07d425453b3121788b2ff60086..06b8e348cbe3d2a7e16ce2735971802efa73a863 100644 (file)
@@ -1,16 +1,15 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_DATAFILE_COLLECTION_H_
 #define MSP_DATAFILE_COLLECTION_H_
 
+#include <msp/core/maputils.h>
+#include <msp/core/meta.h>
 #include <msp/core/refptr.h>
+#include "collectionsource.h"
 #include "loader.h"
 
+/* XXX This file is a big mess with too many things in it.  However, the
+dependencies between those things make it difficult to split up. */
+
 namespace Msp {
 namespace DataFile {
 
@@ -18,298 +17,500 @@ namespace DataFile {
 Helper struct to determine whether a Loader has a Collection typedef.
 */
 template<typename T>
-struct NeedsCollection
+struct NeedsCollection: public Sfinae
 {
-       struct Yes { char c[2]; };
-       struct No { char c; };
-
        template<typename U>
        static Yes f(typename U::Collection *);
        template<typename U>
        static No f(...);
 
-       enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
+       enum { value = Evaluate<sizeof(f<T>(0))>::value };
 };
 
-template<typename T>
-struct RemoveConst
-{ typedef T Type; };
+class CollectionItemTypeBase;
 
 template<typename T>
-struct RemoveConst<const T>
-{ typedef T Type; };
+class CollectionItemType;
 
 /**
 A collection of objects that can be loaded from a datafile.  Each object is
 identified by a name, which must be unique across the entire collection.
+
+While this class can be instantiated by itself and used for storing objects,
+loading requires that a subclass defines the supported types.  See the add_type
+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.
+
+As a last resort, a fallback collection can be designated for 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
 {
 public:
-       class Loader;
+       /**
+       Loads objects into a Collection.  Automatically picks up keywords from
+       defined item types.
+       */
+       class Loader: public DataFile::Loader
+       {
+               template<typename T> friend class CollectionItemType;
 
-private:
-       /* XXX I don't really like sticking all this stuff in here, but there's some
-          complex inter-class relationships, especially between ItemKeyword and
-          Collection::Loader. */
+       private:
+               Collection &coll;
 
-       struct ItemBase
-       {
-               virtual ~ItemBase() { }
+       public:
+               Loader(Collection &);
+               Collection &get_object() const { return coll; }
+       private:
+               template<typename T, typename S>
+               void item(const std::string &n)
+               {
+                       RefPtr<T> it = new T;
+                       ItemLoader<T> ldr(*it, coll);
+                       load_sub_with(ldr);
+                       coll.add<S>(n, it.get());
+                       it.release();
+               }
        };
 
+       template<typename T, bool = NeedsCollection<typename T::Loader>::value>
+       class ItemLoader;
+
+private:
+       typedef std::map<std::string, Variant> ItemMap;
+       typedef std::list<CollectionItemTypeBase *> TypeList;
+       typedef std::list<const CollectionSource *> SourceList;
+
+       TypeList types;
+       ItemMap items;
+       SourceList sources;
+       Collection *fallback;
+
+       Collection(const Collection &);
+       Collection &operator=(const Collection &);
+public:
+       Collection();
+       virtual ~Collection();
+
+       /** Adds an object into the collection.  The name must not pre-exist.  The
+       collection takes ownership of the object. */
        template<typename T>
-       struct Item: public ItemBase
+       void add(const std::string &name, T *item)
        {
-               T *data;
+               if(!item)
+                       throw std::invalid_argument("Collection::add(item)");
 
-               Item(T *d): data(d) { }
-               ~Item() { delete data; }
-       };
+               RefPtr<typename RemoveConst<T>::Type> ptr(item);
+               try
+               {
+                       insert_unique(items, name, ptr);
+               }
+               catch(...)
+               {
+                       // Avoid deleting the object
+                       ptr.release();
+                       throw;
+               }
+       }
 
-       /**
-       Used to store keywords for types that can be loaded.
-       */
-       struct ItemKeywordBase
+       /// Gets a typed object from the collection.
+       template<typename T>
+       T &get(const std::string &name) const
        {
-               virtual void add_to_loader(Loader &) const { };
-       };
+               return extract<typename RemoveConst<T>::Type>(get_item(items, name));
+       }
 
-       template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
-       struct ItemKeyword: public ItemKeywordBase
+       /** 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 &name)
        {
-               std::string keyword;
+               typedef typename RemoveConst<T>::Type NCT;
+               return extract<NCT>(get_var(name, get_type<NCT>()));
+       }
 
-               ItemKeyword(const std::string &kw): keyword(kw) { }
+private:
+       const Variant &get_var(const std::string &, const CollectionItemTypeBase *);
 
-               void add_to_loader(Loader &ldr) const
-               { ldr.add(keyword, &Loader::item<T, S>); }
-       };
+       template<typename T>
+       T &extract(const Variant &var) const;
 
-       template<typename T, typename S>
-       struct ItemKeyword<T, S, true>: public ItemKeywordBase
+       template<typename T>
+       std::list<T *> extract_list(const std::list<const Variant *> &vars) const
        {
-               std::string keyword;
+               std::list<T *> result;
+               for(std::list<const Variant *>::const_iterator i=vars.begin(); i!=vars.end(); ++i)
+                       result.push_back(&extract<T>(**i));
+               return result;
+       }
 
-               ItemKeyword(const std::string &kw): keyword(kw) { }
+       void gather_items(std::list<const Variant *> *, std::list<std::string> *, const CollectionItemTypeBase &, bool) const;
 
-               virtual void add_to_loader(Loader &ldr) const
-               { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
-       };
+       template<typename T>
+       void gather_items(std::list<const Variant *> *vars, std::list<std::string> *names, const CollectionItemTypeBase *type, bool include_sources) const
+       {
+               if(type || (type = get_type<T>()))
+                       gather_items(vars, names, *type, include_sources);
+               else
+                       gather_items(vars, names, CollectionItemType<T>(), false);
+       }
 
-       /**
-       Used to store types that can be created automatically.
-       */
-       struct ItemCreatorBase
+public:
+       /// Returns a list of the names of objects of one type in the collection.
+       template<typename T>
+       std::list<std::string> get_names() const
        {
-               virtual ~ItemCreatorBase() { }
+               std::list<std::string> names;
+               gather_items<typename RemoveConst<T>::Type>(0, &names, 0, false);
+               return names;
+       }
 
-               template<typename S>
-               bool create(Collection &coll, const std::string &name, S *&ptr)
-               {
-                       ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
-                       if(creator)
-                       {
-                               creator->create(coll, name, ptr);
-                               return true;
-                       }
-                       return false;
-               }
-       };
+       /** Returns a list of the names of objects of one type in the collection or
+       available from sources. */
+       template<typename T>
+       std::list<std::string> get_names()
+       {
+               std::list<std::string> names;
+               gather_items<typename RemoveConst<T>::Type>(0, &names, 0, true);
+               return names;
+       }
 
-       template<typename S>
-       struct ItemCreatorBridge: public ItemCreatorBase
+       /// Returns a list of objects of one type in the collection.
+       template<typename T>
+       std::list<T *> get_list() const
        {
-               virtual bool create(Collection &, const std::string &, S *&) const =0;
-       };
+               std::list<const Variant *> vars;
+               gather_items<typename RemoveConst<T>::Type>(&vars, 0, 0, false);
+               return extract_list<T>(vars);
+       }
 
-       template<typename T, typename S, typename C>
-       struct ItemCreator: public ItemCreatorBridge<S>
+       /** Returns a list of objects of one type, loading them from sources if
+       necessary. */
+       template<typename T>
+       std::list<T *> get_list()
        {
-               typedef T *(C::*fCreate)(const std::string &);
+               CollectionItemTypeBase *type = get_type<typename RemoveConst<T>::Type>();
+               if(type)
+                       load_items_from_sources(*type);
 
-               fCreate create_func;
+               std::list<const Variant *> vars;
+               gather_items<typename RemoveConst<T>::Type>(&vars, 0, type, true);
+               return extract_list<T>(vars);
+       }
 
-               ItemCreator(fCreate cf): create_func(cf) { }
-               virtual S *create(Collection &coll, const std::string &name) const
-               { return (dynamic_cast<C &>(coll).*create_func)(name); }
-       };
+private:
+       unsigned get_status(const std::string &, const CollectionItemTypeBase &) const;
+
+       template<typename T>
+       unsigned get_status(const std::string &name) const
+       {
+               // XXX Should go through all applicable types
+               if(CollectionItemTypeBase *type = get_type<T>())
+                       return get_status(name, *type);
+
+               ItemMap::const_iterator i = items.find(name);
+               return (i!=items.end() && i->second.check_type<RefPtr<T> >());
+       }
 
 public:
-       /**
-       Loads objects into a Collection.
-       */
-       class Loader: public DataFile::Loader
+       /// Checks whether a typed object exists in the collection.
+       template<typename T>
+       bool contains(const std::string &name) const
+       { return get_status<typename RemoveConst<T>::Type>(name)==1; }
+
+       /** Checks whether a typed object exists in the collection or is loadable
+       from a source. */
+       template<typename T>
+       bool contains(const std::string &name)
+       { return get_status<typename RemoveConst<T>::Type>(name)>0; }
+
+       /// Returns the name of an item in the collection.
+       template<typename T>
+       const std::string &get_name(T *d) const
        {
-       private:
-               Collection &coll;
+               typedef RefPtr<typename RemoveConst<T>::Type> RPNCT;
 
-       public:
-               Loader(Collection &);
-               Collection &get_object() const { return coll; }
-       private:
-               template<typename T, typename S, typename C>
-               void coll_item(const std::string &n)
-               {
-                       RefPtr<T> it=new T;
-                       load_sub(*it, dynamic_cast<C &>(coll));
-                       coll.add<S>(n, it.get());
-                       it.release();
-               }
+               for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
+                       if(i->second.check_type<RPNCT>())
+                               if(i->second.value<RPNCT>().get()==d)
+                                       return i->first;
+       
+               // XXX Need better exception class
+               throw std::runtime_error("Item not found in collection");
+       }
 
-               template<typename T, typename S>
-               void item(const std::string &n)
-               {
-                       RefPtr<T> it=new T;
-                       load_sub(*it);
-                       coll.add<S>(n, it.get());
-                       it.release();
-               }
+protected:
+       /** Adds a type to the collection.  The returned descriptor object reference
+       can be used to define how objects of that type can be loaded. */
+       template<typename T>
+       CollectionItemType<T> &add_type();
 
-               template<typename, typename, bool> friend class ItemKeyword;
-       };
+private:
+       /// Returns the descriptor for a type, or null if one isn't defined.
+       template<typename T>
+       CollectionItemTypeBase *get_type() const;
+
+       /// Gets a descriptor with the same type as another descriptor.
+       CollectionItemTypeBase *get_type(const CollectionItemTypeBase &) const;
+
+       /// Returns the descriptor for an item, or null if it's of an unknown type.
+       CollectionItemTypeBase *get_type_for_item(const Variant &) const;
+
+protected:
+       /** Adds a source for automatically loading items.  Sources are consulted
+       in the order they are added. */
+       void add_source(const CollectionSource &);
+
+public:
+       /** Opens a raw resource, without interpreting it as object data.  Null is
+       returned if no such file is found.  The caller must dispose of the returned
+       object when done with it. */
+       IO::Seekable *open_raw(const std::string &) const;
+
+protected:
+       IO::Seekable *open_from_sources(const std::string &n) { return open_raw(n); }
 
 private:
-       typedef std::map<std::string, ItemBase *> ItemMap;
-       typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
-       typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
+       void gather_names_from_sources(std::list<std::string> &, const CollectionItemTypeBase &) const;
 
-       ItemMap items;
-       ItemKeywordSeq keywords;
-       ItemCreatorSeq creators;
+       void load_items_from_sources(const CollectionItemTypeBase &);
 
-       Collection(const Collection &);
-       Collection &operator=(const Collection &);
+protected:
+       /** Sets a fallback collection, which will be consulted if an item is not
+       found. */
+       void set_fallback(Collection *);
+
+       Collection *get_fallback() const { return fallback; }
+};
+
+template<typename T>
+class Collection::ItemLoader<T, false>: public T::Loader
+{
 public:
-       Collection() { }
-       virtual ~Collection();
+       ItemLoader(T &o, Collection &):
+               T::Loader(o)
+       { }
+};
+
+template<typename T>
+class Collection::ItemLoader<T, true>: public T::Loader
+{
+public:
+       ItemLoader(T &o, Collection &c):
+               T::Loader(o, dynamic_cast<typename T::Loader::Collection &>(c))
+       { }
+};
+
+
+class CollectionItemTypeBase
+{
+protected:
+       struct ExtractorBase
+       {
+               virtual ~ExtractorBase() { }
+       };
 
-       /**
-       Adds an object into the collection.  If a name collision occurs, an
-       exception is thrown.  The collection takes ownership of the object.
-       */
        template<typename T>
-       void add(const std::string &name, T *d)
+       struct Extractor: ExtractorBase
        {
-               if(items.count(name))
-                       throw KeyError("Duplicate key '"+name+"' in collection");
+               virtual T &extract(const Variant &) const = 0;
+       };
 
-               items[name]=new Item<typename RemoveConst<T>::Type>(d);
+       std::string kwd;
+       std::vector<std::string> suffixes;
+       std::vector<ExtractorBase *> extractors;
+
+       CollectionItemTypeBase() { }
+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 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;
+       virtual void create_item(Collection &, const std::string &) const = 0;
+       virtual void load_item(Collection &, Parser &, const std::string &) const = 0;
+
+       template<typename T>
+       bool can_extract() const
+       {
+               for(std::vector<ExtractorBase *>::const_iterator i=extractors.begin(); i!=extractors.end(); ++i)
+                       if(dynamic_cast<Extractor<T> *>(*i))
+                               return true;
+               return false;
        }
 
-       /**
-       Gets an object of a specific type from the collection.
-       */
        template<typename T>
-       T *get(const std::string &name) const
+       T *extract(const Variant &var) const
        {
-               typedef typename RemoveConst<T>::Type NCT;
+               for(std::vector<ExtractorBase *>::const_iterator i=extractors.begin(); i!=extractors.end(); ++i)
+                       if(Extractor<T> *ex = dynamic_cast<Extractor<T> *>(*i))
+                               return &ex->extract(var);
+               return 0;
+       }
+};
 
-               ItemMap::const_iterator i=items.find(name);
-               if(i==items.end())
-                       throw KeyError("Item '"+name+"' not found in collection");
 
-               const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
-               if(!item)
-                       throw TypeError("Item '"+name+"' is not of correct type");
+/**
+Describes a type of item that can be loaded by a Collection.  These are created
+by Collection::add_type.
+*/
+template<typename T>
+class CollectionItemType: public CollectionItemTypeBase
+{
+private:
+       struct CreatorBase
+       {
+               virtual ~CreatorBase() { }
 
-               return *item->data;
-       }
+               virtual T *create(Collection &, const std::string &) const = 0;
+       };
 
-       /**
-       Gets an object of a specific type from the collection.  If the name is not
-       found in the collection and there is a creator for the item type, it is
-       invoked.
-       */
-       template<typename T>
-       T *get(const std::string &name)
+       template<typename C>
+       struct Creator: CreatorBase
        {
-               typedef typename RemoveConst<T>::Type NCT;
+               typedef T *(C::*FuncPtr)(const std::string &);
 
-               ItemMap::const_iterator i=items.find(name);
-               if(i==items.end())
-               {
-                       for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
-                       {
-                               NCT *d=0;
-                               if((*j)->create(*this, name, d))
-                               {
-                                       // We already know that the item didn't exist yet
-                                       items[name]=new Item<NCT>(d);
-                                       return *d;
-                               }
-                       }
-                       throw KeyError("Item '"+name+"' not found in collection");
-               }
+               FuncPtr func;
 
-               const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
-               if(!item)
-                       throw TypeError("Item '"+name+"' is not of correct type");
+               Creator(FuncPtr f): func(f) { }
 
-               return *item->data;
+               virtual T *create(Collection &coll, const std::string &name) const
+               { return (dynamic_cast<C &>(coll).*func)(name); }
+       };
+
+       template<typename B>
+       struct Extractor: CollectionItemTypeBase::Extractor<B>
+       {
+               virtual B &extract(const Variant &var) const
+               { return *var.value<RefPtr<T> >(); }
+       };
+
+       CreatorBase *creat;
+
+public:
+       CollectionItemType():
+               creat(0)
+       { }
+
+       ~CollectionItemType()
+       {
+               delete creat;
        }
 
-       /**
-       Returns a list of the names of objects of a specific type in the collection.
-       */
-       template<typename T>
-       std::list<std::string> get_names() const
+       /** Sets a datafile keyword for this item type.  The Collection's loader
+       will accept a statement with this keyword and a single string argument - the
+       item's name. */
+       CollectionItemType &keyword(const std::string &k)
        {
-               std::list<std::string> result;
-               for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
-                       if(dynamic_cast<const Item<typename RemoveConst<T>::Type> *>(i->second))
-                               result.push_back(i->first);
-               return result;
+               set_keyword(k);
+               return *this;
        }
 
-       /**
-       Returns a list of objects of a specific type in the collection.
-       */
-       template<typename T>
-       std::list<T *> get_list() const
+       /** Adds a suffix that is used to match names when looking for future
+       objects.  There is no implied separator; a name matches if it ends with the
+       suffix.  If a keyword is defined before any suffixes, then "."+keyword is
+       added as a suffix. */
+       CollectionItemType &suffix(const std::string &s)
        {
-               typedef typename RemoveConst<T>::Type NCT;
+               add_suffix(s);
+               return *this;
+       }
 
-               std::list<T *> result;
-               for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
-                       if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
-                               result.push_back(item->data);
-               return result;
+       /** Attaches a creator function to this item type.  If an item is not found
+       in the Collection, the creator function for its type is called to create it.
+       The function must be a member of the Collection subclass containing the
+       type.  It must return the created object, or null if it could not be
+       created.  It's also permissible to load the item via other means and then
+       return null. */
+       template<typename C>
+       CollectionItemType &creator(T *(C::*func)(const std::string &))
+       {
+               delete creat;
+               creat = new Creator<C>(func);
+               return *this;
        }
 
-       /**
-       Checks whether a name exists in the collection.  Does not care about the
-       type of the object.
-       */
-       bool contains(const std::string &n) const;
+       /** Makes items of this type available through a base class. */
+       template<typename B>
+       CollectionItemType &base()
+       {
+               extractors.push_back(new Extractor<B>);
+               return *this;
+       }
 
-protected:
-       /**
-       Adds a type that can be loaded from datafiles.
-       */
-       template<typename T>
-       void add_keyword(const std::string &keyword)
-       { add_keyword<T, T>(keyword); }
+       virtual bool is_same_type(const CollectionItemTypeBase &other) const
+       { return dynamic_cast<const CollectionItemType<T> *>(&other); }
 
-       /**
-       Adds a type that can be loaded from datafiles, with different storage type.
-       */
-       template<typename T, typename S>
-       void add_keyword(const std::string &keyword)
-       { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
+       virtual bool check_item_type(const Variant &var) const
+       { return var.check_type<RefPtr<T> >(); }
 
-       /**
-       Adds a type that can be created automatically.
-       */
-       template<typename T, typename C>
-       void add_creator(T *(C::*func)(const std::string &))
-       { add_creator<T, T, C>(func); }
+       virtual void add_to_loader(Collection::Loader &loader) const
+       { loader.add(kwd, &Collection::Loader::item<T, T>); }
 
-       template<typename T, typename S, typename C>
-       void add_creator(T *(C::*func)(const std::string &))
-       { creators.push_back(new ItemCreator<T, S, C>(func)); }
+       virtual bool can_create() const
+       { return creat!=0; }
+
+       virtual void create_item(Collection &coll, const std::string &name) const
+       {
+               if(!creat)
+                       throw std::runtime_error("no creator");
+               T *obj = creat->create(coll, name);
+               if(obj)
+                       coll.add(name, obj);
+       }
+
+       virtual void load_item(Collection &coll, Parser &parser, const std::string &name) const
+       {
+               RefPtr<T> obj = new T;
+               Collection::ItemLoader<T> ldr(*obj, coll);
+               ldr.load(parser);
+               coll.add(name, obj.get());
+               obj.release();
+       }
 };
 
+
+template<typename T>
+T &Collection::extract(const Variant &var) const
+{
+       if(!var.check_type<RefPtr<T> >())
+               if(CollectionItemTypeBase *type = get_type_for_item(var))
+                       if(T *item = type->extract<T>(var))
+                               return *item;
+
+       return *var.value<RefPtr<T> >();
+}
+
+template<typename T>
+CollectionItemType<T> &Collection::add_type()
+{
+       CollectionItemType<T> *type = new CollectionItemType<T>;
+       types.push_back(type);
+       return *type;
+}
+
+template<typename T>
+CollectionItemTypeBase *Collection::get_type() const
+{
+       for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+               if(dynamic_cast<CollectionItemType<T> *>(*j))
+                       return *j;
+       for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+               if((*j)->can_extract<T>())
+                       return *j;
+       return 0;
+}
+
 } // namespace DataFile
 } // namespace Msp