blob: 438e197838734574c128a01127e8d68a219171c9 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// angleutils.h: Common ANGLE utilities.
8
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00009#ifndef COMMON_ANGLEUTILS_H_
10#define COMMON_ANGLEUTILS_H_
11
Geoff Lang44fa7592014-05-30 11:50:07 -040012#include "common/platform.h"
13
Jamie Madillf386bf72013-07-08 14:02:41 -040014#include <stddef.h>
Jamie Madill93455eb2014-07-08 15:22:50 -040015#include <limits.h>
Geoff Langcec35902014-04-16 10:52:36 -040016#include <string>
17#include <set>
Jamie Madill53cb14d2014-07-08 15:02:35 -040018#include <sstream>
Jamie Madillf386bf72013-07-08 14:02:41 -040019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020// A macro to disallow the copy constructor and operator= functions
21// This must be used in the private: declarations for a class
22#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
23 TypeName(const TypeName&); \
24 void operator=(const TypeName&)
25
Ehsan Akhgari10530c32014-07-02 20:37:53 -040026// Macros for writing try catch blocks. Defining ANGLE_NO_EXCEPTIONS
27// when building angle will disable the usage of try/catch for compilers
28// without proper support for them (such as clang-cl).
29#ifdef ANGLE_NO_EXCEPTIONS
30#define ANGLE_TRY if (true)
31#define ANGLE_CATCH_ALL else
32#else
33#define ANGLE_TRY try
34#define ANGLE_CATCH_ALL catch(...)
35#endif
36
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000037template <typename T, unsigned int N>
38inline unsigned int ArraySize(T(&)[N])
39{
40 return N;
41}
42
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000043template <typename T, unsigned int N>
44void SafeRelease(T (&resourceBlock)[N])
45{
46 for (unsigned int i = 0; i < N; i++)
47 {
48 SafeRelease(resourceBlock[i]);
49 }
50}
51
52template <typename T>
53void SafeRelease(T& resource)
54{
55 if (resource)
56 {
57 resource->Release();
58 resource = NULL;
59 }
60}
61
Geoff Langea228632013-07-30 15:17:12 -040062template <typename T>
63void SafeDelete(T*& resource)
64{
65 delete resource;
66 resource = NULL;
67}
68
69template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040070void SafeDeleteContainer(T& resource)
71{
Shannon Woods3dc80202014-06-16 13:29:52 -040072 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040073 {
74 SafeDelete(*i);
75 }
76 resource.clear();
77}
78
79template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040080void SafeDeleteArray(T*& resource)
81{
82 delete[] resource;
83 resource = NULL;
84}
85
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040086// Provide a less-than function for comparing structs
87// Note: struct memory must be initialized to zero, because of packing gaps
88template <typename T>
89inline bool StructLessThan(const T &a, const T &b)
90{
91 return (memcmp(&a, &b, sizeof(T)) < 0);
92}
93
94// Provide a less-than function for comparing structs
95// Note: struct memory must be initialized to zero, because of packing gaps
96template <typename T>
97inline bool StructEquals(const T &a, const T &b)
98{
99 return (memcmp(&a, &b, sizeof(T)) == 0);
100}
101
102template <typename T>
103inline void StructZero(T *obj)
104{
105 memset(obj, 0, sizeof(T));
106}
107
Geoff Langcec35902014-04-16 10:52:36 -0400108inline const char* MakeStaticString(const std::string &str)
109{
110 static std::set<std::string> strings;
111 std::set<std::string>::iterator it = strings.find(str);
112 if (it != strings.end())
113 {
114 return it->c_str();
115 }
116
117 return strings.insert(str).first->c_str();
118}
119
Jamie Madill53cb14d2014-07-08 15:02:35 -0400120inline std::string ArrayString(unsigned int i)
121{
122 // We assume UINT_MAX and GL_INVALID_INDEX are equal
123 // See DynamicHLSL.cpp
124 if (i == UINT_MAX)
125 {
126 return "";
127 }
128
129 std::stringstream strstr;
130
131 strstr << "[";
132 strstr << i;
133 strstr << "]";
134
135 return strstr.str();
136}
137
138inline std::string Str(int i)
139{
140 std::stringstream strstr;
141 strstr << i;
142 return strstr.str();
143}
144
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000145#if defined(_MSC_VER)
146#define snprintf _snprintf
147#endif
148
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000149#define VENDOR_ID_AMD 0x1002
150#define VENDOR_ID_INTEL 0x8086
151#define VENDOR_ID_NVIDIA 0x10DE
152
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000153#define GL_BGRA4_ANGLEX 0x6ABC
154#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400155#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400156#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000157
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000158#endif // COMMON_ANGLEUTILS_H_