feisty meow concerns codebase  2.140
object_packers.h
Go to the documentation of this file.
1 #ifndef OBJECT_PACKERS_CLASS
2 #define OBJECT_PACKERS_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : object_packers *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1995-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/byte_array.h>
19 #include <basis/definitions.h>
20 
21 namespace structures {
22 
23 // the sizes in bytes of common objects. if the compiler doesn't match these, there will
24 // probably be severe tire damage.
25 const int PACKED_SIZE_BYTE = 1;
26 const int PACKED_SIZE_INT16 = 2;
27 const int PACKED_SIZE_INT32 = 4;
28 const int PACKED_SIZE_INT64 = 8;
29 
30 // these functions pack and unpack popular data types.
31 
32 void attach(basis::byte_array &packed_form, bool to_attach);
34 bool detach(basis::byte_array &packed_form, bool &to_detach);
36 
37 void attach(basis::byte_array &packed_form, basis::abyte to_attach);
39 bool detach(basis::byte_array &packed_form, basis::abyte &to_detach);
41 
42 int packed_size(const basis::byte_array &packed_form);
44 void attach(basis::byte_array &packed_form, const basis::byte_array &to_attach);
46 bool detach(basis::byte_array &packed_form, basis::byte_array &to_detach);
48 
49 void attach(basis::byte_array &packed_form, char to_attach);
51 bool detach(basis::byte_array &packed_form, char &to_detach);
53 
54 int packed_size(double to_pack);
56 void attach(basis::byte_array &packed_form, double to_pack);
58 bool detach(basis::byte_array &packed_form, double &to_unpack);
60 
61 void attach(basis::byte_array &packed_form, float to_pack);
63 bool detach(basis::byte_array &packed_form, float &to_unpack);
65 
66 void attach(basis::byte_array &packed_form, int to_attach);
68 
71 bool detach(basis::byte_array &packed_form, int &to_detach);
73 
74 void attach(basis::byte_array &packed_form, long int to_attach);
76 
79 bool detach(basis::byte_array &packed_form, long int &to_detach);
81 
82 void obscure_attach(basis::byte_array &packed_form, basis::un_int to_attach);
84 
86 bool obscure_detach(basis::byte_array &packed_form, basis::un_int &to_detach);
88 
89 /*
90 void attach(basis::byte_array &packed_form, long to_attach);
92 bool detach(basis::byte_array &packed_form, long &to_detach);
94 */
95 
96 void attach(basis::byte_array &packed_form, short to_attach);
98 bool detach(basis::byte_array &packed_form, short &to_detach);
100 
101 void attach(basis::byte_array &packed_form, basis::un_int to_attach);
103 bool detach(basis::byte_array &packed_form, basis::un_int &to_detach);
105 
106 /*
107 void attach(basis::byte_array &packed_form, basis::un_long to_attach);
109 bool detach(basis::byte_array &packed_form, basis::un_long &to_detach);
111 */
112 
113 void attach(basis::byte_array &packed_form, basis::un_short to_attach);
115 bool detach(basis::byte_array &packed_form, basis::un_short &to_detach);
117 
119 
120 // helpful template functions for packing.
121 
123 template <class contents>
124 void pack_array(basis::byte_array &packed_form, const basis::array<contents> &to_pack) {
125  obscure_attach(packed_form, to_pack.length());
126  for (int i = 0; i < to_pack.length(); i++) to_pack[i].pack(packed_form);
127 }
128 
130 template <class contents>
131 bool unpack_array(basis::byte_array &packed_form, basis::array<contents> &to_unpack) {
132  to_unpack.reset();
133  basis::un_int len;
134  if (!obscure_detach(packed_form, len)) return false;
135  basis::array<contents> swappy_array(len, NULL_POINTER, to_unpack.flags());
136  // we create an array of the specified length to see if it's tenable.
137  if (!swappy_array.observe()) return false; // failed to allocate.
138  for (int i = 0; i < (int)len; i++) {
139  if (!swappy_array[i].unpack(packed_form))
140  return false;
141  }
142  // now that we've got exactly what we want, plunk it into the result array.
143  swappy_array.swap_contents(to_unpack);
144  return true;
145 }
146 
148 template <class contents>
150  int to_return = sizeof(int) * 2; // obscure version uses double int size.
151  for (int i = 0; i < to_pack.length(); i++)
152  to_return += to_pack[i].packed_size();
153  return to_return;
154 }
155 
157 
159 template <class contents>
160 void pack_simple(basis::byte_array &packed_form, const basis::array<contents> &to_pack) {
161  obscure_attach(packed_form, to_pack.length());
162  for (int i = 0; i < to_pack.length(); i++)
163  attach(packed_form, to_pack[i]);
164 }
165 
167 
169 template <class contents>
171  to_unpack.reset();
172  basis::un_int len;
173  if (!obscure_detach(packed_form, len)) return false;
174  basis::array<contents> swappy_array(len, NULL_POINTER, to_unpack.flags());
175  if (!swappy_array.observe()) return false; // failed to allocate.
176  for (int i = 0; i < len; i++) {
177  if (!detach(packed_form, swappy_array[i]))
178  return false;
179  }
180  swappy_array.swap_contents(to_unpack);
181  return true;
182 }
183 
184 } // namespace
185 
186 #endif
187 
Represents a sequential, ordered, contiguous collection of objects.
Definition: array.h:54
int flags() const
Provides the raw flags value, without interpreting what it means.
Definition: array.h:121
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
const contents * observe() const
Returns a pointer to the underlying C array of data.
Definition: array.h:172
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
void swap_contents(array< contents > &other)
Exchanges the contents of "this" and "other".
Definition: array.h:452
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Constants and objects used throughout HOOPLE.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
unsigned int un_int
Abbreviated name for unsigned integers.
Definition: definitions.h:62
unsigned short un_short
Abbreviated name for unsigned short integers.
Definition: definitions.h:64
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
int packed_size_array(const basis::array< contents > &to_pack)
provides space estimation for the objects to be packed.
bool unpack(basis::byte_array &packed_form, set< contents > &to_unpack)
provides a way to unpack any set that stores packable objects.
Definition: set.h:139
bool obscure_detach(byte_array &packed_form, un_int &to_detach)
shifts the number back and checks validity, false returned if corrupted.
void obscure_attach(byte_array &packed_form, un_int to_attach)
like the normal attach but shifts in some recognizable sentinel data.
const int PACKED_SIZE_BYTE
bool unpack_simple(basis::byte_array &packed_form, basis::array< contents > &to_unpack)
Unpacks flat objects from an array of bytes.
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
const int PACKED_SIZE_INT16
bool unpack_array(basis::byte_array &packed_form, basis::array< contents > &to_unpack)
provides a way to unpack any array that stores packable objects.
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
Definition: set.h:131
void pack_simple(basis::byte_array &packed_form, const basis::array< contents > &to_pack)
Packs flat objects into an array of bytes.
const int PACKED_SIZE_INT64
const int PACKED_SIZE_INT32
void pack_array(basis::byte_array &packed_form, const basis::array< contents > &to_pack)
provides a way to pack any array that stores packable objects.
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".
int packed_size(const byte_array &packed_form)
Reports the size required to pack a byte array into a byte array.