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/data/File.h" 00013 #include "ibrcommon/thread/Semaphore.h" 00014 #include <iostream> 00015 #include <sstream> 00016 #include <fstream> 00017 00018 #ifdef __GNUC__ 00019 #define DEPRECATED(func) func __attribute__ ((deprecated)) 00020 #elif defined(_MSC_VER) 00021 #define DEPRECATED(func) __declspec(deprecated) func 00022 #else 00023 #pragma message("WARNING: You need to implement DEPRECATED for this compiler") 00024 #define DEPRECATED(func) func 00025 #endif 00026 00027 namespace ibrcommon 00028 { 00029 class CanNotOpenFileException : public ibrcommon::IOException 00030 { 00031 public: 00032 CanNotOpenFileException(ibrcommon::File f) throw() : IOException("Could not open file " + f.getPath() + ".") 00033 { 00034 }; 00035 }; 00036 00037 class BLOB : public Mutex 00038 { 00039 public: 00044 static File tmppath; 00045 00049 static ibrcommon::Semaphore _filelimit; 00050 00054 bool _locked; 00055 00056 virtual ~BLOB(); 00057 00062 virtual void clear() = 0; 00063 00064 virtual void open() = 0; 00065 virtual void close() = 0; 00066 00067 class Reference : public Mutex 00068 { 00069 public: 00070 Reference(BLOB *blob); 00071 Reference(const Reference &ref); 00072 virtual ~Reference(); 00073 00078 std::iostream& operator* (); 00079 00084 void enter() throw (ibrcommon::MutexException); 00085 00089 void leave() throw (ibrcommon::MutexException); 00090 00094 void trylock() throw (ibrcommon::MutexException); 00095 00100 void clear(); 00101 00106 size_t getSize() const; 00107 00108 private: 00109 BLOB *_blob; 00110 }; 00111 00112 protected: 00113 BLOB(); 00114 void increment(); 00115 void decrement(); 00116 bool isUnbound(); 00117 00118 virtual size_t getSize(); 00119 00120 virtual std::iostream &__get_stream() = 0; 00121 00122 private: 00123 BLOB(const BLOB &ref); // forbidden copy constructor 00124 size_t _refcount; 00125 Mutex _reflock; 00126 }; 00127 00132 class StringBLOB : public BLOB 00133 { 00134 public: 00135 static BLOB::Reference create(); 00136 virtual ~StringBLOB(); 00137 00138 virtual void clear(); 00139 00140 virtual void open(); 00141 virtual void close(); 00142 00143 protected: 00144 std::iostream &__get_stream() 00145 { 00146 return _stringstream; 00147 } 00148 00149 private: 00150 StringBLOB(); 00151 std::stringstream _stringstream; 00152 }; 00153 00158 class FileBLOB : public BLOB 00159 { 00160 public: 00161 static BLOB::Reference create(const File &f); 00162 virtual ~FileBLOB(); 00163 00164 virtual void clear(); 00165 00166 virtual void open(); 00167 virtual void close(); 00168 00169 protected: 00170 std::iostream &__get_stream() 00171 { 00172 return (*_filestream); 00173 } 00174 00175 private: 00176 FileBLOB(const File &f); 00177 ibrcommon::locked_fstream _filestream; 00178 File _file; 00179 }; 00180 00186 class TmpFileBLOB : public BLOB 00187 { 00188 public: 00189 static BLOB::Reference create(); 00190 virtual ~TmpFileBLOB(); 00191 00192 virtual void clear(); 00193 00194 virtual void open(); 00195 virtual void close(); 00196 00197 protected: 00198 std::iostream &__get_stream() 00199 { 00200 return (*_filestream); 00201 } 00202 00203 private: 00204 TmpFileBLOB(); 00205 void createtmpfile(); 00206 ibrcommon::locked_fstream _filestream; 00207 int _fd; 00208 File _tmpfile; 00209 }; 00210 } 00211 00212 #endif /* BLOB_H_ */
1.7.1