57 template <
class contents>
79 void resize(
int new_maximum = 0);
104 const contents *
get(
int field)
const;
108 contents *
borrow(
int field);
111 const contents *
operator [] (
int field)
const {
return get(field); }
174 void check_fields(
const char *where)
const;
178 void set_nil(
int start,
int end);
187 amorph &operator = (
const amorph &to_copy) {
return *
this; }
201 template <
class contents>
203 const amorph<contents> &to_copy);
214 template <
class contents>
218 template <
class contents>
222 template <
class contents>
230 #undef static_class_name
231 #define static_class_name() "amorph"
237 #define AMO_ALERT(a, b, c) basis::throw_error(basis::astring(a), basis::astring(func), basis::astring(b) + " -- " + c)
238 #define CHECK_FIELDS check_fields(func)
240 #define AMO_ALERT(a1, a2, a3) {}
241 #define CHECK_FIELDS { if (!func) {} }
246 template <
class contents>
249 |
basis::array<contents *>::EXPONE |
basis::array<contents *>::FLUSH_INVISIBLE),
257 template <
class contents>
265 template <
class contents>
268 for (
int i = start; i <= end; i++)
272 template <
class contents>
273 void amorph<contents>::check_fields(
const char *where)
const
277 for (
int i = 0; i < elements(); i++)
279 if (_fields_used != counter) {
281 "error in _fields_used count");
285 template <
class contents>
294 template <
class contents>
299 for (
int i = 0; i < elements(); i++) clear(i);
302 template <
class contents>
307 adjust(elements() + 1);
308 return put(elements() - 1, (contents *)data);
311 template <
class contents>
320 template <
class contents>
325 if (new_maximum < 0)
return;
326 int old_max = elements();
327 if (new_maximum == old_max)
return;
328 if (new_maximum < old_max) {
330 zap(new_maximum, old_max - 1);
336 int new_fields = new_maximum - old_max;
339 for (
int i = old_max; i < new_maximum; i++) {
344 template <
class contents>
349 bounds_return(position, 0, elements(), basis::common::OUT_OF_RANGE);
351 if (o != basis::common::OKAY)
return basis::common::OUT_OF_RANGE;
352 set_nil(position, position + lines_to_add - 1);
353 return basis::common::OKAY;
356 template <
class contents>
361 bounds_return(start_index, 0, elements() - 1, basis::common::OUT_OF_RANGE);
362 bounds_return(end_index, 0, elements() - 1, basis::common::OUT_OF_RANGE);
363 if (end_index < start_index)
return basis::common::OKAY;
364 for (
int i = start_index; i <= end_index; i++) clear(i);
366 return (o == basis::common::OKAY? basis::common::OKAY : basis::common::OUT_OF_RANGE);
369 template <
class contents>
374 bounds_return(field, 0, elements() - 1, basis::common::OUT_OF_RANGE);
375 contents *to_whack = acquire(field);
381 return basis::common::OKAY;
384 template <
class contents>
389 if (_fields_used == elements()) { o = basis::common::IS_FULL;
return 0; }
390 for (
int i = 0; i < elements(); i++)
392 AMO_ALERT(
"amorph",
"empty",
"_fields_used is incorrect");
393 return basis::common::IS_FULL;
396 template <
class contents>
402 for (
int i = field; i < elements(); i++)
410 template <
class contents>
418 template <
class contents>
423 contents *to_return = borrow(field);
431 template <
class contents>
436 if (!_fields_used) { o = basis::common::NOT_FOUND;
return 0; }
437 for (
int i = 0; i < elements(); i++) {
439 o = basis::common::OKAY;
443 o = basis::common::NOT_FOUND;
447 template <
class contents>
456 template <
class contents>
461 int hold_fields = _fields_used;
462 _fields_used = other._fields_used;
463 other._fields_used = hold_fields;
467 template <
class contents>
471 for (
int i = 0; i < to_pack.
elements(); i++) {
472 const contents *current = to_pack.
get(i);
473 if (current) parts_size += current->packed_size();
478 template <
class contents>
482 for (
int i = 0; i < to_pack.
elements(); i++) {
483 const contents *current = to_pack.
get(i);
484 if (current) current->pack(packed_form);
488 template <
class contents>
494 for (
int i = 0; i < elem; i++) {
495 contents *to_add =
new contents;
496 if (!to_add->unpack(packed_form)) {
delete to_add;
return false; }
502 template <
class contents>
506 if (&to_assign == &to_copy)
return;
512 for (
int i = 0; i < to_assign.
elements(); i++) {
513 if (to_copy.
get(i)) to_assign.
put(i,
new contents(*to_copy.
get(i)));
518 #undef static_class_name
#define AMO_ALERT(a1, a2, a3)
a_sprintf is a specialization of astring that provides printf style support.
Represents a sequential, ordered, contiguous collection of objects.
outcome put(int index, const contents &to_put)
Stores an object at the index "index" in the array.
outcome insert(int index, int new_indices)
Adds "new_indices" new positions for objects into the array at "index".
const contents * observe() const
Returns a pointer to the underlying C array of data.
const contents & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
contents * access()
A non-constant access of the underlying C-array. BE REALLY CAREFUL.
int length() const
Returns the current reported length of the allocated C array.
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
void swap_contents(array< contents > &other)
Exchanges the contents of "this" and "other".
A very common template for a dynamic array of bytes.
Outcomes describe the state of completion for an operation.
const contents * operator[](int field) const
synonym for get.
int elements() const
the maximum number of elements currently allowed in this amorph.
void resize(int new_maximum=0)
like adjust but doesn't keep existing contents.
basis::outcome put(int field, const contents *data)
Enters an object into the field at index "field" in the amorph.
basis::outcome operator+=(const contents *data)
a synonym for append.
amorph(int elements=0)
constructs an amorph capable of holding "elements" pointers.
basis::outcome append(const contents *data)
puts "data" on the end of this amorph.
basis::outcome zap(int start, int end)
Removes a range of indices from the amorph.
void reset()
cleans out all of the contents.
void swap_contents(amorph< contents > &other)
Exchanges the contents of "this" and "other".
void adjust(int new_max)
Changes the maximum number of elements for this amorph.
int find(const contents *to_locate, basis::outcome &o)
Searches the amorph for the contents specified.
const contents * get(int field) const
Returns a constant pointer to the information at the index "field".
int find_empty(basis::outcome &o) const
Returns the index of a free field if there are any.
const contents * next_valid(int &field) const
Returns the contents of the next valid element at or after "field".
basis::outcome insert(int position, int lines_to_add)
Adds "lines_to_add" indices to the amorph at the index "position".
contents * acquire(int field)
Retrieves a "field" from the amorph, taking responsibility for it back.
basis::outcome clear(int field)
Clears the contents of the field specified.
contents * borrow(int field)
Returns a pointer to the information at the index "field".
int valid_fields() const
Returns the number of fields that have non-null contents.
void clear_all()
Clears every field in the amorph.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
#define bounds_return(value, low, high, to_return)
Verifies that "value" is between "low" and "high", inclusive.
The guards collection helps in testing preconditions and reporting errors.
A dynamic container class that holds any kind of object via pointers.
void amorph_pack(basis::byte_array &packed_form, const amorph< contents > &to_pack)
support for packing an amorph into an array of bytes.
int amorph_packed_size(const amorph< contents > &to_pack)
reports how large the packed form will be.
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
void amorph_assign(amorph< contents > &to_assign, const amorph< contents > &to_copy)
This can be used when the templated object has a copy constructor.
const int PACKED_SIZE_INT32
bool amorph_unpack(basis::byte_array &packed_form, amorph< contents > &to_unpack)
unpacks the amorph from an array of bytes.
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".