1 //-------------------------------------------------------------------------------------
4 // Utility header with helper classes for exception-safe handling of resources
6 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
7 // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
11 // Copyright (c) Microsoft Corporation. All rights reserved.
12 //-------------------------------------------------------------------------------------
14 #if defined(_MSC_VER) && (_MSC_VER > 1000)
22 //---------------------------------------------------------------------------------
23 struct aligned_deleter { void operator()(void* p) { _aligned_free(p); } };
25 typedef std::unique_ptr<float, aligned_deleter> ScopedAlignedArrayFloat;
28 typedef std::unique_ptr<XMVECTOR, aligned_deleter> ScopedAlignedArrayXMVECTOR;
30 typedef std::unique_ptr<DirectX::XMVECTOR, aligned_deleter> ScopedAlignedArrayXMVECTOR;
33 //---------------------------------------------------------------------------------
34 struct handle_closer { void operator()(HANDLE h) { assert(h != INVALID_HANDLE_VALUE); if (h) CloseHandle(h); } };
36 typedef std::unique_ptr<void, handle_closer> ScopedHandle;
38 inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
41 //---------------------------------------------------------------------------------
42 template<class T> class ScopedObject
45 explicit ScopedObject( T *p = 0 ) : _pointer(p) {}
55 bool IsNull() const { return (!_pointer); }
57 T& operator*() { return *_pointer; }
58 T* operator->() { return _pointer; }
59 T** operator&() { return &_pointer; }
61 void Reset(T *p = 0) { if ( _pointer ) { _pointer->Release(); } _pointer = p; }
63 T* Get() const { return _pointer; }
66 ScopedObject(const ScopedObject&);
67 ScopedObject& operator=(const ScopedObject&);