SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PersistentSTL.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 ///
31 ///@file PersistentSTL.h
32 ///@brief Persistent i/o for STL containers
33 ///
34 ///@author David Hart, Alexei Samsonov
35 /// Department of Computer Science
36 /// University of Utah
37 ///@date Mar. 2000, Dec 2000
38 ///
39 
40 #ifndef SCI_project_PersistentSTL_h
41 #define SCI_project_PersistentSTL_h 1
42 
44 #include <Core/Persistent/share.h>
45 #include <map>
46 #include <vector>
47 #include <list>
48 
49 
50 namespace SCIRun {
51 
52 #define MAP_VERSION 1
53 
54 // Persistent IO for maps
55 template <class Key, class Data>
56 void
57 Pio(Piostream& stream, std::map<Key, Data>& data );
58 
59 //////////
60 // Persistent IO of vector containers
61 template <class T>
62 void Pio(Piostream& stream, std::vector<T>& data);
63 
64 template<class T,class S>
65 void
66 Pio( Piostream &stream, std::pair<T,S>& pair);
67 
68 
69 SCISHARE void
70 Pio_index(Piostream& stream, std::vector<index_type>& data);
71 
72 //////////
73 // Persistent io for maps
74 template <class Key, class Data>
75 inline void
76 Pio(Piostream& stream, std::map<Key, Data>& data) {
77 
78  stream.begin_class("Map", MAP_VERSION);
79 
80  // if reading from stream
81  if (stream.reading()) {
82  // get map size
83  int n;
84  Pio(stream, n);
85  // read elements
86  Key k;
87  Data d;
88  for (int i = 0; i < n; i++) {
89  Pio(stream, k);
90  Pio(stream, d);
91  data[k] = d;
92  }
93 
94  }
95  // if writing to stream
96  else {
97  // write map size
98  int n = data.size();
99  Pio(stream, n);
100  // write elements
101  for (auto iter = data.begin(); iter != data.end(); iter++) {
102  // have to copy iterator elements,
103  // since passing them directly in a
104  // call to Pio can be invalid because
105  // Pio passes data by reference
106  Key ik = (*iter).first;
107  Data dk = (*iter).second;
108  Pio(stream, ik);
109  Pio(stream, dk);
110  }
111 
112  }
113 
114  stream.end_class();
115 
116 }
117 
118 //////////
119 // PIO for vectors
120 #define STLVECTOR_VERSION 2
121 
122 
123 
124 // Optimize heavily used in the field classes.
125 template <>
126 SCISHARE void Pio(Piostream& stream, std::vector<bool>& data);
127 template <>
128 SCISHARE void Pio(Piostream& stream, std::vector<char>& data);
129 template <>
130 SCISHARE void Pio(Piostream& stream, std::vector<unsigned char>& data);
131 template <>
132 SCISHARE void Pio(Piostream& stream, std::vector<short>& data);
133 template <>
134 SCISHARE void Pio(Piostream& stream, std::vector<unsigned short>& data);
135 template <>
136 SCISHARE void Pio(Piostream& stream, std::vector<int>& data);
137 template <>
138 SCISHARE void Pio(Piostream& stream, std::vector<unsigned int>& data);
139 template <>
140 SCISHARE void Pio(Piostream& stream, std::vector<long long>& data);
141 template <>
142 SCISHARE void Pio(Piostream& stream, std::vector<unsigned long long>& data);
143 template <>
144 SCISHARE void Pio(Piostream& stream, std::vector<float>& data);
145 template <>
146 SCISHARE void Pio(Piostream& stream, std::vector<double>& data);
147 
148 template <class T>
149 void Pio(Piostream& stream, std::vector<T>& data)
150 {
151  if (stream.reading() && stream.peek_class() == "Array1")
152  {
153  stream.begin_class("Array1", STLVECTOR_VERSION);
154  }
155  else
156  {
157  stream.begin_class("STLVector", STLVECTOR_VERSION);
158  }
159 
160  int size=static_cast<int>(data.size());
161  stream.io(size);
162 
163  if(stream.reading()){
164  data.resize(size);
165  }
166 
167  for (int i = 0; i < size; i++)
168  {
169  Pio(stream, data[i]);
170  }
171 
172  stream.end_class();
173 }
174 
175 template <class T>
176 void Pio(Piostream& stream, std::vector<T*>& data)
177 {
178  if (stream.reading() && stream.peek_class() == "Array1")
179  {
180  stream.begin_class("Array1", STLVECTOR_VERSION);
181  }
182  else
183  {
184  stream.begin_class("STLVector", STLVECTOR_VERSION);
185  }
186 
187  int size=static_cast<int>(data.size());
188  stream.io(size);
189 
190  if(stream.reading()){
191  data.resize(size);
192  }
193 
194  for (int i = 0; i < size; i++)
195  {
196  Pio(stream, *data[i]);
197  }
198 
199  stream.end_class();
200 }
201 
202 
203 
204 //////////
205 // PIO for lists
206 #define STLLIST_VERSION 1
207 
208 template <class T>
209 void Pio(Piostream& stream, std::list<T>& data)
210 {
211  stream.begin_cheap_delim();
212 
213  int size=data.size();
214  stream.io(size);
215 
216  if(stream.reading()){
217  data.resize(size);
218  }
219 
220  for (typename std::list<T>::iterator ii=data.begin(); ii!=data.end(); ii++)
221  Pio(stream, *ii);
222 
223  stream.end_cheap_delim();
224 }
225 
226 
227 //////////
228 // PIO for pair
229 #define STLPAIR_VERSION 1
230 
231 template <class T,class S>
232 void Pio(Piostream& stream, std::pair<T,S>& data)
233 {
234  stream.begin_class("STLPair", STLPAIR_VERSION);
235 
236  Pio(stream, data.first);
237  Pio(stream, data.second);
238 
239  stream.end_class();
240 }
241 
242 } // End namespace SCIRun
243 
244 #endif // SCI_project_PersistentSTL_h
245 
bool reading() const
Definition: Persistent.h:164
virtual std::string peek_class()
Definition: Persistent.cc:131
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
#define SCISHARE
Definition: share.h:39
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
#define STLVECTOR_VERSION
Definition: PersistentSTL.h:120
Base class for persistent objects...
#define MAP_VERSION
Definition: PersistentSTL.h:52
void Pio_index(Piostream &stream, index_type *data, size_type size)
Definition: Persistent.cc:606
dictionary data
Definition: eabLatVolData.py:11
virtual void end_cheap_delim()
Definition: Persistent.cc:188
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
virtual void end_class()
Definition: Persistent.cc:178
#define STLPAIR_VERSION
Definition: PersistentSTL.h:229
int n
Definition: eab.py:9
int size
Definition: eabLatVolData.py:2