-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpack_graph.h
180 lines (156 loc) · 4.47 KB
/
pack_graph.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/**\file
\brief serialisation for dynamic structures (graphs/trees and so on)
*/
#ifndef CLASSDESC_PACK_GRAPH_H
#define CLASSDESC_PACK_GRAPH_H
#include <vector>
#include <map>
#include <pack_base.h>
/*
treenodes should really be deprecated in favour of graphnodes
*/
namespace classdesc
{
///serialise a tree (or DAG)
template <class T>
void pack(pack_t& targ, const string& desc, is_treenode dum, const T* const& arg)
{
int valid=0;
if (arg==NULL)
pack(targ,desc,valid);
else
{
valid=1;
pack(targ,desc,valid);
pack(targ,desc,*arg);
}
}
///unserialise a tree.
/** Serialised DAGs will be converted to a tree*/
template <class T>
void unpack(unpack_t& targ, const string& desc, is_treenode dum, T*& arg)
{
int valid;
unpack(targ,desc,valid);
if (valid)
{
// arg=new T;
// targ->alloced.push_back(arg);
targ.alloced.push_back(new classdesc::PtrStore<T>);
arg=static_cast<T*>(targ.alloced.back().data());
unpack(targ,desc,*arg);
}
}
/*
pack up a graph (possibly containing cycles).
T must have the following members:
V& operator*() - dereference operator
operator bool() - returns true if valid, false otherwise
T& operator=(const T&) - assignable x=y => &*x==&*y
T() - construct an invalid object
T(const T&) - copyable
Alloc<T>(T& x) - allocates a new object referenced by x
V must be serialisable.
*/
/** for non pointer types - eg smart pointers, an Alloc specialisation needs to be provided */
template <class T> struct Alloc;
template <class T>
struct Alloc<T*>
{
void operator()(pack_t& buf, T*& x) {
buf.alloced.push_back(new PtrStore<T>);
x=static_cast<T*>(buf.alloced.back().data());
}
};
template <class T>
inline void pack_graph(pack_t& buf, T& arg)
{
static std::map<T,int> graph_map;
static std::vector<T*> restart;
static unsigned recur_level=0;
int nought=0;
if (recur_level==0)
pack(buf,string(),buf.recur_max);
recur_level++;
if (recur_level==buf.recur_max)
restart.push_back(&arg); //save ref for restart
else if (!arg) //reference is invalid
pack(buf,string(),nought);
else if (graph_map.count(arg)==0)
{
int ID=graph_map.size()+1;
graph_map[arg]=ID;
pack(buf,string(),ID);
pack(buf,string(),*arg);
}
else
pack(buf,string(),graph_map[arg]);
recur_level--;
if (recur_level==0) //process restarts
{
while (restart.size())
{
T& aa=*restart.back(); //we need to pop_back() before packing
restart.pop_back(); //in case further restarts are needed
pack_graph(buf,aa);
}
graph_map.clear();
}
}
template <class T>
void unpack_graph(pack_t& buf, T& arg)
{
static std::map<int,T> graph_map;
static std::vector<T*> restart;
static unsigned recur_level=0;
int myid;
Alloc<T> alloc;
if (recur_level==0)
unpack(buf,string(),buf.recur_max);
recur_level++;
if (recur_level==buf.recur_max)
restart.push_back(&arg); //save ref for restart
else
{
unpack(buf,string(),myid);
if (myid==0)
arg=T(); //reset arg to invalid state
else if (graph_map.count(myid))
arg=graph_map[myid];
else
{
alloc(buf,arg);
graph_map[myid]=arg;
unpack(buf,string(),*arg);
}
}
recur_level--;
if (recur_level==0) //process restarts
{
while (restart.size())
{
T& aa=*restart.back(); //we need to pop_back() before packing
restart.pop_back(); //in case further restarts are needed
unpack_graph(buf,aa);
}
graph_map.clear();
}
}
///serialise a graph structure
/** can contain cycles */
template <class T>
inline void pack(pack_t& targ, const string& desc,is_graphnode dum,const T& arg)
{pack_graph(targ,arg);}
///unserialise a graph structure
/** can contain cycles */
template <class T>
inline void unpack(pack_t& targ, const string& desc,is_graphnode dum,T& arg)
{unpack_graph(targ,arg);}
}
#endif