DataEncoder.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_DataEncoder_hxx
00024 #define __ccxx_DataEncoder_hxx
00025 
00026 #include <commonc++/IOException.h++>
00027 
00028 #include <deque>
00029 
00030 namespace ccxx {
00031 
00032 class DataEncoder; // fwd decl
00033 
00075 class COMMONCPP_API DataEncoder
00076 {
00077   public:
00078 
00087   class COMMONCPP_API Manipulator
00088   {
00089     public:
00090 
00092     virtual ~Manipulator() throw() { }
00093 
00099     virtual void apply(DataEncoder &encoder) const throw(IOException) = 0;
00100   };
00101 
00108   class COMMONCPP_API SetEndianness : public Manipulator
00109   {
00110     public:
00111 
00116     SetEndianness(Endianness endianness) throw();
00117 
00119     ~SetEndianness() throw();
00120 
00121     protected:
00122 
00123     void apply(DataEncoder &encoder) const throw(IOException);
00124 
00125     private:
00126 
00127     Endianness _endianness;
00128   };
00129 
00138   class COMMONCPP_API SetLength : public Manipulator
00139   {
00140     public:
00141 
00146     SetLength(size_t length) throw();
00147 
00149     ~SetLength() throw();
00150 
00151     protected:
00152 
00153     void apply(DataEncoder &encoder) const throw(IOException);
00154 
00155     private:
00156 
00157     size_t _length;
00158   };
00159 
00167   class COMMONCPP_API Skip : public Manipulator
00168   {
00169     public:
00170 
00175     Skip(size_t count) throw();
00176 
00178     ~Skip() throw();
00179 
00180     protected:
00181 
00182     void apply(DataEncoder &encoder) const throw(IOException);
00183 
00184     private:
00185 
00186     size_t _count;
00187   };
00188 
00197   class COMMONCPP_API Align : public Manipulator
00198   {
00199     public:
00200 
00206     Align(size_t size) throw();
00207 
00209     ~Align() throw();
00210 
00211     protected:
00212 
00213     void apply(DataEncoder &encoder) const throw(IOException);
00214 
00215     private:
00216 
00217     size_t _size;
00218   };
00219 
00227   class COMMONCPP_API SetLimit : public Manipulator
00228   {
00229     public:
00230 
00235     SetLimit(int64_t limit) throw();
00236 
00238     ~SetLimit() throw();
00239 
00240     protected:
00241 
00242     void apply(DataEncoder &encoder) const throw(IOException);
00243 
00244     private:
00245 
00246     int64_t _limit;
00247   };
00248 
00256   class COMMONCPP_API SetStringPadding : public Manipulator
00257   {
00258     public:
00259 
00265     SetStringPadding(bool stringPadding) throw();
00266 
00268     ~SetStringPadding() throw();
00269 
00270     protected:
00271 
00272     void apply(DataEncoder &encoder) const throw(IOException);
00273 
00274     private:
00275 
00276     bool _stringPadding;
00277   };
00278 
00282   virtual void reset() throw(IOException);
00283 
00285   void pushContext();
00286 
00291   void popContext();
00292 
00294   bool isTopContext() const throw();
00295 
00297   uint_t getContextDepth() const throw();
00298 
00302   void setEndianness(Endianness endianness) throw();
00303 
00305   Endianness getEndianness() const throw();
00306 
00313   void setLength(size_t length) throw();
00314 
00316   size_t getLength() const throw();
00317 
00322   void setLimit(int64_t limit) throw(IOException);
00323 
00325   int64_t getLimit() const throw();
00326 
00334   void setStringPadding(bool stringPadding) throw();
00335 
00337   bool getStringPadding() const throw();
00338 
00340   int64_t getOffset() const throw();
00341 
00343   virtual void setOffset(int64_t offset) throw(IOException) = 0;
00344 
00348   int64_t getCumulativeOffset() const throw();
00349 
00354   int64_t getRemaining() const throw();
00355 
00364   void align(size_t size) throw(IOException);
00365 
00373   virtual void skip(size_t count) throw(IOException) = 0;
00374 
00376   virtual ~DataEncoder() throw();
00377 
00378   protected:
00379 
00381   DataEncoder() throw();
00382 
00386   bool isSameEndianness() const throw();
00387 
00393   struct Context
00394   {
00396     Context();
00397 
00398     void reset();
00399 
00400     void bumpOffset(size_t delta);
00401 
00403     Endianness endianness;
00405     size_t length;
00407     int64_t limit;
00409     int64_t parentLimit;
00411     int64_t offset;
00413     int64_t maxOffset;
00415     bool stringPadding;
00416   };
00417 
00419   Context &currentContext();
00420 
00422   const Context &currentContext() const;
00423 
00431   void checkRemaining(const Context &ctx, size_t count) const
00432     throw(IOException);
00433 
00434   class ContextStack; // fwd decl
00435 
00437   ContextStack *_contexts;
00438   int64_t _maxOffset;
00441   private:
00442 
00443   CCXX_COPY_DECLS(DataEncoder);
00444 };
00445 
00446 }; // namespace ccxx
00447 
00448 #endif // __ccxx_DataEncoder_hxx
00449 
00450 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3