summaryrefslogtreecommitdiff
path: root/project2/common/xmlStorage.h
blob: 267343f2adae669ed6f2183cab8878660d100c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef XMLSTORAGE_H
#define XMLSTORAGE_H

#include "sourceObject.h"
#include "xmlObjectLoader.h"
#include "exceptions.h"
#include "scripts.h"
#include <set>
#include <list>
#include <map>
#include <boost/intrusive_ptr.hpp>

SimpleMessageException(StoreFailed);

#define SINGLE(X) \
	boost::intrusive_ptr<X>
#define STORAGEOF(X) \
	std::map<std::string, boost::intrusive_ptr<X> >
#define ANONORDEREDSTORAGEOF(X) \
	std::list<boost::intrusive_ptr<X> >
#define ANONSTORAGEOF(X) \
	std::set<boost::intrusive_ptr<X> >

class Storer;
typedef boost::intrusive_ptr<Storer> StorerPtr;
class Storer : public virtual IntrusivePtrBase {
	public:
		template <class L, class X>
		static StorerPtr into(SINGLE(X) * obj);
		template <class L, class X>
		static StorerPtr into(STORAGEOF(X) * map);
		template <class L, class X>
		static StorerPtr into(ANONSTORAGEOF(X) * set);
		template <class L, class X>
		static StorerPtr into(ANONORDEREDSTORAGEOF(X) * list);

		virtual boost::intrusive_ptr<IntrusivePtrBase> create(ScriptNodePtr) const = 0;
		virtual bool save(boost::intrusive_ptr<IntrusivePtrBase>, ScriptNodePtr) = 0;
};

template <class X, class M, class L>
class StorerBase : public Storer {
	public:
		typedef M * Map;
		boost::intrusive_ptr<IntrusivePtrBase> create(ScriptNodePtr p) const {
			return LoaderBase::getLoader<L, NotSupported>(p->get_name())->createFrom(p);
		}
		bool save(boost::intrusive_ptr<IntrusivePtrBase> o, ScriptNodePtr p) {
			boost::intrusive_ptr<X> O = boost::dynamic_pointer_cast<X>(o);
			if (O) {
				if (!insert(p, O)) {
					throw StoreFailed(p->get_name());
				}
			}
			return O;
		}
		virtual bool insert(ScriptNodePtr, boost::intrusive_ptr<X>) = 0;
};

template <class X, class M, class L>
class StorerImpl : public StorerBase<X, M, L> {
	public:
		StorerImpl(M * m);
		bool insert(ScriptNodePtr, boost::intrusive_ptr<X> O);
};
template <class X, class L>
class StorerImpl<X, SINGLE(X), L> : public StorerBase<X, SINGLE(X), L> {
	public:
		typedef SINGLE(X) Obj;
		StorerImpl(SINGLE(X) * o) : obj(o)
		{
		}
		bool insert(ScriptNodePtr, boost::intrusive_ptr<X> O)
		{
			*obj = O;
			return true;
		}
		Obj * obj;
};
template <class X, class L>
class StorerImpl<X, STORAGEOF(X), L> : public StorerBase<X, STORAGEOF(X), L> {
	public:
		typedef STORAGEOF(X) Map;
		StorerImpl(STORAGEOF(X) * m) : map(m)
		{
		}
		bool insert(ScriptNodePtr, boost::intrusive_ptr<X> O)
		{
			return map->insert(typename Map::value_type(O->name, O)).second;
		}
		Map * map;
};
template <class X, class L>
class StorerImpl<X, ANONSTORAGEOF(X), L> : public StorerBase<X, ANONSTORAGEOF(X), L> {
	public:
		typedef ANONSTORAGEOF(X) Map;
		StorerImpl(ANONSTORAGEOF(X) * m) : map(m)
		{
		}
		bool insert(ScriptNodePtr, boost::intrusive_ptr<X> O)
		{
			map->insert(O);
			return true;
		}
		Map * map;
};
template <class X, class L>
class StorerImpl<X, ANONORDEREDSTORAGEOF(X), L> : public StorerBase<X, ANONORDEREDSTORAGEOF(X), L> {
	public:
		typedef ANONORDEREDSTORAGEOF(X) Map;
		StorerImpl(ANONORDEREDSTORAGEOF(X) * m) : map(m)
		{
		}
		bool insert(ScriptNodePtr, boost::intrusive_ptr<X> O)
		{
			map->push_back(O);
			return true;
		}
		Map * map;
};

template <class L, class X>
StorerPtr
Storer::into(SINGLE(X) * obj) {
	return new StorerImpl<X, SINGLE(X), L>(obj);
}

template <class L, class X>
StorerPtr
Storer::into(STORAGEOF(X) * map) {
	return new StorerImpl<X, STORAGEOF(X), L>(map);
}

template <class L, class X>
StorerPtr
Storer::into(ANONSTORAGEOF(X) * set) {
	return new StorerImpl<X, ANONSTORAGEOF(X), L>(set);
}

template <class L, class X>
StorerPtr
Storer::into(ANONORDEREDSTORAGEOF(X) * list) {
	return new StorerImpl<X, ANONORDEREDSTORAGEOF(X), L>(list);
}

#endif