• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

crypt/record.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //
00003 // $Id: record.h 3342 2010-09-17 18:32:00Z java $
00004 //
00005 // Copyright (C) 2008-2010  Rafael Ostertag
00006 //
00007 // This file is part of YAPET.
00008 //
00009 // YAPET is free software: you can redistribute it and/or modify it under the
00010 // terms of the GNU General Public License as published by the Free Software
00011 // Foundation, either version 3 of the License, or (at your option) any later
00012 // version.
00013 //
00014 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00015 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00016 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00017 // details.
00018 //
00019 // You should have received a copy of the GNU General Public License along with
00020 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Additional permission under GNU GPL version 3 section 7
00023 //
00024 // If you modify this program, or any covered work, by linking or combining it
00025 // with the OpenSSL project's OpenSSL library (or a modified version of that
00026 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00027 // licenses, Rafael Ostertag grants you additional permission to convey the
00028 // resulting work.  Corresponding Source for a non-source form of such a
00029 // combination shall include the source code for the parts of OpenSSL used as
00030 // well as that of the covered work.
00031 //
00032 
00033 #ifndef _RECORD_H
00034 #define _RECORD_H
00035 
00036 #ifdef HAVE_CONFIG_H
00037 # include <config.h>
00038 #endif
00039 
00040 #ifdef HAVE_INTTYPES_H
00041 # include <inttypes.h>
00042 #endif
00043 
00044 #ifdef HAVE_STDLIB_H
00045 # include <stdlib.h>
00046 #endif
00047 
00048 #ifdef HAVE_STRING_H
00049 # include <string.h>
00050 #endif
00051 
00052 #include "../intl.h"
00053 
00054 #include "bdbuffer.h"
00055 #include "yapetexception.h"
00056 
00057 namespace YAPET {
00058 
00070     template<class T>
00071     class Record {
00072         private:
00079             uint32_t _size;
00085             T* data;
00086 
00093             void alloc_mem() throw (YAPETException) {
00094                 data = (T*) malloc (sizeof (T) );
00095 
00096                 if (data == NULL)
00097                     throw YAPETException (_ ("Memory exhausted") );
00098 
00099                 _size = sizeof (T);
00100             }
00101 
00107             void free_mem() {
00108                 memset (data, 0, _size);
00109                 free (data);
00110             }
00111 
00112         public:
00122             Record<T> (const T& d) throw (YAPETException) {
00123                 alloc_mem();
00124                 memcpy (data, &d, sizeof (T) );
00125             }
00126 
00133             Record<T>() throw (YAPETException) {
00134                 alloc_mem();
00135             }
00136 
00137             Record<T> (const Record<T>& r) throw (YAPETException) {
00138                 alloc_mem();
00139                 memcpy (data, r.data, _size);
00140             }
00141 
00142             virtual ~Record<T>() {
00143                 free_mem();
00144             }
00145 
00151             uint32_t size() const {
00152                 return _size;
00153             }
00154 
00162             T* getData() {
00163                 return data;
00164             }
00172             const T* getData() const {
00173                 return data;
00174             }
00175 
00183             operator T*() {
00184                 return data;
00185             }
00193             operator const T*() const {
00194                 return data;
00195             }
00204             operator uint8_t*() {
00205                 return (uint8_t*) data;
00206             }
00215             operator const uint8_t*() const {
00216                 return (const uint8_t*) data;
00217             }
00218 
00228             const Record<T>& operator= (const Record<T>& r)
00229             throw (YAPETException) {
00230                 if (this == &r) return *this;
00231 
00232                 free_mem();
00233                 // This sets _size member too
00234                 alloc_mem();
00235                 memcpy (data, r.data, r._size);
00236                 return *this;
00237             }
00238 
00248             const Record<T>& operator= (const T& r) throw (YAPETException) {
00249                 free_mem();
00250                 // This sets _size member too
00251                 alloc_mem();
00252                 memcpy (data, &r, _size);
00253                 return *this;
00254             }
00255 
00265             const Record<T>& operator= (const T* r) throw (YAPETException) {
00266                 free_mem();
00267                 // This sets _size member too
00268                 alloc_mem();
00269                 memcpy (data, r, _size);
00270                 return *this;
00271             }
00272 
00286             const Record<T>& operator= (const BDBuffer& bdb)
00287             throw (YAPETException) {
00288         // As of version 0.6, the exnum is used to determine whether to use a 32 or 64 bit header
00289                 if (bdb.size() < _size)
00290                     throw YAPETException (_ ("BDBuffer too small. Cannot assign to Record<T>"), BDBUFFER_TOO_SMALL );
00291         if (bdb.size() > _size)
00292             throw YAPETException (_ ("BDBuffer too big. Cannot assign to Record<T>"), BDBUFFER_TOO_BIG );
00293 
00294                 free_mem();
00295                 // This sets _size member too
00296                 alloc_mem();
00297                 memcpy (data, bdb(), _size);
00298                 return *this;
00299             }
00300     };
00301 }
00302 
00303 #endif // _RECORD_H

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1