packable.h

Go to the documentation of this file.
00001 #ifndef PACKABLE_CLASS
00002 #define PACKABLE_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : packable                                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "byte_array.h"
00019 
00021 
00027 class packable
00028 {
00029 public:
00030   virtual ~packable();
00031 
00032   virtual void pack(byte_array &packed_form) const = 0;
00034 
00037   virtual bool unpack(byte_array &packed_form) = 0;
00039 
00046   virtual int packed_size() const;
00048 };
00049 
00051 
00052 namespace basis {
00053   // added to the general basis namespace rather than something more specific.
00054 
00055 // these functions pack and unpack popular data types.
00056 
00057 void attach(byte_array &packed_form, bool to_attach);
00059 bool detach(byte_array &packed_form, bool &to_detach);
00061 
00062 void attach(byte_array &packed_form, byte to_attach);
00064 bool detach(byte_array &packed_form, byte &to_detach);
00066 
00067 int packed_size(byte_array &packed_form);
00069 void attach(byte_array &packed_form, const byte_array &to_attach);
00071 bool detach(byte_array &packed_form, byte_array &to_detach);
00073 
00074 void attach(byte_array &packed_form, char to_attach);
00076 bool detach(byte_array &packed_form, char &to_detach);
00078 
00079 void attach(byte_array &packed_form, const char *to_attach);
00081 bool detach(byte_array &packed_form, istring &to_detach);
00083 
00084 void attach(byte_array &packed_form, double to_pack);
00086 bool detach(byte_array &packed_form, double &to_unpack);
00088 
00089 void attach(byte_array &packed_form, float to_pack);
00091 bool detach(byte_array &packed_form, float &to_unpack);
00093 
00094 void attach(byte_array &packed_form, int to_attach);
00096 
00099 bool detach(byte_array &packed_form, int &to_detach);
00101 
00102 void obscure_attach(byte_array &packed_form, int to_attach);
00104 
00106 bool obscure_detach(byte_array &packed_form, int &to_detach);
00108 
00109 void attach(byte_array &packed_form, long to_attach);
00111 bool detach(byte_array &packed_form, long &to_detach);
00113 
00114 void attach(byte_array &packed_form, short to_attach);
00116 bool detach(byte_array &packed_form, short &to_detach);
00118 
00119 void attach(byte_array &packed_form, u_int to_attach);
00121 bool detach(byte_array &packed_form, u_int &to_detach);
00123 
00124 void attach(byte_array &packed_form, u_long to_attach);
00126 bool detach(byte_array &packed_form, u_long &to_detach);
00128 
00129 void attach(byte_array &packed_form, u_short to_attach);
00131 bool detach(byte_array &packed_form, u_short &to_detach);
00133 
00135 
00136 // helpful template functions for packing.
00137 
00139 template <class contents>
00140 void pack(byte_array &packed_form, const array<contents> &to_pack)
00141 {
00142   basis::obscure_attach(packed_form, to_pack.length());
00143   for (int i = 0; i < to_pack.length(); i++) to_pack[i].pack(packed_form);
00144 }
00145 
00147 template <class contents>
00148 bool unpack(byte_array &packed_form, array<contents> &to_unpack)
00149 {
00150   to_unpack.reset();
00151   int len;
00152   if (!basis::obscure_detach(packed_form, len)) return false;
00153   array<contents> swappy_array(len, NIL, to_unpack.flags());
00154     // we create an array of the specified length to see if it's tenable.
00155   if (!swappy_array.observe()) return false;  // failed to allocate.
00156   for (int i = 0; i < len; i++) {
00157     if (!swappy_array[i].unpack(packed_form))
00158       return false;
00159   }
00160   // now that we've got exactly what we want, plunk it into the result array.
00161   swappy_array.swap_contents(to_unpack);
00162   return true;
00163 }
00164 
00166 template <class contents>
00167 int packed_size(const array<contents> &to_pack)
00168 {
00169   int to_return = sizeof(int) * 2;  // obscure version uses double int size.
00170   for (int i = 0; i < to_pack.length(); i++)
00171     to_return += to_pack[i].packed_size();
00172   return to_return;
00173 }
00174 
00176 
00178 template <class contents>
00179 void pack_simple(byte_array &packed_form, const array<contents> &to_pack)
00180 {
00181   basis::obscure_attach(packed_form, to_pack.length());
00182   for (int i = 0; i < to_pack.length(); i++)
00183     basis::attach(packed_form, to_pack[i]);
00184 }
00185 
00187 
00189 template <class contents>
00190 bool unpack_simple(byte_array &packed_form, array<contents> &to_unpack)
00191 {
00192   to_unpack.reset();
00193   int len;
00194   if (!basis::obscure_detach(packed_form, len)) return false;
00195   array<contents> swappy_array(len, NIL, to_unpack.flags());
00196   if (!swappy_array.observe()) return false;  // failed to allocate.
00197   for (int i = 0; i < len; i++) {
00198     if (!basis::detach(packed_form, swappy_array[i]))
00199       return false;
00200   }
00201   swappy_array.swap_contents(to_unpack);
00202   return true;
00203 }
00204 
00205 } // namespace
00206 
00207 #endif
00208 

Generated on Fri Sep 5 04:28:37 2008 for HOOPLE Libraries by  doxygen 1.5.1