00001 #ifndef PACKABLE_CLASS
00002 #define PACKABLE_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00054
00055
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
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
00155 if (!swappy_array.observe()) return false;
00156 for (int i = 0; i < len; i++) {
00157 if (!swappy_array[i].unpack(packed_form))
00158 return false;
00159 }
00160
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;
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;
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 }
00206
00207 #endif
00208