]> git.tdb.fi Git - libs/datafile.git/blob - source/objectloader.h
bab1ce5378f5bce344fd931d80061b6a67af9698
[libs/datafile.git] / source / objectloader.h
1 #ifndef MSP_DATAFILE_OBJECTLOADER_H_
2 #define MSP_DATAFILE_OBJECTLOADER_H_
3
4 #include "loader.h"
5
6 namespace Msp {
7 namespace DataFile {
8
9 class Collection;
10
11 /**
12 Provides the basic functionality of an object loader.  Deriving from this
13 allows loading values directly into member variables of the objects.
14 */
15 template<typename O>
16 class ObjectLoader: public Loader
17 {
18 public:
19         typedef O Object;
20
21 protected:
22         O &obj;
23
24         ObjectLoader(O &o): obj(o) { }
25
26 public:
27         O &get_object() const { return obj; }
28 };
29
30
31 /**
32 Provides functionality for loading objects with a Collection.  Deriving from
33 this allows loading pointers to objects in the collection automatically.
34 */
35 template<typename O, typename C = Collection>
36 class CollectionObjectLoader: public ObjectLoader<O>
37 {
38 public:
39         typedef C Collection;
40
41 protected:
42         C *coll;
43
44         CollectionObjectLoader(O &o, C *c): ObjectLoader<O>(o), coll(c) { }
45
46 public:
47         C &get_collection() const
48         {
49                 if(!coll)
50                         throw InvalidState("No collection");
51                 return *coll;
52         }
53 };
54
55 } // namespace DataFile
56 } // namespace Msp
57
58 #endif