00001 #ifndef hpp_PlatformSpecificCode_hpp
00002 #define hpp_PlatformSpecificCode_hpp
00003
00004
00005 #include "../Strings/Strings.hpp"
00006 #ifdef _WIN32
00007 #include <windows.h>
00008 #else
00009 #include <pthread.h>
00010 #endif
00011
00013 namespace PlatformSpecific
00014 {
00015 typedef Strings::FastString String;
00016
00018 class FileConvention
00019 {
00020 public:
00022 virtual String convertFilename(String fileName) const = 0;
00023 };
00024
00025 const FileConvention & getCurrentFileConvention();
00026
00027
00097 class FastLock
00098 {
00099 public:
00101 enum TimeOut
00102 {
00103 InstantCheck = 0,
00104 Infinite = 0xFFFFFFFF,
00105 };
00106 private:
00108 #ifdef _WIN32
00109 CRITICAL_SECTION mhMutex;
00110 #else
00111 pthread_mutex_t mhMutex;
00112 #endif
00113 bool _Lock(const TimeOut & dwLength);
00114 void _Unlock();
00115
00116 public:
00117 FastLock(const char * sName = NULL, const bool & bInitialOwner = false);
00118 ~FastLock();
00119
00123 inline bool Acquire() { return _Lock(Infinite); }
00125 inline bool Release() { _Unlock(); return true; }
00126 };
00127
00128
00142 class ScopeLock
00143 {
00144 private:
00146 FastLock & lock;
00147 public:
00148 ScopeLock(volatile FastLock & _lock) : lock(*const_cast<FastLock*>(&_lock)) { lock.Acquire(); }
00149 ScopeLock(FastLock & _lock) : lock(_lock) { lock.Acquire(); }
00150 ~ScopeLock() { lock.Release(); }
00151 };
00152
00163 template <class T>
00164 class LockingPtr
00165 {
00166 public:
00167
00168 LockingPtr(volatile T& obj, FastLock& mtx) : pObj_(const_cast<T*>(&obj)), pMtx_(&mtx) { mtx.Acquire(); }
00169 ~LockingPtr() { pMtx_->Release(); }
00170
00171 T& operator*() { return *pObj_; }
00172 T* operator->() { return pObj_; }
00173
00174 private:
00175
00176 T* pObj_;
00177
00178 FastLock* pMtx_;
00179
00180
00181
00182 LockingPtr& operator=(const LockingPtr&);
00183 };
00184
00195 template <class T>
00196 class LockingObjPtr
00197 {
00198 public:
00199
00200 LockingObjPtr(volatile T& obj, volatile FastLock& mtx) : pObj_(const_cast<T*>(&obj)), pMtx_(const_cast<FastLock*>(&mtx)) { pMtx_->Acquire(); }
00201 ~LockingObjPtr() { pMtx_->Release(); }
00202
00203 T& operator*() { return *pObj_; }
00204 T* operator->() { return pObj_; }
00205
00206 private:
00207
00208 T* pObj_;
00209
00210 FastLock* pMtx_;
00211
00212
00213 LockingObjPtr& operator=(const LockingObjPtr&);
00214
00215 };
00216
00227 template <class T>
00228 class LockingConstObjPtr
00229 {
00230 public:
00231
00232 LockingConstObjPtr(const volatile T& obj,
00233 const volatile FastLock& mtx)
00234 : pObj_(const_cast<const T*>(&obj)),
00235 pMtx_(const_cast<FastLock*>(&mtx))
00236 { pMtx_->Acquire(); }
00237 ~LockingConstObjPtr() { pMtx_->Release(); }
00238
00239 const T& operator*() { return *pObj_; }
00240 const T* operator->() { return pObj_; }
00241
00242 private:
00243
00244 const T* pObj_;
00245
00246 FastLock* pMtx_;
00247
00248
00249
00250 LockingConstObjPtr& operator=(const LockingConstObjPtr&);
00251 };
00252
00254 template <class T>
00255 inline LockingObjPtr<T> AccessProt(volatile T & obj) { return LockingObjPtr<T>(obj, obj.lock); }
00256 template <class T>
00257 inline LockingConstObjPtr<T> AccessProt(const volatile T * obj) { return LockingConstObjPtr<T>(*obj, obj->lock); }
00258 template <class T>
00259 inline LockingPtr<T> AccessProt(volatile T & obj, FastLock & lock) { return LockingPtr<T>(obj, lock); }
00260 }
00261
00262 #endif