X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=thirdparty%2Fdirectxtex%2FDirectXTex%2Fscoped.h;fp=thirdparty%2Fdirectxtex%2FDirectXTex%2Fscoped.h;h=358290d7d55ff6d049e47547ffbec219dea813fd;hb=f6a9034a9d6d58ab27b574a0c146a36782762d55;hp=0000000000000000000000000000000000000000;hpb=a4bcf6ae9c4988600a7c4b5b8f9ee37528f342d4;p=apitrace diff --git a/thirdparty/directxtex/DirectXTex/scoped.h b/thirdparty/directxtex/DirectXTex/scoped.h new file mode 100644 index 0000000..358290d --- /dev/null +++ b/thirdparty/directxtex/DirectXTex/scoped.h @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------------- +// scoped.h +// +// Utility header with helper classes for exception-safe handling of resources +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------------- + +#if defined(_MSC_VER) && (_MSC_VER > 1000) +#pragma once +#endif + +#include +#include +#include + +//--------------------------------------------------------------------------------- +struct aligned_deleter { void operator()(void* p) { _aligned_free(p); } }; + +typedef std::unique_ptr ScopedAlignedArrayFloat; + +#ifdef USE_XNAMATH +typedef std::unique_ptr ScopedAlignedArrayXMVECTOR; +#else +typedef std::unique_ptr ScopedAlignedArrayXMVECTOR; +#endif + +//--------------------------------------------------------------------------------- +struct handle_closer { void operator()(HANDLE h) { assert(h != INVALID_HANDLE_VALUE); if (h) CloseHandle(h); } }; + +typedef public std::unique_ptr ScopedHandle; + +inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; } + + +//--------------------------------------------------------------------------------- +template class ScopedObject +{ +public: + explicit ScopedObject( T *p = 0 ) : _pointer(p) {} + ~ScopedObject() + { + if ( _pointer ) + { + _pointer->Release(); + _pointer = nullptr; + } + } + + bool IsNull() const { return (!_pointer); } + + T& operator*() { return *_pointer; } + T* operator->() { return _pointer; } + T** operator&() { return &_pointer; } + + void Reset(T *p = 0) { if ( _pointer ) { _pointer->Release(); } _pointer = p; } + + T* Get() const { return _pointer; } + +private: + ScopedObject(const ScopedObject&); + ScopedObject& operator=(const ScopedObject&); + + T* _pointer; +};