1 #ifndef MSP_DATAFILE_DIRECTORYCOLLECTION_H_
2 #define MSP_DATAFILE_DIRECTORYCOLLECTION_H_
4 #include <msp/fs/path.h>
5 #include "collection.h"
11 A Collection that can automatically load items from files in a directory.
13 class DirectoryCollection: public Collection
16 template<typename T, bool = NeedsCollection<typename T::Loader>::value>
19 std::list<FS::Path> dirs;
22 DirectoryCollection();
25 void set_directory(const FS::Path &);
26 void add_directory(const FS::Path &);
29 CollectionItemType<T> &add_type()
31 return Collection::add_type<T>().creator(&DirectoryCollection::create<T>);
36 T *create(const std::string &name)
39 if(lookup_file(name, file))
41 RefPtr<T> item = new T;
42 ItemLoader<T> ldr(*item, *this);
43 IO::BufferedFile in(file.str());
44 Parser parser(in, file.str());
46 return item.release();
53 bool lookup_file(const std::string &, FS::Path &) const;
57 class DirectoryCollection::ItemLoader<T, false>: public T::Loader
60 ItemLoader(T &o, Collection &):
66 class DirectoryCollection::ItemLoader<T, true>: public T::Loader
69 ItemLoader(T &o, Collection &c):
70 T::Loader(o, dynamic_cast<typename T::Loader::Collection &>(c))
74 } // namespace DataFile