summaryrefslogtreecommitdiff
path: root/project2/common/xmlStorage.h
blob: 73fce0a125dae970fbf18c035582410515768bac (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
#ifndef XMLSTORAGE_H
#define XMLSTORAGE_H

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

SimpleMessageException(StoreFailed);

#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 X>
		static StorerPtr into(STORAGEOF(X) * map);
		template <class X>
		static StorerPtr into(ANONSTORAGEOF(X) * set);
		template <class X>
		static StorerPtr into(ANONORDEREDSTORAGEOF(X) * list);

		virtual bool save(SourceObjectPtr o, const xmlpp::Element *) = 0;
};

template <class X, class M = STORAGEOF(X)>
class StorerBase : public Storer {
	public:
		typedef M * Map;
		bool save(SourceObjectPtr obj, const xmlpp::Element * p) {
			boost::intrusive_ptr<X> O = boost::dynamic_pointer_cast<X>(obj);
			if (O) {
				if (insert(p, O)) {
					return true;
				}
				throw StoreFailed(obj->name);
			}
			return false;
		}
		virtual bool insert(const xmlpp::Element *, boost::intrusive_ptr<X>) = 0;
};

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

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

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

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

#endif