IBR-DTNSuite 0.6

ibrcommon/ibrcommon/data/BLOB.h

Go to the documentation of this file.
00001 /*
00002  * BLOB.h
00003  *
00004  *  Created on: 15.12.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef BLOB_H_
00009 #define BLOB_H_
00010 
00011 #include "ibrcommon/thread/Mutex.h"
00012 #include "ibrcommon/thread/MutexLock.h"
00013 #include "ibrcommon/data/File.h"
00014 #include "ibrcommon/thread/Semaphore.h"
00015 #include <iostream>
00016 #include <sstream>
00017 #include <fstream>
00018 
00019 namespace ibrcommon
00020 {
00021         class CanNotOpenFileException : public ibrcommon::IOException
00022         {
00023         public:
00024                 CanNotOpenFileException(ibrcommon::File f) throw() : IOException("Could not open file " + f.getPath() + ".")
00025                 {
00026                 };
00027         };
00028 
00029         class BLOB : public Mutex
00030         {
00031         private:
00035                 bool _locked;
00036 
00037         public:
00042                 static File tmppath;
00043 
00047                 static ibrcommon::Semaphore _filelimit;
00048 
00052                 static std::ostream& copy(std::ostream &output, std::istream &input, const size_t size, const size_t buffer_size = 0x1000);
00053 
00054                 virtual ~BLOB();
00055 
00060                 virtual void clear() = 0;
00061 
00062                 virtual void open() = 0;
00063                 virtual void close() = 0;
00064 
00065                 class iostream
00066                 {
00067                 private:
00068                         BLOB *_blob;
00069                         ibrcommon::MutexLock lock;
00070                         std::iostream &_stream;
00071 
00072                 public:
00073                         iostream(BLOB *blob)
00074                          : _blob(blob), lock(*blob), _stream(blob->__get_stream())
00075                         {
00076                                 _blob->open();
00077                         }
00078 
00079                         virtual ~iostream()
00080                         {
00081                                 _blob->close();
00082                         };
00083 
00084                         std::iostream* operator->() { return &(_blob->__get_stream()); };
00085                         std::iostream& operator*() { return _blob->__get_stream(); };
00086 
00087                         size_t size()
00088                         {
00089                                 return _blob->__get_size();
00090                         };
00091 
00092                         void clear()
00093                         {
00094                                 _blob->clear();
00095                         }
00096                 };
00097 
00098                 class Reference : public Mutex
00099                 {
00100                 public:
00101                         Reference(BLOB *blob);
00102                         Reference(const Reference &ref);
00103                         virtual ~Reference();
00104 
00109                         std::iostream& operator*() __attribute__ ((deprecated));
00110                         BLOB::iostream iostream();
00111 
00116                         virtual void enter() throw (ibrcommon::MutexException);
00117 
00121                         virtual void leave() throw (ibrcommon::MutexException);
00122 
00126                         virtual void trylock() throw (ibrcommon::MutexException);
00127 
00132                         void clear() __attribute__ ((deprecated));
00133 
00138                         size_t getSize() const __attribute__ ((deprecated));
00139 
00140                 private:
00141                         BLOB *_blob;
00142                 };
00143 
00144         protected:
00145                 BLOB();
00146                 size_t _refcount;
00147                 Mutex _reflock;
00148 
00149                 virtual size_t __get_size() = 0;
00150                 virtual std::iostream &__get_stream() = 0;
00151 
00152         private:
00153                 BLOB(const BLOB &ref); // forbidden copy constructor
00154         };
00155 
00160         class StringBLOB : public BLOB
00161         {
00162         public:
00163                 static BLOB::Reference create();
00164                 virtual ~StringBLOB();
00165 
00166                 virtual void clear();
00167 
00168                 virtual void open();
00169                 virtual void close();
00170 
00171         protected:
00172                 std::iostream &__get_stream()
00173                 {
00174                         return _stringstream;
00175                 }
00176 
00177                 size_t __get_size();
00178 
00179         private:
00180                 StringBLOB();
00181                 std::stringstream _stringstream;
00182         };
00183 
00188         class FileBLOB : public BLOB
00189         {
00190         public:
00191                 static BLOB::Reference create(const File &f);
00192                 virtual ~FileBLOB();
00193 
00194                 virtual void clear();
00195 
00196                 virtual void open();
00197                 virtual void close();
00198 
00199         protected:
00200                 std::iostream &__get_stream()
00201                 {
00202                         return _filestream;
00203                 }
00204 
00205                 size_t __get_size();
00206 
00207         private:
00208                 FileBLOB(const File &f);
00209                 std::fstream _filestream;
00210                 File _file;
00211         };
00212 
00218         class TmpFileBLOB : public BLOB
00219         {
00220         public:
00221                 static BLOB::Reference create();
00222                 virtual ~TmpFileBLOB();
00223 
00224                 virtual void clear();
00225 
00226                 virtual void open();
00227                 virtual void close();
00228 
00229         protected:
00230                 std::iostream &__get_stream()
00231                 {
00232                         return _filestream;
00233                 }
00234 
00235                 size_t __get_size();
00236 
00237         private:
00238                 TmpFileBLOB();
00239                 void createtmpfile();
00240                 std::fstream _filestream;
00241                 int _fd;
00242                 File _tmpfile;
00243         };
00244 }
00245 
00246 #endif /* BLOB_H_ */