]> git.tdb.fi Git - libs/datafile.git/commitdiff
Proper loading of collection-enabled objects in DirectoryCollection
authorMikko Rasa <tdb@tdb.fi>
Sat, 18 Aug 2012 21:16:50 +0000 (00:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 18 Aug 2012 21:34:20 +0000 (00:34 +0300)
source/directorycollection.h

index 54bf62a90079f9dc2f5a365c24ccdfc39c8a34d9..e9a946332c767d0831afced78f4f981c593037cc 100644 (file)
@@ -13,6 +13,9 @@ A Collection that can automatically load items from files in a directory.
 class DirectoryCollection: public Collection
 {
 private:
+       template<typename T, bool = NeedsCollection<typename T::Loader>::value>
+       class ItemLoader;
+
        std::list<FS::Path> dirs;
 
 public:
@@ -36,7 +39,10 @@ private:
                if(lookup_file(name, file))
                {
                        RefPtr<T> item = new T;
-                       load(*item, file.str());
+                       ItemLoader<T> ldr(*item, *this);
+                       IO::BufferedFile in(file.str());
+                       Parser parser(in, file.str());
+                       ldr.load(parser);
                        return item.release();
                }
                else
@@ -47,6 +53,24 @@ protected:
        bool lookup_file(const std::string &, FS::Path &) const;
 };
 
+template<typename T>
+class DirectoryCollection::ItemLoader<T, false>: public T::Loader
+{
+public:
+       ItemLoader(T &o, Collection &):
+               T::Loader(o)
+       { }
+};
+
+template<typename T>
+class DirectoryCollection::ItemLoader<T, true>: public T::Loader
+{
+public:
+       ItemLoader(T &o, Collection &c):
+               T::Loader(o, dynamic_cast<typename T::Loader::Collection &>(c))
+       { }
+};
+
 } // namespace DataFile
 } // namespace Msp