Scopira 20080306

flow.h

00001 
00002 /*
00003  *  Copyright (c) 2002-2007    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013  
00014 #ifndef __INCLUDED_SCOPIRA_TOOL_FLOW_H__
00015 #define __INCLUDED_SCOPIRA_TOOL_FLOW_H__
00016 
00017 #include <string>
00018 
00019 #include <scopira/tool/object.h>
00020 #include <scopira/tool/export.h>
00021 
00022 // THIS FILE HAS BEEN FULLY DOCUMENTED
00023 
00024 //
00025 // the *file_i* based streams provide "simple persistance" (rouge wave
00026 // guide, ver 7, p124). conceptually then are somewhere above
00027 // C FILE and slightly below iostreams (although theyre more
00028 // virtual/runtime polymorphic than iostrmea). theyre modeled after
00029 // Java File.
00030 //
00031 // @author Aleksander Demko
00032 //
00033 
00034 //
00035 // the flow_i interfaces allow polymorphic persinance, often wrapped
00036 // around a file_i based driver. depending on the driver (see polyflow.h)
00037 //
00038 // @author Aleksander Demko
00039 //
00040 
00041 
00042 namespace scopira
00043 {
00044   namespace tool
00045   {
00046     class flow_i;
00047     class iflow_i;
00048     class oflow_i;
00049     class otflow_i;
00050     class itflow_i;
00051     class iobjflow_i;
00052     class oobjflow_i;
00053 
00054     typedef unsigned char byte_t;
00055   }
00056 }
00057 
00065 class scopira::tool::flow_i :  public virtual scopira::tool::object
00066 {
00067   public:
00073     typedef scopira::tool::byte_t byte_t;
00078     typedef int mode_t;
00079 
00080     enum {
00082       input_c = 1,
00084       output_c = 2
00085     };
00086 
00087       // by powers of 2. bits 0-9 are reserved by this
00088       // header file. your first bit should be 10 (2^10 == 1024)
00089       // other decentdants may reserve
00090       // more
00091       
00092     // common methods... seek, close, eof-testing, size ?
00093 
00100     SCOPIRA_EXPORT virtual bool failed(void) const = 0;
00101 };
00102 
00108 class scopira::tool::iflow_i : public virtual scopira::tool::flow_i
00109 {
00110   public:
00119     SCOPIRA_EXPORT virtual size_t read(byte_t *_buf, size_t _maxsize) = 0;
00127     virtual size_t read_byte(byte_t &out) {
00128       return read(&out, 1);
00129     }
00130 
00138     template <class TT>
00139       size_t read_array(TT *_buf, size_t _numelem) {
00140         return read(reinterpret_cast<byte_t*>(_buf), _numelem*sizeof(TT))/sizeof(TT);
00141       }
00142 
00151     inline size_t read_void(void *_buf, size_t _maxsize) { return read(reinterpret_cast<byte_t*>(_buf), _maxsize); }
00152 };
00153 
00159 class scopira::tool::oflow_i : public virtual scopira::tool::flow_i
00160 {
00161   public:
00170     SCOPIRA_EXPORT virtual size_t write(const byte_t *_buf, size_t _size) = 0;
00178     virtual size_t write_byte(byte_t b) {
00179       return write(&b, sizeof(b));
00180     }
00181 
00189     template <class TT>
00190       size_t write_array(const TT *_buf, size_t _numelem) {
00191         return write(reinterpret_cast<const byte_t*>(_buf), _numelem*sizeof(TT))/sizeof(TT);
00192       }
00193 
00202     inline size_t write_void(const void *_buf, size_t _size) { return write(reinterpret_cast<const byte_t*>(_buf), _size); }
00203 };
00204 
00212 class scopira::tool::itflow_i : public virtual scopira::tool::iflow_i
00213 {
00214   public:
00219     SCOPIRA_EXPORT virtual bool read_bool(bool&) = 0;
00224     SCOPIRA_EXPORT virtual bool read_char(char&) = 0;
00229     SCOPIRA_EXPORT virtual bool read_short(short&) = 0;
00234     SCOPIRA_EXPORT virtual bool read_int(int&) = 0;
00239     SCOPIRA_EXPORT virtual bool read_size_t(size_t&) = 0;
00244     SCOPIRA_EXPORT virtual bool read_int64_t(int64_t&) = 0;
00249     SCOPIRA_EXPORT virtual bool read_long(long&) = 0;
00254     SCOPIRA_EXPORT virtual bool read_float(float&) = 0;
00259     SCOPIRA_EXPORT virtual bool read_double(double&) = 0;
00264     SCOPIRA_EXPORT virtual bool read_string(std::string&) = 0;
00269     template <class TT>
00270       inline bool read_generic(TT &v);
00271       // implementation in traits.h to avoid circular refs
00272 };
00273 
00281 class scopira::tool::otflow_i : public virtual scopira::tool::oflow_i
00282 {
00283   public:
00288     SCOPIRA_EXPORT virtual void write_bool(bool) = 0;
00293     SCOPIRA_EXPORT virtual void write_char(char) = 0;
00298     SCOPIRA_EXPORT virtual void write_short(short) = 0;
00303     SCOPIRA_EXPORT virtual void write_int(int) = 0;
00308     SCOPIRA_EXPORT virtual void write_size_t(size_t) = 0;
00313     SCOPIRA_EXPORT virtual void write_int64_t(int64_t) = 0;
00318     SCOPIRA_EXPORT virtual void write_long(long) = 0;
00323     SCOPIRA_EXPORT virtual void write_float(float) = 0;
00324     /***
00325      * Writes a double
00326      * @author Aleksander Demko
00327      */ 
00328     SCOPIRA_EXPORT virtual void write_double(double) = 0;
00333     SCOPIRA_EXPORT virtual void write_string(const std::string&) = 0;
00338     template <class TT>
00339       inline void write_generic(const TT &v);
00340       // implementation in traits.h to avoid circular refs
00341 };
00342 
00343 
00352 class scopira::tool::iobjflow_i : public virtual scopira::tool::itflow_i
00353 {
00354   public:
00355 
00372     SCOPIRA_EXPORT virtual bool read_object(object* &out) = 0;
00373 
00383     template <class TT> bool read_object_type(TT * &out);
00389     template <class TT> bool read_object_type(count_ptr<TT> &out);
00390 };
00391 template <class TT> bool scopira::tool::iobjflow_i::read_object_type(TT * &out)
00392 {
00393   object *o;
00394 
00395   if (!read_object(o))
00396     return false;
00397   assert(!o || dynamic_cast<TT*>(o));
00398   out = dynamic_cast<TT*>(o);
00399   return true;
00400 };
00401 template <class TT> bool scopira::tool::iobjflow_i::read_object_type(count_ptr<TT> &out)
00402 {
00403   object *o;
00404 
00405   if (!read_object(o))
00406     return false;
00407   assert(!o || dynamic_cast<TT*>(o));
00408   out = dynamic_cast<TT*>(o);
00409   return true;
00410 };
00411 
00412 
00421 class scopira::tool::oobjflow_i : public virtual scopira::tool::otflow_i
00422 {
00423   public:
00429     SCOPIRA_EXPORT virtual void write_object(const scopira::tool::object* o) = 0;
00435     template <class L>
00436       void write_object_type(const scopira::tool::count_ptr<L> &o)
00437         { write_object(o.get()); }
00438 };
00439 
00440 //
00441 // Friend functions to allow visual ascii conversion of basic types
00442 // (like printoflow)
00443 //
00444 
00449 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, const char* val);
00454 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, const std::string& val);
00459 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, char val);
00464 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, bool val);
00469 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, int val);
00474 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, long val);
00479 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, double val);
00484 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned int val);
00489 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned long val);
00494 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, long long val);
00499 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, unsigned long long val);
00510 SCOPIRA_EXPORT scopira::tool::oflow_i& operator<<(scopira::tool::oflow_i& o, scopira::tool::iflow_i& i);
00511 
00594 #endif
00595