PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/serializer.hpp

http://serializer.googlecode.com/
C++ Header | 194 lines | 151 code | 27 blank | 16 comment | 12 complexity | f81c20eae1306c2a4b4732120ed6be63 MD5 | raw file
  1/*
  2 **************************************************************************
  3 *                                                                        *
  4 *                     Binary Data Serializer Library                     *
  5 *                                                                        *
  6 * Author: Arash Partow - 2001                                            *
  7 * URL: http://www.partow.net                                             *
  8 *                                                                        *
  9 * Copyright notice:                                                      *
 10 * Free use of the Binary Data Serializer Library is permitted under the  *
 11 * guidelines and in accordance with the most current version of the      *
 12 * Common Public License.                                                 *
 13 * http://www.opensource.org/licenses/cpl.php                             *
 14 *                                                                        *
 15 **************************************************************************
 16*/
 17
 18#ifndef INCLUDE_SERIALIZER_HPP
 19#define INCLUDE_SERIALIZER_HPP
 20
 21
 22#include <iostream>
 23#include <string>
 24#include <algorithm>
 25#include <fstream>
 26
 27
 28class serializer
 29{
 30public:
 31
 32   serializer(char* buffer, const unsigned int& buffer_length)
 33   : original_buffer_(buffer),
 34     buffer_(buffer + 1),
 35     buffer_length_(buffer_length)
 36   {
 37   }
 38
 39   void reset()
 40   {
 41      written_buffer_size_ = 0;
 42      read_buffer_size_    = 0;
 43      buffer_ = original_buffer_;
 44   }
 45
 46   void clear()
 47   {
 48      reset();
 49      for(unsigned int i = 0; i < buffer_length_; ++i)
 50      {
 51         buffer_[i] = 0;
 52      }
 53   }
 54
 55   void delete_internal_buffer()
 56   {
 57      delete[] original_buffer_;
 58      original_buffer_ = 0;
 59      buffer_ = 0;
 60   }
 61
 62   unsigned int length() { return written_buffer_size_; }
 63
 64   template<typename T> inline bool operator >> (T& output) { return read (output); }
 65   template<typename T> inline bool operator << (T& output) { return write(output); }
 66
 67   inline bool read(char& output)         { return read_pod(output); }
 68   inline bool read(int& output)          { return read_pod(output); }
 69   inline bool read(unsigned int& output) { return read_pod(output); }
 70   inline bool read(float& output)        { return read_pod(output); }
 71   inline bool read(double& output)       { return read_pod(output); }
 72
 73   inline bool read(std::string& output)
 74   {
 75      unsigned int length = 0;
 76      if (!read_pod(length))
 77      {
 78         return false;
 79      }
 80
 81      if ((length + sizeof(unsigned int) + read_buffer_size_) > buffer_length_)
 82      {
 83         return false;
 84      }
 85      output.resize(length);
 86      std::copy(buffer_, buffer_ + length,output.begin());
 87      buffer_ += length;
 88      read_buffer_size_ += length;
 89      return true;
 90   }
 91
 92   inline bool write(const char&         input) { return write_pod(input); }
 93   inline bool write(const int&          input) { return write_pod(input); }
 94   inline bool write(const unsigned int& input) { return write_pod(input); }
 95   inline bool write(const float&        input) { return write_pod(input); }
 96   inline bool write(const double&       input) { return write_pod(input); }
 97   inline bool write(const short&        input) { return write_pod(input); }
 98
 99   inline bool write(std::string& input)
100   {
101      return write(input.c_str(),static_cast<unsigned int>(input.size()));
102   }
103
104   inline bool write(const char* data, unsigned int length)
105   {
106      if ((length + sizeof(unsigned int) + written_buffer_size_) > buffer_length_)
107      {
108         return false;
109      }
110      write(length);
111      std::copy(data,data + length,buffer_);
112      buffer_ += length;
113      written_buffer_size_ += length;
114      return true;
115   }
116
117   inline void write_to_stream(std::ofstream& stream)
118   {
119      stream.write(original_buffer_,written_buffer_size_);
120   }
121
122   inline void read_from_stream(std::ifstream& stream, const unsigned int length)
123   {
124      if (length > buffer_length_)
125      {
126         return;
127      }
128      stream.read(original_buffer_,length);
129   }
130
131   inline void write_to_buffer(char data[])
132   {
133      std::copy(original_buffer_,original_buffer_ + written_buffer_size_,data);
134   }
135
136   inline void read_from_buffer(const char data[], const unsigned int& length)
137   {
138      if (length > buffer_length_)
139      {
140         return;
141      }
142      std::copy(data,data + length,original_buffer_);
143   }
144
145private:
146
147   serializer();
148   serializer(const serializer& s);
149   serializer& operator=(const serializer& s);
150
151   template<typename T>
152   inline bool write_pod(const T& data)
153   {
154      const unsigned int data_length = sizeof(T);
155      if ((data_length + written_buffer_size_) > buffer_length_)
156      {
157         return false;
158      }
159      const char* ptr = reinterpret_cast<const char*>(&data);
160      const char* end = reinterpret_cast<const char*>(&data) + sizeof(T);
161      for(; ptr != end; ++buffer_, ++ptr)
162      {
163         *buffer_ = *ptr;
164      }
165      written_buffer_size_ += data_length;
166      return true;
167   }
168
169   template<typename T>
170   inline bool read_pod(T& data)
171   {
172      const unsigned int data_length = sizeof(T);
173      if ((data_length + read_buffer_size_) > buffer_length_)
174      {
175         return false;
176      }
177      char* ptr = reinterpret_cast<char*>(&data);
178      char* end = reinterpret_cast<char*>(&data) + sizeof(T);
179      for(; ptr != end; ++buffer_, ++ptr)
180      {
181         *ptr = *buffer_;
182      }
183      read_buffer_size_ += data_length;
184      return true;
185   }
186
187   char* original_buffer_;
188   char* buffer_;
189   unsigned int buffer_length_;
190   unsigned int written_buffer_size_;
191   unsigned int read_buffer_size_;
192};
193
194#endif