daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // 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.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #ifndef COMMON_ANGLEUTILS_H_ |
| 10 | #define COMMON_ANGLEUTILS_H_ |
| 11 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 12 | #include "common/platform.h" |
| 13 | |
Jamie Madill | f386bf7 | 2013-07-08 14:02:41 -0400 | [diff] [blame] | 14 | #include <stddef.h> |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <set> |
Jamie Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 17 | #include <sstream> |
Jamie Madill | f386bf7 | 2013-07-08 14:02:41 -0400 | [diff] [blame] | 18 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 19 | // 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 Akhgari | 10530c3 | 2014-07-02 20:37:53 -0400 | [diff] [blame] | 25 | // 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.com | 6d79257 | 2013-02-28 23:07:25 +0000 | [diff] [blame] | 36 | template <typename T, unsigned int N> |
| 37 | inline unsigned int ArraySize(T(&)[N]) |
| 38 | { |
| 39 | return N; |
| 40 | } |
| 41 | |
shannon.woods%transgaming.com@gtempaccount.com | 9622477 | 2013-04-13 03:30:18 +0000 | [diff] [blame] | 42 | template <typename T, unsigned int N> |
| 43 | void SafeRelease(T (&resourceBlock)[N]) |
| 44 | { |
| 45 | for (unsigned int i = 0; i < N; i++) |
| 46 | { |
| 47 | SafeRelease(resourceBlock[i]); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | template <typename T> |
| 52 | void SafeRelease(T& resource) |
| 53 | { |
| 54 | if (resource) |
| 55 | { |
| 56 | resource->Release(); |
| 57 | resource = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 61 | template <typename T> |
| 62 | void SafeDelete(T*& resource) |
| 63 | { |
| 64 | delete resource; |
| 65 | resource = NULL; |
| 66 | } |
| 67 | |
| 68 | template <typename T> |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 69 | void SafeDeleteContainer(T& resource) |
| 70 | { |
Shannon Woods | 3dc8020 | 2014-06-16 13:29:52 -0400 | [diff] [blame] | 71 | for (typename T::iterator i = resource.begin(); i != resource.end(); i++) |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 72 | { |
| 73 | SafeDelete(*i); |
| 74 | } |
| 75 | resource.clear(); |
| 76 | } |
| 77 | |
| 78 | template <typename T> |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 79 | void SafeDeleteArray(T*& resource) |
| 80 | { |
| 81 | delete[] resource; |
| 82 | resource = NULL; |
| 83 | } |
| 84 | |
Jamie Madill | d3f0f1e | 2013-09-20 13:31:08 -0400 | [diff] [blame] | 85 | // Provide a less-than function for comparing structs |
| 86 | // Note: struct memory must be initialized to zero, because of packing gaps |
| 87 | template <typename T> |
| 88 | inline 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 |
| 95 | template <typename T> |
| 96 | inline bool StructEquals(const T &a, const T &b) |
| 97 | { |
| 98 | return (memcmp(&a, &b, sizeof(T)) == 0); |
| 99 | } |
| 100 | |
| 101 | template <typename T> |
| 102 | inline void StructZero(T *obj) |
| 103 | { |
| 104 | memset(obj, 0, sizeof(T)); |
| 105 | } |
| 106 | |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 107 | inline 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 Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 119 | inline 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 | |
| 137 | inline std::string Str(int i) |
| 138 | { |
| 139 | std::stringstream strstr; |
| 140 | strstr << i; |
| 141 | return strstr.str(); |
| 142 | } |
| 143 | |
alokp@chromium.org | 79fb101 | 2012-04-26 21:07:39 +0000 | [diff] [blame] | 144 | #if defined(_MSC_VER) |
| 145 | #define snprintf _snprintf |
| 146 | #endif |
| 147 | |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 148 | #define VENDOR_ID_AMD 0x1002 |
| 149 | #define VENDOR_ID_INTEL 0x8086 |
| 150 | #define VENDOR_ID_NVIDIA 0x10DE |
| 151 | |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 152 | #define GL_BGRA4_ANGLEX 0x6ABC |
| 153 | #define GL_BGR5_A1_ANGLEX 0x6ABD |
Jamie Madill | 0fda986 | 2013-07-19 16:36:55 -0400 | [diff] [blame] | 154 | #define GL_INT_64_ANGLEX 0x6ABE |
Jamie Madill | 28167c6 | 2013-08-30 13:21:10 -0400 | [diff] [blame] | 155 | #define GL_STRUCT_ANGLEX 0x6ABF |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 156 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 157 | #endif // COMMON_ANGLEUTILS_H_ |