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.cpp"
00019 #include "object_base.h"
00020 
00021 #include <memory.h>
00022 
00024 
00031 class byte_array : public array<byte>, public virtual object_base
00032 {
00033 public:
00034   inline byte_array(int number = 0, const byte *initial_contents = NIL)
00035       : array<byte>(number, initial_contents, SIMPLE_COPY | EXPONE) {}
00037   byte_array(const byte_array &to_copy);
00039   inline byte_array(const array<byte> &to_copy) : array<byte>(to_copy) {}
00041 
00042   IMPLEMENT_CLASS_NAME("byte_array");
00043 
00044   static const byte_array &empty_array();
00046 
00050 
00051   bool operator == (const byte_array &to_compare) const {
00052     if (length() != to_compare.length()) return false;
00053     return !memcmp(observe(), to_compare.observe(), length());
00054   }
00056   bool operator != (const byte_array &tc) const { return !(*this == tc); }
00057 };
00058 
00060 
00061 // the two templates below can be used to add or remove objects from an array
00062 // of bytes.  NOTE: the functions below will only work with objects that are
00063 // already platform independent.  it's better to make structures packable by
00064 // using the attach and detach functions in the packable header.
00065 
00067 template <class contents>
00068 void attach_flat(byte_array &target, const contents &attachment)
00069 { target.concatenate(byte_array(sizeof(attachment), (byte *)&attachment)); }
00070 
00072 template <class contents>
00073 bool detach_flat(byte_array &source, contents &detached)
00074 {
00075   if (sizeof(detached) > source.length()) return false;
00076   detached = *(contents *)source.observe();
00077   source.zap(0, sizeof(detached) - 1);
00078   return true;
00079 }
00080 
00081 #endif
00082 

Generated on Tue Aug 19 04:29:32 2008 for HOOPLE Libraries by  doxygen 1.5.1