]> git.tdb.fi Git - libs/datafile.git/blob - source/directorycollection.h
93f93545b589708f312fce6d87c1330de8f79ac3
[libs/datafile.git] / source / directorycollection.h
1 #ifndef MSP_DATAFILE_DIRECTORYCOLLECTION_H_
2 #define MSP_DATAFILE_DIRECTORYCOLLECTION_H_
3
4 #include <msp/fs/path.h>
5 #include "collection.h"
6
7 namespace Msp {
8 namespace DataFile {
9
10 /**
11 A Collection that can automatically load items from files in a directory.
12 */
13 class DirectoryCollection: public Collection
14 {
15 private:
16         std::list<FS::Path> dirs;
17
18 public:
19         DirectoryCollection();
20
21 protected:
22         void set_directory(const FS::Path &);
23         void add_directory(const FS::Path &);
24
25         template<typename T>
26         CollectionItemType<T> &add_type()
27         {
28                 return Collection::add_type<T>().creator(&DirectoryCollection::create<T>);
29         }
30
31 private:
32         template<typename T>
33         T *create(const std::string &name)
34         {
35                 FS::Path file;
36                 if(lookup_file(name, file))
37                 {
38                         RefPtr<T> item = new T;
39                         load(*item, file.str());
40                         return item.release();
41                 }
42                 else
43                         return 0;
44         }
45
46         bool lookup_file(const std::string &, FS::Path &) const;
47 };
48
49 } // namespace DataFile
50 } // namespace Msp
51
52 #endif