]> Dogcows Code - chaz/yoink/blob - src/stlplus/persistence/persistent_digraph.tpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / persistence / persistent_digraph.tpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "persistent_int.hpp"
10 #include "persistent_xref.hpp"
11
12 namespace stlplus
13 {
14 ////////////////////////////////////////////////////////////////////////////////
15
16 template<typename NT, typename AT, typename DN, typename DA>
17 void dump_digraph(dump_context& context, const digraph<NT,AT>& data,
18 DN dump_node, DA dump_arc)
19 throw(persistent_dump_failed)
20 {
21 // dump a magic key to the address of the graph for use in persistence of iterators
22 // and register it as a dumped address
23 std::pair<bool,unsigned> mapping = context.pointer_map(&data);
24 if (mapping.first) throw persistent_dump_failed("digraph: already dumped this graph");
25 dump_unsigned(context,mapping.second);
26 // dump the nodes
27 dump_unsigned(context,data.size());
28 for (typename digraph<NT,AT>::const_iterator node = data.begin(); node != data.end(); node++)
29 {
30 // nodes are keyed by the magic key to the node address
31 // this key is then used in dumping the arc from/to pointers
32 std::pair<bool,unsigned> node_mapping = context.pointer_map(node.node());
33 if (node_mapping.first) throw persistent_dump_failed("digraph: already dumped this node");
34 dump_unsigned(context,node_mapping.second);
35 // finally, dump the node contents
36 dump_node(context,*node);
37 }
38 // dump the arcs
39 dump_unsigned(context,data.arc_size());
40 for (typename digraph<NT,AT>::const_arc_iterator arc = data.arc_begin(); arc != data.arc_end(); arc++)
41 {
42 // dump the magic key to the arc address
43 // this is used by iterator persistence too
44 std::pair<bool,unsigned> arc_mapping = context.pointer_map(arc.node());
45 if (arc_mapping.first) throw persistent_dump_failed("digraph: already dumped this arc");
46 dump_unsigned(context,arc_mapping.second);
47 // now dump the from/to pointers as cross-references
48 dump_xref(context,data.arc_from(arc).node());
49 dump_xref(context,data.arc_to(arc).node());
50 // now dump the arc's data
51 dump_arc(context,*arc);
52 }
53 }
54
55 ////////////////////////////////////////////////////////////////////////////////
56
57 template<typename NT, typename AT, typename RN, typename RA>
58 void restore_digraph(restore_context& context, digraph<NT,AT>& data,
59 RN restore_node, RA restore_arc)
60 throw(persistent_restore_failed)
61 {
62 data.clear();
63 // restore the graph's magic key and map it onto the graph's address
64 // this is used in the persistence of iterators
65 unsigned magic = 0;
66 restore_unsigned(context,magic);
67 context.pointer_add(magic,&data);
68 // restore the nodes
69 unsigned nodes = 0;
70 restore_unsigned(context, nodes);
71 for (unsigned n = 0; n < nodes; n++)
72 {
73 unsigned node_magic = 0;
74 restore_unsigned(context,node_magic);
75 // create a new node and map the magic key onto the new address
76 typename digraph<NT,AT>::iterator node = data.insert(NT());
77 context.pointer_add(node_magic,node.node());
78 // now restore the user's data
79 restore_node(context,*node);
80 }
81 // restore the arcs
82 unsigned arcs = 0;
83 restore_unsigned(context, arcs);
84 for (unsigned a = 0; a < arcs; a++)
85 {
86 unsigned arc_magic = 0;
87 restore_unsigned(context,arc_magic);
88 // restore the from and to cross-references
89 digraph_node<NT,AT>* from = 0;
90 digraph_node<NT,AT>* to = 0;
91 restore_xref(context,from);
92 restore_xref(context,to);
93 // create an arc with these from/to pointers
94 digraph_arc_iterator<NT,AT,AT&,AT*> arc =
95 data.arc_insert(digraph_iterator<NT,AT,NT&,NT*>(from),
96 digraph_iterator<NT,AT,NT&,NT*>(to));
97 context.pointer_add(arc_magic,arc.node());
98 // restore the user data
99 restore_arc(context,*arc);
100 }
101 }
102
103 ////////////////////////////////////////////////////////////////////////////////
104
105 template<typename NT, typename AT, typename NRef, typename NPtr>
106 void dump_digraph_iterator(dump_context& context,
107 const digraph_iterator<NT,AT,NRef,NPtr>& data)
108 throw(persistent_dump_failed)
109 {
110 dump_xref(context,data.owner());
111 dump_xref(context,data.node());
112 }
113
114 template<typename NT, typename AT, typename NRef, typename NPtr>
115 void restore_digraph_iterator(restore_context& context,
116 digraph_iterator<NT,AT,NRef,NPtr>& data)
117 throw(persistent_restore_failed)
118 {
119 digraph<NT,AT>* owner = 0;
120 digraph_node<NT,AT>* node = 0;
121 restore_xref(context,owner);
122 restore_xref(context,node);
123 data = digraph_iterator<NT,AT,NRef,NPtr>(node);
124 data.assert_owner(owner);
125 }
126
127 ////////////////////////////////////////////////////////////////////////////////
128
129 template<typename NT, typename AT, typename NRef, typename NPtr>
130 void dump_digraph_arc_iterator(dump_context& context,
131 const digraph_arc_iterator<NT,AT,NRef,NPtr>& data)
132 throw(persistent_dump_failed)
133 {
134 dump_xref(context,data.owner());
135 dump_xref(context,data.node());
136 }
137
138 template<typename NT, typename AT, typename NRef, typename NPtr>
139 void restore_digraph_arc_iterator(restore_context& context,
140 digraph_arc_iterator<NT,AT,NRef,NPtr>& data)
141 throw(persistent_restore_failed)
142 {
143 digraph<NT,AT>* owner = 0;
144 digraph_arc<NT,AT>* arc = 0;
145 restore_xref(context,owner);
146 restore_xref(context,arc);
147 data = digraph_arc_iterator<NT,AT,NRef,NPtr>(arc);
148 data.assert_owner(owner);
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////
152
153 } // end namespace stlplus
This page took 0.039678 seconds and 4 git commands to generate.