]> git.tdb.fi Git - libs/datafile.git/blob - source/directorycollection.h
Add a test case for comments
[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         /** Examines the names of files in the designated directories and adds any
26         applicable ones as future objects. */
27         void load_names();
28
29         template<typename T>
30         CollectionItemType<T> &add_type()
31         {
32                 return Collection::add_type<T>().creator(&DirectoryCollection::create<T>);
33         }
34
35 private:
36         template<typename T>
37         T *create(const std::string &name)
38         {
39                 FS::Path file;
40                 if(lookup_file(name, file))
41                 {
42                         RefPtr<T> item = new T;
43                         ItemLoader<T> ldr(*item, *this);
44                         IO::BufferedFile in(file.str());
45                         Parser parser(in, file.str());
46                         ldr.load(parser);
47                         return item.release();
48                 }
49                 else
50                         return 0;
51         }
52
53 protected:
54         bool lookup_file(const std::string &, FS::Path &) const;
55 };
56
57 } // namespace DataFile
58 } // namespace Msp
59
60 #endif