Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PlatformHelpers.h
Go to the documentation of this file.
1 // The copyright in this software is being made available under the BSD License, included below.
2 // This software may be subject to other third party and contributor rights, including patent rights,
3 // and no such rights are granted under this license.
4 //
5 // Copyright (c) 2013, Microsoft Open Technologies, Inc.
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without modification,
9 // are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright notice,
12 // this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright notice,
14 // this list of conditions and the following disclaimer in the documentation
15 // and/or other materials provided with the distribution.
16 // - Neither the name of Microsoft Open Technologies, Inc. nor the names of its contributors
17 // may be used to endorse or promote products derived from this software
18 // without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 //--------------------------------------------------------------------------------------
30 // File: PlatformHelpers.h
31 //
32 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
33 // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
34 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
35 // PARTICULAR PURPOSE.
36 //
37 // Copyright (c) Microsoft Corporation. All rights reserved.
38 //
39 // http://go.microsoft.com/fwlink/?LinkId=248929
40 //--------------------------------------------------------------------------------------
41 
42 #pragma once
43 
44 #pragma warning(disable : 4324)
45 
46 #include <exception>
47 
48 #if defined(_DEBUG) || defined(PROFILE)
49 //#pragma comment(lib,"dxguid.lib")
50 #endif
51 
52 namespace DirectX
53 {
54  // Helper utility converts D3D API failures into exceptions.
55  inline void ThrowIfFailed(HRESULT hr)
56  {
57  if (FAILED(hr))
58  {
59  throw std::exception();
60  }
61  }
62 
63 
64  // Helper sets a D3D resource name string (used by PIX and debug layer leak reporting).
65  template<UINT TNameLength>
66  inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_z_ const char (&name)[TNameLength])
67  {
68  #if defined(_DEBUG) || defined(PROFILE)
69  resource->SetPrivateData(WKPDID_D3DDebugObjectName, TNameLength - 1, name);
70  #else
71  UNREFERENCED_PARAMETER(resource);
72  UNREFERENCED_PARAMETER(name);
73  #endif
74  }
75 
76 
77  // Helper smart-pointers
78  struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
79 
80  typedef public std::unique_ptr<void, handle_closer> ScopedHandle;
81 
82  inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
83 
84 
85  template<class T> class ScopedObject
86  {
87  public:
88  explicit ScopedObject( T *p = 0 ) : _pointer(p) {}
90  {
91  if ( _pointer )
92  {
93  _pointer->Release();
94  _pointer = nullptr;
95  }
96  }
97 
98  bool IsNull() const { return (!_pointer); }
99 
100  T& operator*() { return *_pointer; }
101  T* operator->() { return _pointer; }
102  T** operator&() { return &_pointer; }
103 
104  void Reset(T *p = 0) { if ( _pointer ) { _pointer->Release(); } _pointer = p; }
105 
106  T* Get() const { return _pointer; }
107 
108  private:
109  ScopedObject(const ScopedObject&);
110  ScopedObject& operator=(const ScopedObject&);
111 
112  T* _pointer;
113  };
114 }
115 
116 
117 #if defined(_MSC_VER) && (_MSC_VER < 1610)
118 
119 // Emulate the C++0x mutex and lock_guard types when building with Visual Studio versions < 2012.
120 namespace std
121 {
122  class mutex
123  {
124  public:
125  mutex() { InitializeCriticalSection(&mCriticalSection); }
126  ~mutex() { DeleteCriticalSection(&mCriticalSection); }
127 
128  void lock() { EnterCriticalSection(&mCriticalSection); }
129  void unlock() { LeaveCriticalSection(&mCriticalSection); }
130  bool try_lock() { return TryEnterCriticalSection(&mCriticalSection) != 0; }
131 
132  private:
133  CRITICAL_SECTION mCriticalSection;
134 
135  mutex(mutex const&);
136  mutex& operator= (mutex const&);
137  };
138 
139 
140  template<typename Mutex>
141  class lock_guard
142  {
143  public:
144  typedef Mutex mutex_type;
145 
146  explicit lock_guard(mutex_type& mutex)
147  : mMutex(mutex)
148  {
149  mMutex.lock();
150  }
151 
152  ~lock_guard()
153  {
154  mMutex.unlock();
155  }
156 
157  private:
158  mutex_type& mMutex;
159 
160  lock_guard(lock_guard const&);
161  lock_guard& operator= (lock_guard const&);
162  };
163 }
164 
165 #else // _MSC_VER < 1610
166 
167 #include <mutex>
168 
169 #endif
T & operator*()
Definition: PlatformHelpers.h:100
void ThrowIfFailed(HRESULT hr)
Definition: PlatformHelpers.h:55
T * operator->()
Definition: PlatformHelpers.h:101
public std::unique_ptr< void, handle_closer > ScopedHandle
Definition: PlatformHelpers.h:80
Definition: PlatformHelpers.h:78
Definition: PlatformHelpers.h:85
HANDLE safe_handle(HANDLE h)
Definition: PlatformHelpers.h:82
ScopedObject(T *p=0)
Definition: PlatformHelpers.h:88
T ** operator&()
Definition: PlatformHelpers.h:102
void Reset(T *p=0)
Definition: PlatformHelpers.h:104
GLfloat GLfloat p
Definition: GLee.h:8473
int int int int int int h
Definition: GLee.h:17156
GLuint const GLchar * name
Definition: GLee.h:2259
T * Get() const
Definition: PlatformHelpers.h:106
void operator()(HANDLE h)
Definition: PlatformHelpers.h:78
bool IsNull() const
Definition: PlatformHelpers.h:98
void SetDebugObjectName(_In_ ID3D11DeviceChild *resource, _In_z_ const char(&name)[TNameLength])
Definition: PlatformHelpers.h:66
~ScopedObject()
Definition: PlatformHelpers.h:89