Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

TypeStream.h

00001 /*****************************************************************************
00002  * Author:   Valient Gough <vgough@pobox.com>
00003  *
00004  *****************************************************************************
00005  * Copyright (c) 2001, Valient Gough
00006  *
00007  * This library is free software; you can distribute it and/or modify it under
00008  * the terms of the GNU Lesser General Public License (LGPL), as published by
00009  * the Free Software Foundation; either version 2.1 of the License, or (at your
00010  * option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE.  See the LGPL in the file COPYING for more
00015  * details.
00016  *
00017  */
00018 
00019 #ifndef _TypeStream_incl_
00020 #define _TypeStream_incl_
00021 
00022 #include <rel/StreamCoder.h>
00023 
00024 #include <string>
00025 
00026 namespace rel
00027 {
00028 
00029     // BeginKey is a marker used in TypeStream to tell when a named section
00030     // begins.
00031     struct BeginKey
00032     {
00033         OpaqueValue name;
00034 
00035         BeginKey() {}
00036         BeginKey(const BeginKey &src)
00037             : name(src.name) {}
00038     };
00039 
00040     // no value associated with EndKey - it is just a placeholder to tell when
00041     // a named section ends (corresponding to the the last BeginKey instance).
00042     struct EndKey
00043     {
00044     };
00045 
00046 
00057     class TypeStream
00058     {
00059     public:
00060         typedef StreamCoder::size_type size_type;
00061 
00062         typedef bool (*CodingWriteFunction)(TypeStream &, const OpaqueValue &);
00063 
00064         /* Register an encoding function for an object type.  All basic types
00065            are handled internally - but other types must have a registered
00066            encoder.  There is no decode function registry because the encode
00067            data is expected to be self-constructing.  That is, given the
00068            encoded data, a lookup into the global object map will produce the
00069            decoded object.
00070          */
00071         static void addEncoder(CodingWriteFunction func, const char *typeName);
00072 
00073         TypeStream(const SmartPtr<StreamCoder> &streamCoder);
00074         virtual ~TypeStream();
00075 
00076 
00077         bool atEnd() const;
00078         void flush();
00079         int getReadDescriptor() const;
00080         bool isDirectAccess() const;
00081         size_type at() const;
00082         void at(size_type offset);
00083 
00084 
00085         // two methods for writing integers
00086         bool read(uint8_t &) const;
00087         bool read(int32_t &) const;
00088         bool read(int64_t &) const;
00089         // method for floating point
00090         bool read(double &) const;
00091         // read string (can contain arbitrary binary data)
00092         bool read(std::string &) const;
00093         // read an arbitrary type (requires self-constructing data when treated
00094         // as a find operation in the Global Object Map). 
00095         bool read(OpaqueValue &) const;
00096 
00097 
00098         // two methods for writing integers
00099         bool write(const uint8_t &);
00100         bool write(const int32_t &);
00101         bool write(const int64_t &);
00102         // method for writing floating point
00103         bool write(const double &);
00104         // write string (can contain arbitrary binary data)
00105         bool write(const std::string &);
00106         bool write(const char *bytes, size_type length);
00107 
00108         // write an arbitrary type. 
00109         bool write(const OpaqueValue &);
00110 
00111         // write BeginKey marker (the next value written is the key, then any
00112         // number of values may be written, followed by EndKey)
00113         bool writeBeginKey();
00114         bool writeEndKey();
00115 
00116         bool readBeginKey() const;
00117         bool readEndKey() const;
00118 
00119         // write null marker..
00120         bool writeNull();
00121         bool readNull() const;
00122 
00123         bool append(const TypeStream &src);
00124 
00125         // for debugging
00126         SmartPtr<char*> print();
00127 
00128     private:
00129         void init();
00130         bool checkType(uint8_t expectedType) const;
00131         bool readConstructable(OpaqueValue &value) const;
00132         static void addEncoder(CodingWriteFunction func, const char *typeName,
00133                 uint8_t marker);
00134 
00135         struct TypeStreamImpl *impl;
00136     };
00137 
00138     class OperationError : public std::exception
00139     {
00140         string _what;
00141     public:
00142         OperationError(const char *operationType, const char *operandType);
00143         OperationError(const char *operationType, 
00144                 const std::string &operandType);
00145         const char *what() const;
00146     };
00147 
00148 } // namespace rel
00149 
00150 
00151 // Define the API for standard operator << types, along with smart pointer
00152 // helpers.  These are defined outside of the rel namespace..
00153 #define TYPEDIOINTERFACE(Type) \
00154 rel::TypeStream &operator << (rel::TypeStream &, const Type &); \
00155 const rel::TypeStream &operator >> (const rel::TypeStream &, Type &);
00156 
00157 TYPEDIOINTERFACE(uint8_t)
00158 TYPEDIOINTERFACE(int32_t)
00159 TYPEDIOINTERFACE(int64_t)
00160 TYPEDIOINTERFACE(double)
00161 TYPEDIOINTERFACE(std::string)
00162 TYPEDIOINTERFACE(rel::OpaqueValue)
00163 
00164 // same as output for std::string, but write char*...
00165 rel::TypeStream &operator << (rel::TypeStream &, const char *);
00166 
00167 // embed one stream into another (not an append).  Use TypeStream::append to
00168 // append one stream without any other markers..
00169 rel::TypeStream &operator << (rel::TypeStream &, const rel::TypeStream &);
00170 // read an embedded stream.
00171 rel::TypeStream &operator >> (rel::TypeStream &, rel::TypeStream &);
00172 
00173 #endif
00174 

Generated at Sat Sep 22 02:20:00 2001 for librel by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001