]> git.tdb.fi Git - libs/datafile.git/blob - source/dynamicobjectloader.h
Use nullptr instead of 0 for pointers
[libs/datafile.git] / source / dynamicobjectloader.h
1 #ifndef MSP_DATAFILE_DYNAMICOBJECTLOADER_H_
2 #define MSP_DATAFILE_DYNAMICOBJECTLOADER_H_
3
4 #include <msp/core/typeregistry.h>
5 #include "collection.h"
6 #include "except.h"
7 #include "loader.h"
8
9 namespace Msp {
10 namespace DataFile {
11
12 class Collection;
13
14 /**
15 Loads a dynamically typed object, with the type specified as a statement.  The
16 T template parameter is the common base class for all possible loaded types.
17 */
18 template<typename T, typename C = Collection>
19 class DynamicObjectLoader: public Loader
20 {
21 public:
22         typedef C Collection;
23
24 protected:
25         template<typename U>
26         struct CreateObject
27         {
28                 void operator()(const std::string &, DynamicObjectLoader &) const;
29         };
30
31         typedef Msp::TypeRegistry<CreateObject, DynamicObjectLoader &> TypeRegistry;
32
33         Collection *coll;
34         T *object;
35 private:
36         Loader *obj_loader;
37         std::function<void(Collection &, const std::string &, T *)> store_func;
38
39         static ActionMap shared_actions;
40
41 protected:
42         DynamicObjectLoader(Collection *);
43 public:
44         ~DynamicObjectLoader() { delete object; delete obj_loader; }
45
46 private:
47         virtual void init_actions();
48
49 public:
50         T *get_object() { T *o = object; object = 0; return o; }
51         T *store_object(Collection &, const std::string &);
52
53 protected:
54         virtual void type(const Symbol &);
55
56 private:
57         template<typename U>
58         typename std::enable_if<NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type create_object_loader(U &obj) const;
59
60         template<typename U>
61         typename std::enable_if<!NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type create_object_loader(U &obj) const;
62
63 protected:
64         virtual const TypeRegistry &get_type_registry() const = 0;
65 };
66
67
68 template<typename T, typename C>
69 Loader::ActionMap DynamicObjectLoader<T, C>::shared_actions;
70
71 template<typename T, typename C>
72 DynamicObjectLoader<T, C>::DynamicObjectLoader(Collection *c):
73         coll(c),
74         object(nullptr),
75         obj_loader(nullptr)
76 {
77         set_actions(shared_actions);
78 }
79
80 template<typename T, typename C>
81 void DynamicObjectLoader<T, C>::init_actions()
82 {
83         add("type", &DynamicObjectLoader::type);
84 }
85
86 template<typename T, typename C>
87 T *DynamicObjectLoader<T, C>::store_object(Collection &c, const std::string &name)
88 {
89         if(!store_func)
90                 throw std::logic_error("no store function");
91
92         T *o = object;
93         store_func(c, name, object);
94         object = 0;
95         return o;
96 }
97
98 template<typename T, typename C>
99 void DynamicObjectLoader<T, C>::type(const Symbol &t)
100 {
101         if(obj_loader)
102                 throw std::logic_error("duplicate type statement");
103
104         get_type_registry().invoke(t.name, *this);
105 }
106
107 template<typename T, typename C>
108 template<typename U>
109 typename std::enable_if<NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
110 {
111         if(!coll)
112                 throw no_collection(typeid(U));
113         return new typename U::Loader(obj, *coll);
114 }
115
116 template<typename T, typename C>
117 template<typename U>
118 typename std::enable_if<!NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
119 {
120         return new typename U::Loader(obj);
121 }
122
123
124 template<typename T, typename C>
125 template<typename U>
126 void DynamicObjectLoader<T, C>::CreateObject<U>::operator()(const std::string &, DynamicObjectLoader &ldr) const
127 {
128         U *obj = new U;
129         ldr.object = obj;
130         ldr.obj_loader = ldr.create_object_loader<U>(*obj);
131         ldr.add_auxiliary_loader(*ldr.obj_loader);
132         ldr.store_func = [](Collection &c, const std::string &n, T *o){ c.add(n, static_cast<U *>(o)); };
133 }
134
135 } // namespace DataFile
136 } // namespace Msp
137
138 #endif