]> git.tdb.fi Git - libs/datafile.git/blob - source/dynamicobjectloader.h
Use the override specifier to mark overridden virtual functions
[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 = nullptr;
34         T *object = nullptr;
35 private:
36         Loader *obj_loader = nullptr;
37         void (*store_func)(Collection &, const std::string &, T *) = nullptr;
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         void init_actions() override;
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 {
75         set_actions(shared_actions);
76 }
77
78 template<typename T, typename C>
79 void DynamicObjectLoader<T, C>::init_actions()
80 {
81         add("type", &DynamicObjectLoader::type);
82 }
83
84 template<typename T, typename C>
85 T *DynamicObjectLoader<T, C>::store_object(Collection &c, const std::string &name)
86 {
87         if(!store_func)
88                 throw std::logic_error("no store function");
89
90         T *o = object;
91         store_func(c, name, object);
92         object = 0;
93         return o;
94 }
95
96 template<typename T, typename C>
97 void DynamicObjectLoader<T, C>::type(const Symbol &t)
98 {
99         if(obj_loader)
100                 throw std::logic_error("duplicate type statement");
101
102         get_type_registry().invoke(t.name, *this);
103 }
104
105 template<typename T, typename C>
106 template<typename U>
107 typename std::enable_if<NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
108 {
109         if(!coll)
110                 throw no_collection(typeid(U));
111         return new typename U::Loader(obj, *coll);
112 }
113
114 template<typename T, typename C>
115 template<typename U>
116 typename std::enable_if<!NeedsCollection<typename U::Loader>::value, typename U::Loader *>::type DynamicObjectLoader<T, C>::create_object_loader(U &obj) const
117 {
118         return new typename U::Loader(obj);
119 }
120
121
122 template<typename T, typename C>
123 template<typename U>
124 void DynamicObjectLoader<T, C>::CreateObject<U>::operator()(const std::string &, DynamicObjectLoader &ldr) const
125 {
126         U *obj = new U;
127         ldr.object = obj;
128         ldr.obj_loader = ldr.create_object_loader<U>(*obj);
129         ldr.add_auxiliary_loader(*ldr.obj_loader);
130         ldr.store_func = [](Collection &c, const std::string &n, T *o){ c.add(n, static_cast<U *>(o)); };
131 }
132
133 } // namespace DataFile
134 } // namespace Msp
135
136 #endif