Scopira  20080306
flow.h
1 
2 /*
3  * Copyright (c) 2002-2007 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_TOOL_FLOW_H__
15 #define __INCLUDED_SCOPIRA_TOOL_FLOW_H__
16 
17 #include <string>
18 
19 #include <scopira/tool/object.h>
20 #include <scopira/tool/export.h>
21 
22 // THIS FILE HAS BEEN FULLY DOCUMENTED
23 
24 //
25 // the *file_i* based streams provide "simple persistance" (rouge wave
26 // guide, ver 7, p124). conceptually then are somewhere above
27 // C FILE and slightly below iostreams (although theyre more
28 // virtual/runtime polymorphic than iostrmea). theyre modeled after
29 // Java File.
30 //
31 // @author Aleksander Demko
32 //
33 
34 //
35 // the flow_i interfaces allow polymorphic persinance, often wrapped
36 // around a file_i based driver. depending on the driver (see polyflow.h)
37 //
38 // @author Aleksander Demko
39 //
40 
41 
42 namespace scopira
43 {
44  namespace tool
45  {
46  class flow_i;
47  class iflow_i;
48  class oflow_i;
49  class otflow_i;
50  class itflow_i;
51  class iobjflow_i;
52  class oobjflow_i;
53 
54  typedef unsigned char byte_t;
55  }
56 }
57 
66 {
67  public:
73  typedef scopira::tool::byte_t byte_t;
78  typedef int mode_t;
79 
80  enum {
82  input_c = 1,
85  };
86 
87  // by powers of 2. bits 0-9 are reserved by this
88  // header file. your first bit should be 10 (2^10 == 1024)
89  // other decentdants may reserve
90  // more
91 
92  // common methods... seek, close, eof-testing, size ?
93 
100  SCOPIRA_EXPORT virtual bool failed(void) const = 0;
101 };
102 
109 {
110  public:
119  SCOPIRA_EXPORT virtual size_t read(byte_t *_buf, size_t _maxsize) = 0;
127  virtual size_t read_byte(byte_t &out) {
128  return read(&out, 1);
129  }
130 
138  template <class TT>
139  size_t read_array(TT *_buf, size_t _numelem) {
140  return read(reinterpret_cast<byte_t*>(_buf), _numelem*sizeof(TT))/sizeof(TT);
141  }
142 
151  inline size_t read_void(void *_buf, size_t _maxsize) { return read(reinterpret_cast<byte_t*>(_buf), _maxsize); }
152 };
153 
160 {
161  public:
170  SCOPIRA_EXPORT virtual size_t write(const byte_t *_buf, size_t _size) = 0;
178  virtual size_t write_byte(byte_t b) {
179  return write(&b, sizeof(b));
180  }
181 
189  template <class TT>
190  size_t write_array(const TT *_buf, size_t _numelem) {
191  return write(reinterpret_cast<const byte_t*>(_buf), _numelem*sizeof(TT))/sizeof(TT);
192  }
193 
202  inline size_t write_void(const void *_buf, size_t _size) { return write(reinterpret_cast<const byte_t*>(_buf), _size); }
203 };
204 
213 {
214  public:
219  SCOPIRA_EXPORT virtual bool read_bool(bool&) = 0;
224  SCOPIRA_EXPORT virtual bool read_char(char&) = 0;
229  SCOPIRA_EXPORT virtual bool read_short(short&) = 0;
234  SCOPIRA_EXPORT virtual bool read_int(int&) = 0;
239  SCOPIRA_EXPORT virtual bool read_size_t(size_t&) = 0;
244  SCOPIRA_EXPORT virtual bool read_int64_t(int64_t&) = 0;
249  SCOPIRA_EXPORT virtual bool read_long(long&) = 0;
254  SCOPIRA_EXPORT virtual bool read_float(float&) = 0;
259  SCOPIRA_EXPORT virtual bool read_double(double&) = 0;
264  SCOPIRA_EXPORT virtual bool read_string(std::string&) = 0;
269  template <class TT>
270  inline bool read_generic(TT &v);
271  // implementation in traits.h to avoid circular refs
272 };
273 
282 {
283  public:
288  SCOPIRA_EXPORT virtual void write_bool(bool) = 0;
293  SCOPIRA_EXPORT virtual void write_char(char) = 0;
298  SCOPIRA_EXPORT virtual void write_short(short) = 0;
303  SCOPIRA_EXPORT virtual void write_int(int) = 0;
308  SCOPIRA_EXPORT virtual void write_size_t(size_t) = 0;
313  SCOPIRA_EXPORT virtual void write_int64_t(int64_t) = 0;
318  SCOPIRA_EXPORT virtual void write_long(long) = 0;
323  SCOPIRA_EXPORT virtual void write_float(float) = 0;
324  /***
325  * Writes a double
326  * @author Aleksander Demko
327  */
328  SCOPIRA_EXPORT virtual void write_double(double) = 0;
333  SCOPIRA_EXPORT virtual void write_string(const std::string&) = 0;
338  template <class TT>
339  inline void write_generic(const TT &v);
340  // implementation in traits.h to avoid circular refs
341 };
342 
343 
353 {
354  public:
355 
372  SCOPIRA_EXPORT virtual bool read_object(object* &out) = 0;
373 
383  template <class TT> bool read_object_type(TT * &out);
389  template <class TT> bool read_object_type(count_ptr<TT> &out);
390 };
391 template <class TT> bool scopira::tool::iobjflow_i::read_object_type(TT * &out)
392 {
393  object *o;
394 
395  if (!read_object(o))
396  return false;
397  assert(!o || dynamic_cast<TT*>(o));
398  out = dynamic_cast<TT*>(o);
399  return true;
400 };
402 {
403  object *o;
404 
405  if (!read_object(o))
406  return false;
407  assert(!o || dynamic_cast<TT*>(o));
408  out = dynamic_cast<TT*>(o);
409  return true;
410 };
411 
412 
422 {
423  public:
429  SCOPIRA_EXPORT virtual void write_object(const scopira::tool::object* o) = 0;
435  template <class L>
437  { write_object(o.get()); }
438 };
439 
440 //
441 // Friend functions to allow visual ascii conversion of basic types
442 // (like printoflow)
443 //
444 
449 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, const char* val);
454 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, const std::string& val);
459 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, char val);
464 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, bool val);
469 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, int val);
474 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, long val);
479 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, double val);
484 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned int val);
489 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned long val);
494 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, long long val);
499 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned long long val);
510 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, scopira::tool::iflow_i& i);
511 
594 #endif
595 
virtual size_t read_byte(byte_t &out)
Definition: flow.h:127
Definition: flow.h:108
Open flow for output.
Definition: flow.h:84
scopira::tool::byte_t byte_t
Definition: flow.h:73
bool read_object_type(TT *&out)
Definition: flow.h:391
Definition: archiveflow.h:20
Definition: flow.h:212
Definition: flow.h:281
Definition: flow.h:352
size_t write_array(const TT *_buf, size_t _numelem)
Definition: flow.h:190
T * get(void) const
Definition: object.h:378
Definition: object.h:71
Definition: object.h:42
Open flow for input.
Definition: flow.h:82
size_t read_array(TT *_buf, size_t _numelem)
Definition: flow.h:139
int mode_t
Definition: flow.h:78
Definition: flow.h:421
Definition: flow.h:65
virtual bool failed(void) const =0
size_t read_void(void *_buf, size_t _maxsize)
Definition: flow.h:151
size_t write_void(const void *_buf, size_t _size)
Definition: flow.h:202
virtual size_t write_byte(byte_t b)
Definition: flow.h:178
void write_object_type(const scopira::tool::count_ptr< L > &o)
Definition: flow.h:436
Definition: flow.h:159