]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.h
Move NeedsCollection into its own header and adjust for new Sfinae struct
[libs/datafile.git] / source / collection.h
index 50905ce0d14972d65b24ac37dc74cdd6b5e21be9..34b1260c4c42b729311964736b2efc02343c03fb 100644 (file)
@@ -2,10 +2,10 @@
 #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"
+#include "meta.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. */
@@ -13,20 +13,6 @@ dependencies between those things make it difficult to split up. */
 namespace Msp {
 namespace DataFile {
 
-/**
-Helper struct to determine whether a Loader has a Collection typedef.
-*/
-template<typename T>
-struct NeedsCollection: public Sfinae
-{
-       template<typename U>
-       static Yes f(typename U::Collection *);
-       template<typename U>
-       static No f(...);
-
-       enum { value = Evaluate<sizeof(f<T>(0))>::value };
-};
-
 class CollectionItemTypeBase;
 
 template<typename T>
@@ -83,7 +69,7 @@ public:
 private:
        typedef std::map<std::string, Variant> ItemMap;
        typedef std::list<CollectionItemTypeBase *> TypeList;
-       typedef std::list<CollectionSource *> SourceList;
+       typedef std::list<const CollectionSource *> SourceList;
 
        TypeList types;
        ItemMap items;
@@ -131,7 +117,7 @@ public:
        T &get(const std::string &name)
        {
                typedef typename RemoveConst<T>::Type NCT;
-               return extract<NCT>(get_var(name, get_type<NCT>()));
+               return extract<NCT>(get_var(name, get_type<NCT>(name)));
        }
 
 private:
@@ -251,9 +237,10 @@ protected:
        CollectionItemType<T> &add_type();
 
 private:
-       /// Returns the descriptor for a type, or null if one isn't defined.
+       /** Returns the descriptor for a type, or null if one isn't defined.  An
+       optional name can be given to prioritize matching types. */
        template<typename T>
-       CollectionItemTypeBase *get_type() const;
+       CollectionItemTypeBase *get_type(const std::string & = std::string()) const;
 
        /// Gets a descriptor with the same type as another descriptor.
        CollectionItemTypeBase *get_type(const CollectionItemTypeBase &) const;
@@ -264,7 +251,7 @@ private:
 protected:
        /** Adds a source for automatically loading items.  Sources are consulted
        in the order they are added. */
-       void add_source(CollectionSource &);
+       void add_source(const CollectionSource &);
 
 public:
        /** Opens a raw resource, without interpreting it as object data.  Null is
@@ -284,6 +271,8 @@ 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>
@@ -498,15 +487,20 @@ CollectionItemType<T> &Collection::add_type()
 }
 
 template<typename T>
-CollectionItemTypeBase *Collection::get_type() const
+CollectionItemTypeBase *Collection::get_type(const std::string &name) const
 {
        for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
                if(dynamic_cast<CollectionItemType<T> *>(*j))
                        return *j;
+       CollectionItemTypeBase *type = 0;
        for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
                if((*j)->can_extract<T>())
-                       return *j;
-       return 0;
+               {
+                       if(!name.empty() && (*j)->match_name(name))
+                               return *j;
+                       type = *j;
+               }
+       return type;
 }
 
 } // namespace DataFile