]> git.tdb.fi Git - libs/datafile.git/blob - source/objectloader.h
Cosmetic changes
[libs/datafile.git] / source / objectloader.h
1 #ifndef MSP_DATAFILE_OBJECTLOADER_H_
2 #define MSP_DATAFILE_OBJECTLOADER_H_
3
4 #include "except.h"
5 #include "loader.h"
6
7 namespace Msp {
8 namespace DataFile {
9
10 class Collection;
11
12 /**
13 Provides the basic functionality of an object loader.  Deriving from this
14 allows loading values directly into member variables of the objects.
15 */
16 template<typename O>
17 class ObjectLoader: virtual public Loader
18 {
19 public:
20         typedef O Object;
21
22 protected:
23         O &obj;
24
25         ObjectLoader(O &o): obj(o) { }
26
27 public:
28         O &get_object() const { return obj; }
29 };
30
31
32 /**
33 Convenience class for loading derived objects.  Inherits from the base class
34 loader and shadows its members with ones for the derived type.
35 */
36 template<typename O, typename B>
37 class DerivedObjectLoader: public B
38 {
39 public:
40         typedef O Object;
41
42 protected:
43         O &obj;
44
45         DerivedObjectLoader(O &o): B(o), obj(o) { }
46
47         template<typename T>
48         DerivedObjectLoader(O &o, T &a): B(o, a), obj(o) { }
49
50 public:
51         O &get_object() const { return obj; }
52 };
53
54
55 /**
56 Provides functionality for loading objects with a Collection.  Deriving from
57 this allows loading pointers to objects in the collection automatically.
58 */
59 template<typename O, typename C = Collection>
60 class CollectionObjectLoader: public ObjectLoader<O>
61 {
62 public:
63         typedef C Collection;
64
65 protected:
66         C *coll;
67
68         CollectionObjectLoader(O &o, C *c): ObjectLoader<O>(o), coll(c) { }
69
70 public:
71         C &get_collection() const
72         {
73                 if(!coll)
74                         throw no_collection(typeid(O));
75                 return *coll;
76         }
77 };
78
79 } // namespace DataFile
80 } // namespace Msp
81
82 #endif