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 &);
28 /** Examines the names of files in the designated directories and adds any
29 applicable ones as future objects. */
33 CollectionItemType<T> &add_type()
35 return Collection::add_type<T>().creator(&DirectoryCollection::create<T>);
40 T *create(const std::string &name)
43 if(lookup_file(name, file))
45 RefPtr<T> item = new T;
46 ItemLoader<T> ldr(*item, *this);
47 IO::BufferedFile in(file.str());
48 Parser parser(in, file.str());
50 return item.release();
57 bool lookup_file(const std::string &, FS::Path &) const;
61 class DirectoryCollection::ItemLoader<T, false>: public T::Loader
64 ItemLoader(T &o, Collection &):
70 class DirectoryCollection::ItemLoader<T, true>: public T::Loader
73 ItemLoader(T &o, Collection &c):
74 T::Loader(o, dynamic_cast<typename T::Loader::Collection &>(c))
78 } // namespace DataFile