IBR-DTNSuite  0.8
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 "ibrcommon/refcnt_ptr.h"
00016 #include <iostream>
00017 #include <sstream>
00018 #include <fstream>
00019 
00020 namespace ibrcommon
00021 {
00022         class CanNotOpenFileException : public ibrcommon::IOException
00023         {
00024         public:
00025                 CanNotOpenFileException(ibrcommon::File f) throw() : IOException("Could not open file " + f.getPath() + ".")
00026                 {
00027                 };
00028         };
00029 
00030         class BLOB : public Mutex
00031         {
00032         public:
00036                 static ibrcommon::Semaphore _filelimit;
00037 
00041                 static std::ostream& copy(std::ostream &output, std::istream &input, const size_t size, const size_t buffer_size = 0x1000);
00042 
00043                 virtual ~BLOB();
00044 
00049                 virtual void clear() = 0;
00050 
00051                 virtual void open() = 0;
00052                 virtual void close() = 0;
00053 
00054                 class iostream
00055                 {
00056                 private:
00057                         BLOB &_blob;
00058                         ibrcommon::MutexLock lock;
00059                         std::iostream &_stream;
00060 
00061                 public:
00062                         iostream(BLOB &blob)
00063                          : _blob(blob), lock(blob), _stream(blob.__get_stream())
00064                         {
00065                                 _blob.open();
00066                         }
00067 
00068                         virtual ~iostream()
00069                         {
00070                                 _blob.close();
00071                         };
00072 
00073                         std::iostream* operator->() { return &(_blob.__get_stream()); };
00074                         std::iostream& operator*() { return _blob.__get_stream(); };
00075 
00076                         size_t size()
00077                         {
00078                                 return _blob.__get_size();
00079                         };
00080 
00081                         void clear()
00082                         {
00083                                 _blob.clear();
00084                         }
00085                 };
00086 
00087                 class Reference
00088                 {
00089                 public:
00090                         Reference(BLOB *blob);
00091                         Reference(const Reference &ref);
00092                         virtual ~Reference();
00093 
00098                         BLOB::iostream iostream();
00099 
00104                         const BLOB& operator*() const;
00105 
00106                 private:
00107                         refcnt_ptr<BLOB> _blob;
00108                 };
00109 
00116                 class Provider
00117                 {
00118                 public:
00122                         virtual ~Provider() = 0;
00123 
00128                         virtual BLOB::Reference create() = 0;
00129                 };
00130 
00135                 static ibrcommon::BLOB::Reference create();
00136 
00141                 static ibrcommon::BLOB::Reference open(const ibrcommon::File &f);
00142 
00146                 static void changeProvider(BLOB::Provider *p, bool auto_delete = false);
00147 
00148         protected:
00149                 class ProviderRef
00150                 {
00151                 public:
00152                         ProviderRef(Provider *provider, bool auto_delete);
00153                         virtual ~ProviderRef();
00154 
00155                         void change(Provider *p, bool auto_delete = true);
00156 
00157                         BLOB::Reference create();
00158 
00159                 private:
00160                         Provider *_provider;
00161                         bool _auto_delete;
00162                 };
00163 
00167                 static ProviderRef provider;
00168 
00169                 BLOB();
00170 
00171                 virtual size_t __get_size() = 0;
00172                 virtual std::iostream &__get_stream() = 0;
00173 
00174         private:
00175                 BLOB(const BLOB &ref); // forbidden copy constructor
00176         };
00177 
00182         class FileBLOB : public ibrcommon::BLOB
00183         {
00184         public:
00185                 FileBLOB(const ibrcommon::File &f);
00186                 virtual ~FileBLOB();
00187 
00188                 virtual void clear();
00189 
00190                 virtual void open();
00191                 virtual void close();
00192 
00193         protected:
00194                 std::iostream &__get_stream()
00195                 {
00196                         return _filestream;
00197                 }
00198 
00199                 size_t __get_size();
00200 
00201         private:
00202                 std::fstream _filestream;
00203                 File _file;
00204         };
00205 
00206         class MemoryBLOBProvider : public ibrcommon::BLOB::Provider
00207         {
00208         public:
00209                 MemoryBLOBProvider();
00210                 virtual ~MemoryBLOBProvider();
00211                 BLOB::Reference create();
00212 
00213         private:
00218                 class StringBLOB : public BLOB
00219                 {
00220                 public:
00221                         static BLOB::Reference create();
00222                         virtual ~StringBLOB();
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 _stringstream;
00233                         }
00234 
00235                         size_t __get_size();
00236 
00237                 private:
00238                         StringBLOB();
00239                         std::stringstream _stringstream;
00240                 };
00241         };
00242 
00243         class FileBLOBProvider : public ibrcommon::BLOB::Provider
00244         {
00245         public:
00246                 FileBLOBProvider(const File &path);
00247                 virtual ~FileBLOBProvider();
00248                 BLOB::Reference create();
00249 
00250         private:
00251                 ibrcommon::File _tmppath;
00252 
00258                 class TmpFileBLOB : public BLOB
00259                 {
00260                 public:
00261                         TmpFileBLOB(const File &path);
00262                         virtual ~TmpFileBLOB();
00263 
00264                         virtual void clear();
00265 
00266                         virtual void open();
00267                         virtual void close();
00268 
00269                 protected:
00270                         std::iostream &__get_stream()
00271                         {
00272                                 return _filestream;
00273                         }
00274 
00275                         size_t __get_size();
00276 
00277                 private:
00278                         std::fstream _filestream;
00279                         int _fd;
00280                         File _tmpfile;
00281                 };
00282         };
00283 }
00284 
00285 #endif /* BLOB_H_ */