byte_array.h
Go to the documentation of this file.00001 #ifndef BYTE_ARRAY_CLASS
00002 #define BYTE_ARRAY_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "array.h"
00019 #include "base_string.h"
00020 #include "contracts.h"
00021 #include "definitions.h"
00022
00023 #include <string.h>
00024
00025 namespace basis {
00026
00028
00035 class byte_array : public array<abyte>, public virtual orderable
00036 {
00037 public:
00038 byte_array(int number = 0, const abyte *initial_contents = NIL)
00039 : array<abyte>(number, initial_contents, SIMPLE_COPY | EXPONE) {}
00041
00042 byte_array(const byte_array &to_copy)
00043 : root_object(), array<abyte>(to_copy) {}
00045
00046 byte_array(const array<abyte> &to_copy) : array<abyte>(to_copy) {}
00048
00049 virtual ~byte_array() {}
00050
00051 DEFINE_CLASS_NAME("byte_array");
00052
00054
00057 static const byte_array &empty_array()
00058 { static byte_array g_empty; return g_empty; }
00059
00060
00061 virtual bool equal_to(const equalizable &s2) const {
00062 const byte_array *s2_cast = dynamic_cast<const byte_array *>(&s2);
00063 if (!s2_cast) throw "error: byte_array::==: unknown type";
00064 return comparator(*s2_cast) == 0;
00065 }
00066 virtual bool less_than(const orderable &s2) const {
00067 const byte_array *s2_cast = dynamic_cast<const byte_array *>(&s2);
00068 if (!s2_cast) throw "error: byte_array::<: unknown type";
00069 return comparator(*s2_cast) < 0;
00070 }
00071
00072 int comparator(const byte_array &s2) const {
00073 return memcmp(observe(), s2.observe(), length());
00074 }
00075 };
00076
00078
00080
00086 class packable : public virtual root_object
00087 {
00088 public:
00089 virtual void pack(byte_array &packed_form) const = 0;
00091
00094 virtual bool unpack(byte_array &packed_form) = 0;
00096
00103 virtual int packed_size() const = 0;
00105 };
00106
00108
00109
00110
00111
00112
00113
00115 template <class contents>
00116 void attach_flat(byte_array &target, const contents &attachment)
00117 { target.concatenate(byte_array(sizeof(attachment), (abyte *)&attachment)); }
00118
00120 template <class contents>
00121 bool detach_flat(byte_array &source, contents &detached)
00122 {
00123 if (sizeof(detached) > source.length()) return false;
00124 detached = *(contents *)source.observe();
00125 source.zap(0, sizeof(detached) - 1);
00126 return true;
00127 }
00128
00129 }
00130
00131 #endif
00132