AudRecordLib
|
00001 00005 #ifndef SCOPED_OBJECTS_H 00006 #define SCOPED_OBJECTS_H 00007 00008 #pragma once 00009 00014 template<class Type> 00015 class ScopedArray 00016 { 00017 private: 00019 Type* data; 00020 00021 public: 00026 ScopedArray(Type* data) : data(data) 00027 {} 00028 00033 ScopedArray(UINT elements) : data(new Type[elements]) 00034 {} 00035 00040 ScopedArray() : data(NULL) 00041 {} 00042 00044 ~ScopedArray() 00045 { 00046 delete [] data; 00047 } 00048 00056 Type* get() const 00057 { 00058 ASSERT(data && "Array not initialized"); 00059 return data; 00060 } 00061 00070 Type** operator&() 00071 { 00072 ASSERT(!data && "Array already initialized"); 00073 return &data; 00074 } 00075 00083 Type* operator*() const 00084 { 00085 return get(); 00086 } 00087 00095 void Init(Type* newData) 00096 { 00097 ASSERT(!data && "Array already initialized"); 00098 data = newData; 00099 } 00100 00108 Type* Release() 00109 { 00110 Type* pTemp = data; 00111 data = NULL; 00112 return pTemp; 00113 } 00114 00119 void Swap(ScopedArray& other) 00120 { 00121 Type* pTemp = other.data; 00122 other.data = data; 00123 data = pTemp; 00124 } 00125 }; 00126 00132 template<class Type = HANDLE, class Ret = BOOL> 00133 class WinType 00134 { 00135 private: 00137 typedef Ret (WINAPI*ReleaseProc)(Type); 00139 typedef void (WinType:: * bool_type)(WinType&); 00140 00142 Type data; 00144 ReleaseProc lpfnReleaser; 00145 00146 public: 00152 WinType(const Type& data, ReleaseProc lpfnReleaser) 00153 : data(data), 00154 lpfnReleaser(lpfnReleaser) 00155 {} 00156 00168 WinType(ReleaseProc lpfnReleaser) 00169 : data(), 00170 lpfnReleaser(lpfnReleaser) 00171 {} 00172 00177 WinType() 00178 : data(), 00179 lpfnReleaser(NULL) 00180 {} 00181 00183 ~WinType() 00184 { 00185 if(lpfnReleaser) 00186 { 00187 lpfnReleaser(data); 00188 } 00189 } 00190 00198 Type get() const 00199 { 00200 ASSERT(lpfnReleaser && "Object not initialised"); 00201 return data; 00202 } 00203 00210 Type operator*() const 00211 { 00212 return get(); 00213 } 00214 00222 Type* operator&() 00223 { 00224 ASSERT((data == Type()) && lpfnReleaser && "Object already initialised"); 00225 return &data; 00226 } 00227 00232 operator bool_type() const 00233 { 00234 return lpfnReleaser ? &WinType::Swap : NULL; 00235 } 00236 00244 void Init(const Type& newData, ReleaseProc lpfnNewReleaser) 00245 { 00246 ASSERT((!lpfnReleaser) && "Object already initialized"); 00247 data = newData; 00248 lpfnReleaser = lpfnNewReleaser; 00249 } 00250 00258 Type Release() 00259 { 00260 lpfnReleaser = NULL; 00261 Type temp = data; 00262 data = Type(); 00263 return temp; 00264 } 00265 00270 void Swap(WinType& other) 00271 { 00272 Type temp = other.data; 00273 ReleaseProc tempProc = other.lpfnReleaser; 00274 other.data = data; 00275 other.lpfnReleaser = lpfnReleaser; 00276 data = temp; 00277 lpfnReleaser = tempProc; 00278 } 00279 }; 00280 00285 template<class Type> 00286 class ComType 00287 { 00288 private: 00290 Type* data; 00291 00293 typedef ULONG (STDMETHODCALLTYPE Type::IUnknown:: * bool_type)(); 00294 00295 public: 00303 ComType(Type* data) 00304 : data(data) 00305 { 00306 // check that this is actually a com interface pointer 00307 // and not anything else 00308 C_ASSERT((__is_base_of(IUnknown, Type))); 00309 } 00310 00315 ComType(const ComType& other) 00316 : data(other.data) 00317 { 00318 if(data) 00319 { 00320 data->AddRef(); 00321 } 00322 } 00323 00326 ComType() 00327 : data(NULL) 00328 {} 00329 00332 ~ComType() 00333 { 00334 if(data) 00335 { 00336 data->Release(); 00337 } 00338 } 00339 00344 void operator=(const ComType& other) 00345 { 00346 ComType temp(other); 00347 Swap(temp); 00348 } 00349 00357 Type* get() const 00358 { 00359 ASSERT(data && "No object set"); 00360 return data; 00361 } 00362 00369 Type* operator*() const 00370 { 00371 return get(); 00372 } 00373 00382 Type** operator&() 00383 { 00384 ASSERT(!data && "Object already initialized"); 00385 return &data; 00386 } 00387 00394 Type* operator->() 00395 { 00396 return get(); 00397 } 00398 00403 operator bool_type() const 00404 { 00405 return data ? &Type::Release : 0; 00406 } 00407 00414 void Init(Type* newData) 00415 { 00416 ASSERT(!data && "Object already initialized"); 00417 data = newData; 00418 } 00419 00429 Type* Release() 00430 { 00431 Type* temp = data; 00432 data = NULL; 00433 return temp; 00434 } 00435 00440 void Swap(ComType& other) 00441 { 00442 Type* pTemp = other.data; 00443 other.data = data; 00444 data = pTemp; 00445 } 00446 }; 00447 00451 struct SharedSRWLock 00452 { 00454 SRWLOCK* pLock; 00455 00461 SharedSRWLock(SRWLOCK* pLock) 00462 : pLock(pLock) 00463 { 00464 AcquireSRWLockShared(pLock); 00465 } 00466 00468 ~SharedSRWLock() 00469 { 00470 ReleaseSRWLockShared(pLock); 00471 } 00472 }; 00473 00477 struct ExclusiveSRWLock 00478 { 00480 SRWLOCK* pLock; 00481 00487 ExclusiveSRWLock(SRWLOCK* pLock) 00488 : pLock(pLock) 00489 { 00490 AcquireSRWLockExclusive(pLock); 00491 } 00492 00494 ~ExclusiveSRWLock() 00495 { 00496 ReleaseSRWLockExclusive(pLock); 00497 } 00498 }; 00499 00502 #endif