-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefix.h
302 lines (257 loc) · 6.68 KB
/
Prefix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2011 Robert Engeln ([email protected]) All rights reserved.
// See accompanying LICENSE file for full license information.
///////////////////////////////////////////////////////////////////////////////
#ifndef __NYX_PREFIX_H__
#define __NYX_PREFIX_H__
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <tchar.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <DXGI.h>
#include <DxErr.h>
#include <xnamath.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <boost/exception/all.hpp>
#include <boost/format.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/utility.hpp>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/fterrors.h>
typedef unsigned int uint;
#include "Vector.h"
#include "Matrix.h"
//
// Generic exception class.
//
class Exception : public virtual std::exception,
public virtual boost::exception
{
public:
//
// Constructor.
//
// Parameters:
// [in] msg
// Error message.
//
Exception(const std::string& msg);
Exception(const boost::format& msg);
//
// Appends a string to the error message.
//
Exception& operator<<(const std::string& str);
Exception& operator<<(const boost::format& str);
//
// Returns the error message.
//
virtual const char* what() const throw();
private:
//
// Properties.
//
std::string m_message;
};
inline Exception::Exception(const std::string& msg)
: m_message(msg)
{
}
inline Exception::Exception(const boost::format& msg)
: m_message(msg.str())
{
}
inline Exception& Exception::operator<<(const std::string& str)
{
m_message.append(str);
return *this;
}
inline Exception& Exception::operator<<(const boost::format& str)
{
m_message.append(str.str());
return *this;
}
inline const char* Exception::what() const throw()
{
return m_message.c_str();
}
//
// Template class for specializing exception types.
//
// Parameters:
// [template] T
// Tag type for specializing the exception; unused.
// [template] Base
// Type to inherit from.
//
template <typename T, typename Base = Exception>
class Error : public Base
{
public:
//
// Constructor.
//
// Parameters:
// [in] msg
// Error message.
//
Error(const std::string& msg);
Error(const boost::format& msg);
};
template <typename T, typename Base>
Error<T, Base>::Error(const std::string& msg)
: Base(msg)
{
}
template <typename T, typename Base>
Error<T, Base>::Error(const boost::format& msg)
: Base(msg.str())
{
}
//
// Generic base for system errors.
//
typedef Error<struct _SystemError> SystemError;
//
// Specialized exception type for Win32 errors.
//
typedef Error<struct _Win32Error> Win32Error;
//
// Specialized exception type for Direct3D errors.
//
typedef Error<struct _Direct3DError, SystemError> Direct3DError;
//
// Specialized exception type for Freetype errors.
//
typedef Error<struct _FreetypeError, SystemError> FreetypeError;
//
// Enable Win32 error info to be attached to an exception.
//
typedef boost::error_info<struct _Win32ErrorInfo, HRESULT> Win32ErrorInfo;
inline Win32ErrorInfo GetLastErrorInfo()
{
return Win32ErrorInfo(GetLastError());
}
inline std::string to_string(const Win32ErrorInfo& err)
{
return (boost::format("[0x%08X] %s: %s") % err.value()
% DXGetErrorStringA(err.value())
% DXGetErrorDescriptionA(err.value())).str();
}
//
// Enable DirectX error info to be attached to an exception.
//
typedef boost::error_info<struct _DirectXErrorInfo, HRESULT> DirectXErrorInfo;
inline std::string to_string(const DirectXErrorInfo& err)
{
return (boost::format("[0x%08X] %s: %s") % err.value()
% DXGetErrorStringA(err.value())
% DXGetErrorDescriptionA(err.value())).str();
}
//
// Exception throwing macro.
//
#define THROW(x) BOOST_THROW_EXCEPTION((x))
#define CHECK(e, x) do { if (!(x)) THROW(e("Assert failed: " #x)); } while(0)
#define D3DCHECK(x) do { HRESULT hr; hr = (x); if (FAILED(hr)) THROW(Direct3DError("Assert failed: " #x) << DirectXErrorInfo(hr)); } while(0)
#define WINCHECK(x) do { if (!(x)) THROW(Win32Error("Assert failed: " #x) << GetLastErrorInfo()); } while(0)
#define FTCHECK(x) do { FT_Error err; err = (x); if (err != 0) THROW(FreetypeError("Assert failed: " #x) << (boost::format(" - Error code %d") % err)); } while(0)
//
// Enable intrusive_ptr to work with COM objects.
//
inline void intrusive_ptr_add_ref(IUnknown* ptr)
{
ptr->AddRef();
}
inline void intrusive_ptr_release(IUnknown* ptr)
{
ptr->Release();
}
//
// Enable intrusive_ptr objects to be passed as parameters to functions expecting
// a pointer-to-pointer which will be initialized by the function.
//
template <typename T>
class IntrusivePtrWrapper
{
public:
//
// Constructor.
//
// Parameters:
// [in] ref
// Intrusive_ptr object to wrap.
// [in] addRef
// If true then the ptr's refcount will be incremented on acquisition.
//
IntrusivePtrWrapper(boost::intrusive_ptr<T>& ref, bool addRef);
//
// Destructor.
//
~IntrusivePtrWrapper();
//
// Implicit conversion to T**.
//
operator T**();
//
// Implicit conversion to void**.
//
operator void**();
private:
//
// Properties.
//
boost::intrusive_ptr<T>& m_ref;
T* m_ptr;
bool m_addRef;
};
template <typename T>
inline IntrusivePtrWrapper<T>::IntrusivePtrWrapper(boost::intrusive_ptr<T>& ref, bool addRef)
: m_ref(ref), m_addRef(addRef), m_ptr(nullptr)
{
}
template <typename T>
inline IntrusivePtrWrapper<T>::~IntrusivePtrWrapper()
{
m_ref = boost::intrusive_ptr<T>(m_ptr, m_addRef);
}
template <typename T>
inline IntrusivePtrWrapper<T>::operator T**()
{
return &m_ptr;
}
template <typename T>
inline IntrusivePtrWrapper<T>::operator void**()
{
return reinterpret_cast<void**>(&m_ptr);
}
//
// Helper function for constructing an IntrusivePtrWrapper.
//
// Parameters:
// [in] ref
// Intrusive_ptr object to wrap.
// [in] addRef
// If true then the ptr's refcount will be incremented on acquisition.
// Default is false, which matches the behavior of COM functions.
//
template <typename T>
inline IntrusivePtrWrapper<T> AttachPtr(boost::intrusive_ptr<T>& ref, bool addRef = false)
{
return IntrusivePtrWrapper<T>(ref, addRef);
}
#endif // __NYX_PREFIX_H__