blob: ec9afe82288c8bf73fc3cd7eb158fbf7b288eb1e [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>
Geoff Langcec35902014-04-16 10:52:36 -040015#include <string>
16#include <set>
Jamie Madill53cb14d2014-07-08 15:02:35 -040017#include <sstream>
Jamie Madillf386bf72013-07-08 14:02:41 -040018
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019// A macro to disallow the copy constructor and operator= functions
20// This must be used in the private: declarations for a class
21#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
22 TypeName(const TypeName&); \
23 void operator=(const TypeName&)
24
Ehsan Akhgari10530c32014-07-02 20:37:53 -040025// Macros for writing try catch blocks. Defining ANGLE_NO_EXCEPTIONS
26// when building angle will disable the usage of try/catch for compilers
27// without proper support for them (such as clang-cl).
28#ifdef ANGLE_NO_EXCEPTIONS
29#define ANGLE_TRY if (true)
30#define ANGLE_CATCH_ALL else
31#else
32#define ANGLE_TRY try
33#define ANGLE_CATCH_ALL catch(...)
34#endif
35
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000036template <typename T, unsigned int N>
37inline unsigned int ArraySize(T(&)[N])
38{
39 return N;
40}
41
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000042template <typename T, unsigned int N>
43void SafeRelease(T (&resourceBlock)[N])
44{
45 for (unsigned int i = 0; i < N; i++)
46 {
47 SafeRelease(resourceBlock[i]);
48 }
49}
50
51template <typename T>
52void SafeRelease(T& resource)
53{
54 if (resource)
55 {
56 resource->Release();
57 resource = NULL;
58 }
59}
60
Geoff Langea228632013-07-30 15:17:12 -040061template <typename T>
62void SafeDelete(T*& resource)
63{
64 delete resource;
65 resource = NULL;
66}
67
68template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040069void SafeDeleteContainer(T& resource)
70{
Shannon Woods3dc80202014-06-16 13:29:52 -040071 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040072 {
73 SafeDelete(*i);
74 }
75 resource.clear();
76}
77
78template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040079void SafeDeleteArray(T*& resource)
80{
81 delete[] resource;
82 resource = NULL;
83}
84
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040085// Provide a less-than function for comparing structs
86// Note: struct memory must be initialized to zero, because of packing gaps
87template <typename T>
88inline bool StructLessThan(const T &a, const T &b)
89{
90 return (memcmp(&a, &b, sizeof(T)) < 0);
91}
92
93// Provide a less-than function for comparing structs
94// Note: struct memory must be initialized to zero, because of packing gaps
95template <typename T>
96inline bool StructEquals(const T &a, const T &b)
97{
98 return (memcmp(&a, &b, sizeof(T)) == 0);
99}
100
101template <typename T>
102inline void StructZero(T *obj)
103{
104 memset(obj, 0, sizeof(T));
105}
106
Geoff Langcec35902014-04-16 10:52:36 -0400107inline const char* MakeStaticString(const std::string &str)
108{
109 static std::set<std::string> strings;
110 std::set<std::string>::iterator it = strings.find(str);
111 if (it != strings.end())
112 {
113 return it->c_str();
114 }
115
116 return strings.insert(str).first->c_str();
117}
118
Jamie Madill53cb14d2014-07-08 15:02:35 -0400119inline std::string ArrayString(unsigned int i)
120{
121 // We assume UINT_MAX and GL_INVALID_INDEX are equal
122 // See DynamicHLSL.cpp
123 if (i == UINT_MAX)
124 {
125 return "";
126 }
127
128 std::stringstream strstr;
129
130 strstr << "[";
131 strstr << i;
132 strstr << "]";
133
134 return strstr.str();
135}
136
137inline std::string Str(int i)
138{
139 std::stringstream strstr;
140 strstr << i;
141 return strstr.str();
142}
143
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000144#if defined(_MSC_VER)
145#define snprintf _snprintf
146#endif
147
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000148#define VENDOR_ID_AMD 0x1002
149#define VENDOR_ID_INTEL 0x8086
150#define VENDOR_ID_NVIDIA 0x10DE
151
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000152#define GL_BGRA4_ANGLEX 0x6ABC
153#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400154#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400155#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000156
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000157#endif // COMMON_ANGLEUTILS_H_