object_packers.h
Go to the documentation of this file.00001 #ifndef OBJECT_PACKERS_CLASS
00002 #define OBJECT_PACKERS_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <basis/byte_array.h>
00019 #include <basis/definitions.h>
00020
00021 namespace structures {
00022
00023
00024
00025 const int PACKED_SIZE_BYTE = 1;
00026 const int PACKED_SIZE_INT16 = 2;
00027 const int PACKED_SIZE_INT32 = 4;
00028
00029
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
00083
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
00100
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
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
00128 if (!swappy_array.observe()) return false;
00129 for (int i = 0; i < (int)len; i++) {
00130 if (!swappy_array[i].unpack(packed_form))
00131 return false;
00132 }
00133
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;
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;
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 }
00176
00177 #endif
00178