byte_array.h

Go to the documentation of this file.
00001 #ifndef BYTE_ARRAY_CLASS
00002 #define BYTE_ARRAY_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : byte_array                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "array.h"
00019 #include "base_string.h"
00020 #include "contracts.h"
00021 #include "definitions.h"
00022 
00023 #include <string.h>  // for memcmp.
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   // these implement the orderable and equalizable interfaces.
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 // the two templates below can be used to add or remove objects from an array
00110 // of bytes.  NOTE: the functions below will only work with objects that are
00111 // already platform-independent.  it's better to make structures packable by
00112 // using the attach and detach functions in the "packable" library.
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 } // namespace.
00130 
00131 #endif
00132 
Generated on Sat Jan 28 04:22:11 2012 for hoople2 project by  doxygen 1.6.3