feisty meow concerns codebase  2.140
byte_array.h
Go to the documentation of this file.
1 #ifndef BYTE_ARRAY_CLASS
2 #define BYTE_ARRAY_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : byte_array *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1991-$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 "array.h"
19 #include "base_string.h"
20 #include "contracts.h"
21 #include "definitions.h"
22 
23 #include <string.h> // for memcmp.
24 
25 namespace basis {
26 
28 
35 class byte_array : public array<abyte>, public virtual orderable
36 {
37 public:
38  byte_array(int number = 0, const abyte *initial_contents = NULL_POINTER)
39  : array<abyte>(number, initial_contents, SIMPLE_COPY | EXPONE) {}
41 
42  byte_array(const byte_array &to_copy)
43  : root_object(), array<abyte>(to_copy) {}
45 
46  byte_array(const array<abyte> &to_copy) : array<abyte>(to_copy) {}
48 
49  virtual ~byte_array() {}
50 
51  DEFINE_CLASS_NAME("byte_array");
52 
54 
57  static const byte_array &empty_array()
58  { static byte_array g_empty; return g_empty; }
59 
60  // these implement the orderable and equalizable interfaces.
61  virtual bool equal_to(const equalizable &s2) const {
62  const byte_array *s2_cast = dynamic_cast<const byte_array *>(&s2);
63  if (!s2_cast) throw "error: byte_array::==: unknown type";
64  return comparator(*s2_cast) == 0;
65  }
66  virtual bool less_than(const orderable &s2) const {
67  const byte_array *s2_cast = dynamic_cast<const byte_array *>(&s2);
68  if (!s2_cast) throw "error: byte_array::<: unknown type";
69  return comparator(*s2_cast) < 0;
70  }
71 
72  int comparator(const byte_array &s2) const {
73  return memcmp(observe(), s2.observe(), length());
74  }
75 };
76 
78 
80 
86 class packable : public virtual root_object
87 {
88 public:
89  virtual void pack(byte_array &packed_form) const = 0;
91 
94  virtual bool unpack(byte_array &packed_form) = 0;
96 
103  virtual int packed_size() const = 0;
105 };
106 
108 
109 // the two templates below can be used to add or remove objects from an array
110 // of bytes. NOTE: the functions below will only work with objects that are
111 // already platform-independent. it's better to make structures packable by
112 // using the attach and detach functions in the "packable" library.
113 
115 template <class contents>
116 void attach_flat(byte_array &target, const contents &attachment)
117 { target.concatenate(byte_array(sizeof(attachment), (abyte *)&attachment)); }
118 
120 template <class contents>
121 bool detach_flat(byte_array &source, contents &detached)
122 {
123  if (sizeof(detached) > source.length()) return false;
124  detached = *(contents *)source.observe();
125  source.zap(0, sizeof(detached) - 1);
126  return true;
127 }
128 
129 } // namespace.
130 
131 #endif
132 
Represents a sequential, ordered, contiguous collection of objects.
Definition: array.h:54
@ EXPONE
synonym for EXPONENTIAL_GROWTH.
Definition: array.h:61
@ SIMPLE_COPY
the contents can be memcpy'd and are not deep.
Definition: array.h:59
const abyte * observe() const
Returns a pointer to the underlying C array of data.
Definition: array.h:172
array & concatenate(const array &to_concatenate)
Appends the array "to_concatenate" onto "this" and returns "this".
Definition: array.h:379
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
Definition: array.h:769
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
byte_array(const byte_array &to_copy)
constructs an array bytes by copying the "to_copy" array.
Definition: byte_array.h:42
DEFINE_CLASS_NAME("byte_array")
returns an array of zero bytes.
static const byte_array & empty_array()
Definition: byte_array.h:57
virtual ~byte_array()
Definition: byte_array.h:49
virtual bool less_than(const orderable &s2) const
Definition: byte_array.h:66
int comparator(const byte_array &s2) const
Definition: byte_array.h:72
byte_array(int number=0, const abyte *initial_contents=NULL_POINTER)
constructs an array of "number" bytes from "initial_contents".
Definition: byte_array.h:38
virtual bool equal_to(const equalizable &s2) const
Definition: byte_array.h:61
byte_array(const array< abyte > &to_copy)
constructs an array bytes by copying the "to_copy" array.
Definition: byte_array.h:46
Base class for object that can tell itself apart from other instances.
Definition: contracts.h:44
A base for objects that can be alphabetically (lexicographically) ordered.
Definition: contracts.h:57
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
virtual int packed_size() const =0
Estimates the space needed for the packed structure.
virtual bool unpack(byte_array &packed_form)=0
Restores the packable from the "packed_form".
virtual void pack(byte_array &packed_form) const =0
Creates a packed form of the packable object in "packed_form".
Constants and objects used throughout HOOPLE.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
bool detach_flat(byte_array &source, contents &detached)
detach_flat() pulls the "detached" object out of the array of bytes.
Definition: byte_array.h:121
void attach_flat(byte_array &target, const contents &attachment)
attach_flat() places a copy of "attachment" onto the array of bytes.
Definition: byte_array.h:116