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:
00041                 static ibrcommon::Semaphore _filelimit;
00042 
00046                 static std::ostream& copy(std::ostream &output, std::istream &input, const size_t size, const size_t buffer_size = 0x1000);
00047 
00048                 virtual ~BLOB();
00049 
00054                 virtual void clear() = 0;
00055 
00056                 virtual void open() = 0;
00057                 virtual void close() = 0;
00058 
00059                 class iostream
00060                 {
00061                 private:
00062                         BLOB *_blob;
00063                         ibrcommon::MutexLock lock;
00064                         std::iostream &_stream;
00065 
00066                 public:
00067                         iostream(BLOB *blob)
00068                          : _blob(blob), lock(*blob), _stream(blob->__get_stream())
00069                         {
00070                                 _blob->open();
00071                         }
00072 
00073                         virtual ~iostream()
00074                         {
00075                                 _blob->close();
00076                         };
00077 
00078                         std::iostream* operator->() { return &(_blob->__get_stream()); };
00079                         std::iostream& operator*() { return _blob->__get_stream(); };
00080 
00081                         size_t size()
00082                         {
00083                                 return _blob->__get_size();
00084                         };
00085 
00086                         void clear()
00087                         {
00088                                 _blob->clear();
00089                         }
00090                 };
00091 
00092                 class Reference : public Mutex
00093                 {
00094                 public:
00095                         Reference(BLOB *blob);
00096                         Reference(const Reference &ref);
00097                         virtual ~Reference();
00098 
00103                         std::iostream& operator*() __attribute__ ((deprecated));
00104                         BLOB::iostream iostream();
00105 
00110                         virtual void enter() throw (ibrcommon::MutexException);
00111 
00115                         virtual void leave() throw (ibrcommon::MutexException);
00116 
00120                         virtual void trylock() throw (ibrcommon::MutexException);
00121 
00126                         void clear() __attribute__ ((deprecated));
00127 
00132                         size_t getSize() const __attribute__ ((deprecated));
00133 
00138                         const BLOB* getPointer() const;
00139 
00140                 private:
00141                         BLOB *_blob;
00142                 };
00143 
00150                 class Provider
00151                 {
00152                 public:
00156                         virtual ~Provider() = 0;
00157 
00162                         virtual BLOB::Reference create() = 0;
00163                 };
00164 
00169                 static ibrcommon::BLOB::Reference create();
00170 
00175                 static ibrcommon::BLOB::Reference open(const ibrcommon::File &f);
00176 
00180                 static void changeProvider(BLOB::Provider *p, bool auto_delete = false);
00181 
00182         protected:
00183                 class ProviderRef
00184                 {
00185                 public:
00186                         ProviderRef(Provider *provider, bool auto_delete);
00187                         virtual ~ProviderRef();
00188 
00189                         void change(Provider *p, bool auto_delete = true);
00190 
00191                         BLOB::Reference create();
00192 
00193                 private:
00194                         Provider *_provider;
00195                         bool _auto_delete;
00196                 };
00197 
00201                 static ProviderRef provider;
00202 
00203                 BLOB();
00204                 size_t _refcount;
00205                 Mutex _reflock;
00206 
00207                 virtual size_t __get_size() = 0;
00208                 virtual std::iostream &__get_stream() = 0;
00209 
00210         private:
00211                 BLOB(const BLOB &ref); // forbidden copy constructor
00212         };
00213 
00218         class FileBLOB : public ibrcommon::BLOB
00219         {
00220         public:
00221                 FileBLOB(const ibrcommon::File &f);
00222                 virtual ~FileBLOB();
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                 std::fstream _filestream;
00239                 File _file;
00240         };
00241 
00242         class MemoryBLOBProvider : public ibrcommon::BLOB::Provider
00243         {
00244         public:
00245                 MemoryBLOBProvider();
00246                 virtual ~MemoryBLOBProvider();
00247                 BLOB::Reference create();
00248 
00249         private:
00254                 class StringBLOB : public BLOB
00255                 {
00256                 public:
00257                         static BLOB::Reference create();
00258                         virtual ~StringBLOB();
00259 
00260                         virtual void clear();
00261 
00262                         virtual void open();
00263                         virtual void close();
00264 
00265                 protected:
00266                         std::iostream &__get_stream()
00267                         {
00268                                 return _stringstream;
00269                         }
00270 
00271                         size_t __get_size();
00272 
00273                 private:
00274                         StringBLOB();
00275                         std::stringstream _stringstream;
00276                 };
00277         };
00278 
00279         class FileBLOBProvider : public ibrcommon::BLOB::Provider
00280         {
00281         public:
00282                 FileBLOBProvider(const File &path);
00283                 virtual ~FileBLOBProvider();
00284                 BLOB::Reference create();
00285 
00286         private:
00287                 ibrcommon::File _tmppath;
00288 
00294                 class TmpFileBLOB : public BLOB
00295                 {
00296                 public:
00297                         TmpFileBLOB(const File &path);
00298                         virtual ~TmpFileBLOB();
00299 
00300                         virtual void clear();
00301 
00302                         virtual void open();
00303                         virtual void close();
00304 
00305                 protected:
00306                         std::iostream &__get_stream()
00307                         {
00308                                 return _filestream;
00309                         }
00310 
00311                         size_t __get_size();
00312 
00313                 private:
00314                         std::fstream _filestream;
00315                         int _fd;
00316                         File _tmpfile;
00317                 };
00318         };
00319 }
00320 
00321 #endif /* BLOB_H_ */