00001 #ifndef _STR_InSerializer_H_ // -*-c++-*- 00002 00003 /* 00004 * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU Lesser General Public License as published 00008 * by the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifdef __GNUG__ 00022 # pragma interface 00023 #endif 00024 00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/InSerializer.h,v 1.6 2002/08/29 00:58:04 hopper Exp $ */ 00026 00027 // For a log, see ../ChangeLog 00028 // 00029 // Revision 1.2 1999/10/21 02:09:10 hopper 00030 // Removed all references to <bool.h> and changed all references to 00031 // bool_val and bool_cst to bool. 00032 // 00033 // Revision 1.1 1997/04/14 23:55:14 hopper 00034 // Added class to aid in serialization of simple data tyes. 00035 // 00036 00037 #include <LCore/GenTypes.h> 00038 #include <string> 00039 00040 #define _STR_InSerializer_H_ 00041 00042 namespace strmod { 00043 namespace strmod { 00044 00045 class StrChunkPtr; 00046 00047 /** \class InSerializer InSerializer.h StrMod/InSerializer.h 00048 * Provides a simple way to 'deserialize' a StrChunk. 00049 * 00050 * This converts from a stream of bytes into more structured data. 00051 * 00052 * See class OutSerializer for a more detailed explanation. 00053 */ 00054 class InSerializer 00055 { 00056 public: 00057 //! Construct an InSerializer that reads bytes from \c ptr. 00058 // \param ptr A pointer to a StrChunk to convert from. 00059 InSerializer(const StrChunkPtr &ptr); 00060 //! Construct an InSerializer that reads bytes directly from a memory area. 00061 // \param buf The beginning of the memory region to convert from. 00062 // \param len The number of bytes in the memory region to convert from. 00063 InSerializer(const void *buf, size_t len); 00064 //! It destroys things. :-) 00065 virtual ~InSerializer(); 00066 00067 //! Get a signed 1 octet value (2's complement) and move forward 1 octet. 00068 lcore::S1Byte GetS1Byte(); 00069 //! Get an unsigned 1 octet value and move forward 1 octet. 00070 lcore::U1Byte GetU1Byte(); 00071 00072 //! Get a signed 2 octet value (2's complement) and move forward 2 octets. 00073 lcore::S2Byte GetS2Byte(); 00074 //! Get an unsigned 2 octet value and move forward 2 octets. 00075 lcore::U2Byte GetU2Byte(); 00076 00077 //! Get a signed 4 octet value (2's complement) and move forward 2 octets. 00078 lcore::S4Byte GetS4Byte(); 00079 //! Get an unsigned 4 octet value and move forward 2 octets. 00080 lcore::U4Byte GetU4Byte(); 00081 00082 //! Get a bool value and move forward 1 octet. 00083 inline bool GetBool() { return(GetU1Byte()); } 00084 00085 //! Get a string value. See OutSerializer for more on format. 00086 const ::std::string GetString(); 00087 00088 //! Get a \c len bytes and dump them in \c destbuf. 00089 // \param destbuf A memory area to copy bytes into. 00090 // \param len The number of bytes to copy. 00091 void GetRaw(void *destbuf, size_t len); 00092 00093 //! How many bytes are there left to read? 00094 size_t BytesLeft(); 00095 // Someday, I'll be able to use exceptions instead. 00096 //! Is the serializer in an error state. 00097 bool HadError() const { return(had_error_); } 00098 00099 private: 00100 struct Impl; 00101 00102 Impl &impl_; 00103 bool had_error_; 00104 }; 00105 00106 //-----------------------------inline functions-------------------------------- 00107 00108 inline InSerializer &operator >>(InSerializer &is, lcore::S1Byte &num) 00109 { 00110 num = is.GetS1Byte(); 00111 return(is); 00112 } 00113 00114 inline InSerializer &operator >>(InSerializer &is, lcore::U1Byte &num) 00115 { 00116 num = is.GetU1Byte(); 00117 return(is); 00118 } 00119 00120 inline InSerializer &operator >>(InSerializer &is, lcore::S2Byte &num) 00121 { 00122 num = is.GetS2Byte(); 00123 return(is); 00124 } 00125 00126 inline InSerializer &operator >>(InSerializer &is, lcore::U2Byte &num) 00127 { 00128 num = is.GetU2Byte(); 00129 return(is); 00130 } 00131 00132 inline InSerializer &operator >>(InSerializer &is, lcore::S4Byte &num) 00133 { 00134 num = is.GetS4Byte(); 00135 return(is); 00136 } 00137 00138 inline InSerializer &operator >>(InSerializer &is, lcore::U4Byte &num) 00139 { 00140 num = is.GetU4Byte(); 00141 return(is); 00142 } 00143 00144 inline InSerializer &operator >>(InSerializer &is, ::std::string &str) 00145 { 00146 str = is.GetString(); 00147 return(is); 00148 } 00149 00150 }; // namespace strmod 00151 }; // namespace strmod 00152 00153 #endif
1.3-rc1