object_packers.h

Go to the documentation of this file.
00001 #ifndef OBJECT_PACKERS_CLASS
00002 #define OBJECT_PACKERS_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : object_packers                                                    *
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 <basis/byte_array.h>
00019 #include <basis/definitions.h>
00020 
00021 namespace structures {
00022 
00023 // the sizes in bytes of common objects.  if the compiler doesn't match these, there will
00024 // probably be severe tire damage.
00025 const int PACKED_SIZE_BYTE = 1;
00026 const int PACKED_SIZE_INT16 = 2;
00027 const int PACKED_SIZE_INT32 = 4;
00028 
00029 // these functions pack and unpack popular data types.
00030 
00031 void attach(basis::byte_array &packed_form, bool to_attach);
00033 bool detach(basis::byte_array &packed_form, bool &to_detach);
00035 
00036 void attach(basis::byte_array &packed_form, basis::abyte to_attach);
00038 bool detach(basis::byte_array &packed_form, basis::abyte &to_detach);
00040 
00041 int packed_size(const basis::byte_array &packed_form);
00043 void attach(basis::byte_array &packed_form, const basis::byte_array &to_attach);
00045 bool detach(basis::byte_array &packed_form, basis::byte_array &to_detach);
00047 
00048 void attach(basis::byte_array &packed_form, char to_attach);
00050 bool detach(basis::byte_array &packed_form, char &to_detach);
00052 
00053 int packed_size(double to_pack);
00055 void attach(basis::byte_array &packed_form, double to_pack);
00057 bool detach(basis::byte_array &packed_form, double &to_unpack);
00059 
00060 void attach(basis::byte_array &packed_form, float to_pack);
00062 bool detach(basis::byte_array &packed_form, float &to_unpack);
00064 
00065 void attach(basis::byte_array &packed_form, int to_attach);
00067 
00070 bool detach(basis::byte_array &packed_form, int &to_detach);
00072 
00073 void obscure_attach(basis::byte_array &packed_form, basis::un_int to_attach);
00075 
00077 bool obscure_detach(basis::byte_array &packed_form, basis::un_int &to_detach);
00079 
00080 /*
00081 void attach(basis::byte_array &packed_form, long to_attach);
00083 bool detach(basis::byte_array &packed_form, long &to_detach);
00085 */
00086 
00087 void attach(basis::byte_array &packed_form, short to_attach);
00089 bool detach(basis::byte_array &packed_form, short &to_detach);
00091 
00092 void attach(basis::byte_array &packed_form, basis::un_int to_attach);
00094 bool detach(basis::byte_array &packed_form, basis::un_int &to_detach);
00096 
00097 /*
00098 void attach(basis::byte_array &packed_form, basis::un_long to_attach);
00100 bool detach(basis::byte_array &packed_form, basis::un_long &to_detach);
00102 */
00103 
00104 void attach(basis::byte_array &packed_form, basis::un_short to_attach);
00106 bool detach(basis::byte_array &packed_form, basis::un_short &to_detach);
00108 
00110 
00111 // helpful template functions for packing.
00112 
00114 template <class contents>
00115 void pack_array(basis::byte_array &packed_form, const basis::array<contents> &to_pack) {
00116   obscure_attach(packed_form, to_pack.length());
00117   for (int i = 0; i < to_pack.length(); i++) to_pack[i].pack(packed_form);
00118 }
00119 
00121 template <class contents>
00122 bool unpack_array(basis::byte_array &packed_form, basis::array<contents> &to_unpack) {
00123   to_unpack.reset();
00124   basis::un_int len;
00125   if (!obscure_detach(packed_form, len)) return false;
00126   basis::array<contents> swappy_array(len, NIL, to_unpack.flags());
00127     // we create an array of the specified length to see if it's tenable.
00128   if (!swappy_array.observe()) return false;  // failed to allocate.
00129   for (int i = 0; i < (int)len; i++) {
00130     if (!swappy_array[i].unpack(packed_form))
00131       return false;
00132   }
00133   // now that we've got exactly what we want, plunk it into the result array.
00134   swappy_array.swap_contents(to_unpack);
00135   return true;
00136 }
00137 
00139 template <class contents>
00140 int packed_size_array(const basis::array<contents> &to_pack) {
00141   int to_return = sizeof(int) * 2;  // obscure version uses double int size.
00142   for (int i = 0; i < to_pack.length(); i++)
00143     to_return += to_pack[i].packed_size();
00144   return to_return;
00145 }
00146 
00148 
00150 template <class contents>
00151 void pack_simple(basis::byte_array &packed_form, const basis::array<contents> &to_pack) {
00152   obscure_attach(packed_form, to_pack.length());
00153   for (int i = 0; i < to_pack.length(); i++)
00154     attach(packed_form, to_pack[i]);
00155 }
00156 
00158 
00160 template <class contents>
00161 bool unpack_simple(basis::byte_array &packed_form, basis::array<contents> &to_unpack) {
00162   to_unpack.reset();
00163   basis::un_int len;
00164   if (!obscure_detach(packed_form, len)) return false;
00165   basis::array<contents> swappy_array(len, NIL, to_unpack.flags());
00166   if (!swappy_array.observe()) return false;  // failed to allocate.
00167   for (int i = 0; i < len; i++) {
00168     if (!detach(packed_form, swappy_array[i]))
00169       return false;
00170   }
00171   swappy_array.swap_contents(to_unpack);
00172   return true;
00173 }
00174 
00175 } // namespace
00176 
00177 #endif
00178 
Generated on Sat Jan 28 04:22:26 2012 for hoople2 project by  doxygen 1.6.3