cel_blob.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_blob_h_
00009 #define _cel_blob_h_
00010 
00011 #include "cel_types.h"
00012 #include "cel_datatraits.h"
00013 #include "cel_string.h"
00014 #include "cel_misc.h"
00015 #include <cstring>
00016 
00017 namespace Celartem
00018 {
00027     template<size_t _SIZE, bool NEED_SECURE_CLEANUP = false> struct Blob
00028     {
00032         enum {SIZE = _SIZE};
00033         
00034         // trick for reducing compilation error
00035         static const size_t SIZE_NO_WARN = SIZE ? SIZE : 1;
00036 
00040         u8 bin[SIZE_NO_WARN];
00041         
00045         Blob()
00046         {
00047             if(SIZE)
00048                 std::memset(bin, 0, SIZE_NO_WARN);
00049         }
00050         
00058         Blob(const u8* inBin)
00059         {
00060             if(SIZE)
00061                 std::memcpy(bin, inBin, SIZE_NO_WARN);
00062         }
00063         
00069         Blob(const Blob& inBin)
00070         {
00071             if(SIZE)
00072                 std::memcpy(bin, inBin.bin, SIZE_NO_WARN);
00073         }
00074         
00078         ~Blob()
00079         {
00080             // FIXME: we had better use some optimization-proof zeromemory here
00081             if(NEED_SECURE_CLEANUP)
00082                 clear();
00083         }
00084         
00092         Blob& operator=(const u8* inBin)
00093         {
00094             if(SIZE)
00095                 std::memcpy(bin, inBin, SIZE_NO_WARN);
00096             return *this;
00097         }
00098 
00104         Blob& operator=(const Blob& inBin)
00105         {
00106             if(SIZE)
00107                 std::memcpy(bin, inBin.bin, SIZE_NO_WARN);
00108             return *this;
00109         }
00110         
00114         operator const u8*() const
00115         {
00116             return bin;
00117         }
00118 
00122         operator u8*()
00123         {
00124             return bin;
00125         }
00126         
00130         size_t getSize() const
00131         {
00132             return SIZE;
00133         }
00134 
00138         void clear()
00139         {
00140             if(SIZE)
00141                 std::memset(bin, 0, SIZE_NO_WARN);
00142         }
00143 
00149         bool isAllZero() const
00150         {
00151             for(size_t i = 0; i < SIZE; i++)
00152                 if(bin[i] != 0)
00153                     return false;
00154             return true;
00155         }
00156 
00162         bool operator==(const Blob& inBin) const
00163         {
00164             return compare(inBin) == 0;
00165         }
00166 
00172         bool operator!=(const Blob& inBin) const
00173         {
00174             return compare(inBin) != 0;
00175         }
00176         
00183         bool operator<=(const Blob& inBin) const
00184         {
00185             return compare(inBin) <= 0;
00186         }
00187         
00194         bool operator<(const Blob& inBin) const
00195         {
00196             return compare(inBin) < 0;
00197         }
00198 
00205         bool operator>=(const Blob& inBin) const
00206         {
00207             return compare(inBin) >= 0;
00208         }
00209     
00216         bool operator>(const Blob& inBin) const
00217         {
00218             return compare(inBin) > 0;
00219         }
00220 
00228         int compare(const Blob& inBin) const
00229         {
00230             if(SIZE)
00231                 return std::memcmp(bin, inBin.bin, SIZE_NO_WARN);
00232             return 0;
00233         }
00234     };
00235     
00239     template<size_t SIZE> struct DataTraits<Blob<SIZE> >
00240     {
00241         static const CopyPolicy copyPolicy = byMemcpy;
00242         static const StoragePolicy storagePolicy = byCopyBytes;
00243         static const bool isPOD = false;
00244     };
00245 }
00246 
00247 #endif // _cel_blob_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:54 2008.