]> git.tdb.fi Git - libs/datafile.git/blob - source/directorycollection.h
Make DirectoryCollection::lookup_file available to derived classes
[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 protected:
47         bool lookup_file(const std::string &, FS::Path &) const;
48 };
49
50 } // namespace DataFile
51 } // namespace Msp
52
53 #endif