MessageBuffer.hh

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CERTI - HLA RunTime Infrastructure
00003 // Copyright (C) 2002-2005  ONERA
00004 //
00005 // This program is free software ; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public License
00007 // as published by the Free Software Foundation ; either version 2 of
00008 // the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful, but
00011 // WITHOUT ANY WARRANTY ; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this program ; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018 //
00019 // ----------------------------------------------------------------------------
00020 
00021 #ifndef LIBCERTI_MESSAGE_BUFFER_HH
00022 #define LIBCERTI_MESSAGE_BUFFER_HH
00023 
00024 #include "certi.hh"
00025 #include <string>
00026 
00027 #define DEFAULT_MESSAGE_BUFFER_SIZE 255
00028 
00029 namespace certi {
00030 
00042 class CERTI_EXPORT MessageBuffer
00043 {
00044 public:
00045 
00049     static const bool HostIsBigEndian();
00050 
00054     static const bool HostIsLittleEndian();
00055 
00059     static void show(const void* data, uint32_t n);
00060     
00061     /* 
00062      * We reserve 5 bytes at the beginning of the buffer
00063      * The first byte is a bitset which is used to 
00064      * to tell if the buffer is big or little endian
00065      * The 4 following bytes is for an uint32_t which
00066      * may be used to store the buffer size
00067      */ 
00068     static const uint8_t reservedBytes;
00069 
00074     MessageBuffer();
00075 
00081     MessageBuffer(uint32_t bufferMaxSize);
00082 
00086     ~MessageBuffer();
00087 
00094     uint32_t size() const;
00095 
00101     uint32_t maxSize() const;
00102 
00106     void
00107     assumeBufferIsBigEndian();
00108 
00112     void
00113     assumeBufferIsLittleEndian();
00114 
00120     void
00121     reset();
00122 
00127     void seek_write(uint32_t offset);
00128 
00135     uint32_t resize(uint32_t newSize);
00136 
00145     void assumeSize(uint32_t size);
00146     
00147     void assumeSizeFromReservedBytes();
00148 
00149 #define DECLARE_SIGNED(type)                \
00150     int32_t                     \
00151     write_##type##s(const type##_t* data, uint32_t n) {     \
00152     return write_u##type##s(reinterpret_cast<const u##type##_t*>(data),n);  \
00153 }                           \
00154 \
00155 int32_t                     \
00156 read_##type##s(type##_t* data, uint32_t n) {        \
00157     return read_u##type##s(reinterpret_cast<u##type##_t*>(data),n); \
00158 }                               \
00159 
00160 #define DECLARE_SINGLE_READ_WRITE(type,suffix)     \
00161     int32_t                     \
00162     write_##type(const type##suffix data) {     \
00163     return write_##type##s(&data,1);    \
00164 }                           \
00165 \
00166 int32_t                     \
00167 read_##type(type##suffix* data) {       \
00168     return read_##type##s(data,1);  \
00169 }                                    \
00170 \
00171 type##suffix read_##type() {\
00172     type##suffix retval;     \
00173     read_##type##s(&retval,1);\
00174     return retval; \
00175 } 
00176 
00177 
00178     int32_t
00179     write_uint8s(const uint8_t* data, uint32_t n);
00180 
00181     int32_t
00182     read_uint8s(uint8_t* data, uint32_t n);
00183 
00184     DECLARE_SINGLE_READ_WRITE(uint8,_t)
00185     DECLARE_SIGNED(int8)
00186     DECLARE_SINGLE_READ_WRITE(int8,_t)
00187 
00188     int32_t
00189     write_chars(const char* data, uint32_t n) {
00190         return write_uint8s(reinterpret_cast<const uint8_t*>(data),n);
00191     }
00192 
00193     int32_t
00194     read_chars(char* data, uint32_t n) {
00195         return read_uint8s(reinterpret_cast<uint8_t*>(data),n);
00196     }
00197     DECLARE_SINGLE_READ_WRITE(char,)
00198 
00199 #define write_bytes  write_chars
00200 #define read_bytes   read_chars
00201 #define write_byte   write_char
00202 #define read_byte    read_char
00203 
00204     int32_t
00205     write_uint16s(const uint16_t* data, uint32_t n);
00206     int32_t
00207     read_uint16s(uint16_t* data, uint32_t n);
00208 
00209     DECLARE_SINGLE_READ_WRITE(uint16,_t)
00210     DECLARE_SIGNED(int16)
00211     DECLARE_SINGLE_READ_WRITE(int16,_t)
00212 
00213     int32_t
00214     write_uint32s(const uint32_t* data, uint32_t n);
00215 
00216     int32_t
00217     read_uint32s(uint32_t* data, uint32_t n);
00218 
00219     DECLARE_SINGLE_READ_WRITE(uint32,_t)
00220     DECLARE_SIGNED(int32)
00221     DECLARE_SINGLE_READ_WRITE(int32,_t)
00222 
00223     int32_t
00224     write_uint64s(const uint64_t* data, uint32_t n);
00225     int32_t
00226     read_uint64s(uint64_t* data, uint32_t n);
00227 
00228     DECLARE_SINGLE_READ_WRITE(uint64,_t)
00229     DECLARE_SIGNED(int64)
00230     DECLARE_SINGLE_READ_WRITE(int64,_t)
00231 
00232     int32_t
00233     write_floats(const float* data, uint32_t n);
00234     int32_t
00235     read_floats(float* data, uint32_t n);
00236 
00237     DECLARE_SINGLE_READ_WRITE(float,)
00238 
00239     int32_t
00240     write_doubles(const double* data, uint32_t n);
00241     int32_t
00242     read_doubles(double* data, uint32_t n);
00243 
00244     DECLARE_SINGLE_READ_WRITE(double,)
00245 
00246     int32_t
00247     write_string(const std::string& str);
00248 
00249     std::string
00250     read_string();
00251     
00252     int32_t
00253     write_bool(const bool toggle) {
00254         if(toggle) {
00255             return write_uint8(1);
00256         } else {
00257             return write_uint8(0);
00258         }
00259     };
00260     
00261     bool
00262     read_bool() {   
00263         return (1==read_uint8());
00264     };
00265 
00266     MessageBuffer& operator<<(const uint8_t data) {
00267         this->write_uint8(data);
00268         return *this;
00269     }
00270     
00271     void updateReservedBytes();
00272     
00273     /*
00274      * Pseudo index operator.
00275      * This will be used in send/receive socket call.
00276      * @return address of the underlying buffer + offset.
00277      */
00278     void* operator ()(uint32_t offset);
00279     
00280 private:
00281 
00283     uint8_t* buffer;
00285     uint32_t bufferMaxSize;
00287     bool bufferHasMyEndianness; 
00292     uint32_t writeOffset;   
00293 
00298     uint32_t readOffset;    
00299 
00300     void initialize();
00301     void reallocate(uint32_t n);
00302     void setSizeInReservedBytes(uint32_t n);
00303 };
00304 
00305 } // certi
00306 
00307 #endif // LIBCERTI_MESSAGE_BUFFER_HH

Generated on Thu Apr 30 15:53:49 2009 for CERTIDeveloperDocumentation by doxygen 1.5.5