SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Persistent.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 ///
32 ///@file Persistent.h
33 ///@brief Base class for persistent objects...
34 ///
35 ///@author
36 /// Steven G. Parker
37 /// Department of Computer Science
38 /// University of Utah
39 ///@date April 1994
40 ///
41 
42 #ifndef CORE_PERSISTENT_H
43 #define CORE_PERSISTENT_H 1
44 
45 #include <boost/utility/enable_if.hpp>
46 #include <boost/type_traits/is_base_of.hpp>
48 #include <map>
49 #include <string>
50 #include <boost/shared_ptr.hpp>
51 
52 // for index and size types
54 
56 #include <Core/Logging/LoggerFwd.h>
57 
58 #include <Core/Persistent/share.h>
59 
60 namespace SCIRun {
61 
62  typedef Persistent* (*PersistentMaker0)();
63 
65 public:
67  PersistentTypeID(const std::string& type,
68  const std::string& parent,
69  PersistentMaker0 maker,
70  Persistent* (*bc_maker1)() = 0,
71  Persistent* (*bc_maker2)() = 0);
72  std::string type;
73  std::string parent;
75  Persistent* (*bc_maker1)();
76  Persistent* (*bc_maker2)();
77 };
78 
79 typedef boost::shared_ptr<PersistentTypeID> PersistentTypeIDPtr;
80 typedef boost::shared_ptr<Piostream> PiostreamPtr;
81 
82 SCISHARE PiostreamPtr auto_istream(const std::string& filename,
84 SCISHARE PiostreamPtr auto_ostream(const std::string& filename, const std::string& type,
86 
87 
88 //----------------------------------------------------------------------
90 
91  public:
92  typedef std::map<Persistent*, int> MapPersistentInt;
93  typedef std::map<int, Persistent*> MapIntPersistent;
94 
95  enum Direction {
97  Write
98  };
99 
100  enum Endian {
102  Little
103  };
104 
105  static const int PERSISTENT_VERSION;
106  void flag_error() { err = 1; }
107 
108  protected:
109  Piostream(Direction, int, const std::string &, Core::Logging::LoggerHandle pr);
110 
112  int version_;
113  bool err;
115 
118 
120 
122  std::string peekname_;
123 
127  virtual void emit_pointer(int& have_data, int& pointer_id);
128  public:
129  static bool readHeader(Core::Logging::LoggerHandle pr,
130  const std::string& filename, char* hdr,
131  const char* type, int& version, int& endian);
132  private:
133  virtual void reset_post_header() = 0;
134  public:
135  std::string file_name;
136 
137  virtual ~Piostream();
138 
139  virtual std::string peek_class();
140  virtual int begin_class(const std::string& name, int current_version);
141  virtual void end_class();
142  virtual void begin_cheap_delim();
143  virtual void end_cheap_delim();
144 
145  virtual void io(bool&);
146  virtual void io(char&) = 0;
147  virtual void io(signed char&) = 0;
148  virtual void io(unsigned char&) = 0;
149  virtual void io(short&) = 0;
150  virtual void io(unsigned short&) = 0;
151  virtual void io(int&) = 0;
152  virtual void io(unsigned int&) = 0;
153  virtual void io(long&) = 0;
154  virtual void io(unsigned long&) = 0;
155  virtual void io(long long&) = 0;
156  virtual void io(unsigned long long&) = 0;
157  virtual void io(double&) = 0;
158  virtual void io(float&) = 0;
159  virtual void io(std::string& str) = 0;
160  virtual bool eof() { return false; }
161 
162  void io(Persistent*&, const PersistentTypeID&);
163 
164  bool reading() const { return dir == Read; }
165  bool writing() const { return dir == Write; }
166  bool error() const { return err; }
167 
168  int version() const { return version_; }
169  bool backwards_compat_id() const { return backwards_compat_id_; }
170  void set_backwards_compat_id(bool p) { backwards_compat_id_ = p; }
171  virtual bool supports_block_io() { return false; } // deprecated, redundant.
172 
173  // Returns true if block_io was supported (even on error).
174  virtual bool block_io(void*, size_t, size_t) { return false; }
175 
176  void disable_pointer_hashing() { disable_pointer_hashing_ = true; }
177 
178  SCISHARE friend PiostreamPtr auto_istream(const std::string& filename,
180  SCISHARE friend PiostreamPtr auto_ostream(const std::string& filename,
181  const std::string& type,
183 };
184 
185 
186 //----------------------------------------------------------------------
188 
189  public:
190  virtual ~Persistent();
191  virtual void io(Piostream&);
192 
193  //----------------------------------------------------------------------
194  // Functions for managing the database of Persistent objects
195  // Note all of these functions are static, to allow access from outside
196  // the class
197 
198  static PersistentTypeIDPtr find_derived( const std::string& classname,
199  const std::string& basename );
200  static bool is_base_of(const std::string& parent, const std::string& type);
201 
202  static void add_class(const std::string& type,
203  const std::string& parent,
204  Persistent* (*maker)(),
205  Persistent* (*bc_maker1)() = 0,
206  Persistent* (*bc_maker2)() = 0);
207 
208  static void add_mesh_class(const std::string& type,
209  Persistent* (*maker)(),
210  Persistent* (*bc_maker1)() = 0,
211  Persistent* (*bc_maker2)() = 0);
212 
213  static void add_field_class(const std::string& type,
214  Persistent* (*maker)(),
215  Persistent* (*bc_maker1)() = 0,
216  Persistent* (*bc_maker2)() = 0);
217  private:
218  // Mutex protecting the list, these are in the class so they will be
219  // initialized before any of the static functions can be called
220 
221  typedef std::map<std::string, PersistentTypeIDPtr> MapStringPersistentID;
222  static MapStringPersistentID* persistent_table_;
223  static Core::Thread::Mutex* persistent_mutex_;
224 
225  static void initialize();
226 };
227 
228 //----------------------------------------------------------------------
229 inline void Pio(Piostream& stream, bool& data) { stream.io(data); }
230 inline void Pio(Piostream& stream, char& data) { stream.io(data); }
231 inline void Pio(Piostream& stream, signed char& data) { stream.io(data); }
232 inline void Pio(Piostream& stream, unsigned char& data) { stream.io(data); }
233 inline void Pio(Piostream& stream, short& data) { stream.io(data); }
234 inline void Pio(Piostream& stream, unsigned short& data) { stream.io(data); }
235 inline void Pio(Piostream& stream, int& data) { stream.io(data); }
236 inline void Pio(Piostream& stream, unsigned int& data) { stream.io(data); }
237 inline void Pio(Piostream& stream, long& data) { stream.io(data); }
238 inline void Pio(Piostream& stream, unsigned long& data) { stream.io(data); }
239 inline void Pio(Piostream& stream, long long& data) { stream.io(data); }
240 inline void Pio(Piostream& stream, unsigned long long& data) { stream.io(data); }
241 inline void Pio(Piostream& stream, double& data) { stream.io(data); }
242 inline void Pio(Piostream& stream, float& data) { stream.io(data); }
243 inline void Pio(Piostream& stream, std::string& data) { stream.io(data); }
244 inline void Pio(Piostream& stream, Persistent& data) { data.io(stream); }
245 
246 SCISHARE void Pio_index(Piostream& stream, index_type* data, size_type sz);
247 
248 
249 template<class T>
250 void Pio(Piostream& stream, boost::shared_ptr<T>& data, typename boost::enable_if<typename boost::is_base_of<Persistent, T>::type>* = 0)
251 {
252  stream.begin_cheap_delim();
253  Persistent* trep = data.get();
254  stream.io(trep, T::type_id);
255  if(stream.reading())
256  {
257  data.reset(static_cast<T*>(trep));
258  }
259  stream.end_cheap_delim();
260 }
261 
262 template<class T>
263 inline void Pio(Piostream& stream, T* data, size_type sz)
264 {
265  if (!stream.block_io(data, sizeof(T), sz))
266  {
267  for (index_type i=0;i<sz;i++)
268  stream.io(data[i]);
269  }
270 }
271 
272 template <typename Size>
273 inline void Pio_size(Piostream& stream, Size& size)
274 {
275  long long temp = static_cast<long long>(size);
276  stream.io(temp);
277  size = static_cast<size_type>(temp);
278 }
279 
280 template <typename Index>
281 inline void Pio_index(Piostream& stream, Index& index)
282 {
283  long long temp = static_cast<long long>(index);
284  stream.io(temp);
285  index = static_cast<size_type>(temp);
286 }
287 
288 } // End namespace SCIRun
289 
290 #endif
void disable_pointer_hashing()
Definition: Persistent.h:176
PiostreamPtr auto_ostream(const std::string &filename, const std::string &type, LoggerHandle pr)
Definition: Persistent.cc:478
void set_backwards_compat_id(bool p)
Definition: Persistent.h:170
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
boost::shared_ptr< LegacyLoggerInterface > LoggerHandle
Definition: LoggerFwd.h:42
bool backwards_compat_id() const
Definition: Persistent.h:169
virtual bool supports_block_io()
Definition: Persistent.h:171
Core::Logging::LoggerHandle reporter_
Definition: Persistent.h:124
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
int current_pointer_id
Definition: Persistent.h:119
std::map< Persistent *, int > MapPersistentInt
Definition: Persistent.h:92
Definition: Persistent.h:187
std::map< int, Persistent * > MapIntPersistent
Definition: Persistent.h:93
Direction
Definition: Persistent.h:95
#define SCISHARE
Definition: share.h:39
bool writing() const
Definition: Persistent.h:165
Utility for specifying data invariants (Assertions)
Definition: Mutex.h:43
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
Persistent *(* PersistentMaker0)()
Definition: Persistent.h:62
bool err
Definition: Persistent.h:113
string dir
Definition: eab.py:5
bool error() const
Definition: Persistent.h:166
virtual void io(Piostream &)
Definition: Persistent.cc:205
Direction dir
Definition: Persistent.h:111
PiostreamPtr auto_istream(const std::string &filename, LoggerHandle pr)
Definition: Persistent.cc:394
PersistentMaker0 maker
Definition: Persistent.h:74
boost::shared_ptr< Piostream > PiostreamPtr
Definition: Persistent.h:80
const char * name[]
Definition: BoostGraphExampleTests.cc:87
Definition: Persistent.h:101
std::string peekname_
Definition: Persistent.h:122
void Pio_index(Piostream &stream, index_type *data, size_type size)
Definition: Persistent.cc:606
long long size_type
Definition: Types.h:40
dictionary data
Definition: eabLatVolData.py:11
std::string type
Definition: Persistent.h:72
int version() const
Definition: Persistent.h:168
virtual void end_cheap_delim()
Definition: Persistent.cc:188
bool backwards_compat_id_
Definition: Persistent.h:125
virtual bool eof()
Definition: Persistent.h:160
std::string parent
Definition: Persistent.h:73
MapIntPersistent * inpointers
Definition: Persistent.h:117
bool have_peekname_
Definition: Persistent.h:121
bool disable_pointer_hashing_
Definition: Persistent.h:126
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
long long index_type
Definition: Types.h:39
void flag_error()
Definition: Persistent.h:106
Definition: Persistent.h:96
boost::shared_ptr< PersistentTypeID > PersistentTypeIDPtr
Definition: Persistent.h:79
Definition: Persistent.h:64
std::string file_name
Definition: Persistent.h:135
static const int PERSISTENT_VERSION
Definition: Persistent.h:105
int version_
Definition: Persistent.h:112
Endian
Definition: Persistent.h:100
void Pio_size(Piostream &stream, Size &size)
Definition: Persistent.h:273
int file_endian
Definition: Persistent.h:114
std::string basename(const std::string &path)
Remove directory name.
Definition: StringUtil.h:128
MapPersistentInt * outpointers
Definition: Persistent.h:116
int size
Definition: eabLatVolData.py:2