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> |
Jamie Madill | 93455eb | 2014-07-08 15:22:50 -0400 | [diff] [blame] | 15 | #include <limits.h> |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <set> |
Jamie Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 18 | #include <sstream> |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 19 | #include <cstdarg> |
Jamie Madill | f386bf7 | 2013-07-08 14:02:41 -0400 | [diff] [blame] | 20 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | // A macro to disallow the copy constructor and operator= functions |
| 22 | // This must be used in the private: declarations for a class |
| 23 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 24 | TypeName(const TypeName&); \ |
| 25 | void operator=(const TypeName&) |
| 26 | |
Jamie Madill | 33ea2f9 | 2014-08-29 15:15:01 -0400 | [diff] [blame] | 27 | template <typename T, size_t N> |
| 28 | inline size_t ArraySize(T(&)[N]) |
shannon.woods@transgaming.com | 6d79257 | 2013-02-28 23:07:25 +0000 | [diff] [blame] | 29 | { |
| 30 | return N; |
| 31 | } |
| 32 | |
shannon.woods%transgaming.com@gtempaccount.com | 9622477 | 2013-04-13 03:30:18 +0000 | [diff] [blame] | 33 | template <typename T, unsigned int N> |
| 34 | void SafeRelease(T (&resourceBlock)[N]) |
| 35 | { |
| 36 | for (unsigned int i = 0; i < N; i++) |
| 37 | { |
| 38 | SafeRelease(resourceBlock[i]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
| 43 | void SafeRelease(T& resource) |
| 44 | { |
| 45 | if (resource) |
| 46 | { |
| 47 | resource->Release(); |
| 48 | resource = NULL; |
| 49 | } |
| 50 | } |
| 51 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 52 | template <typename T> |
| 53 | void SafeDelete(T*& resource) |
| 54 | { |
| 55 | delete resource; |
| 56 | resource = NULL; |
| 57 | } |
| 58 | |
| 59 | template <typename T> |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 60 | void SafeDeleteContainer(T& resource) |
| 61 | { |
Shannon Woods | 3dc8020 | 2014-06-16 13:29:52 -0400 | [diff] [blame] | 62 | for (typename T::iterator i = resource.begin(); i != resource.end(); i++) |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 63 | { |
| 64 | SafeDelete(*i); |
| 65 | } |
| 66 | resource.clear(); |
| 67 | } |
| 68 | |
| 69 | template <typename T> |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 70 | void SafeDeleteArray(T*& resource) |
| 71 | { |
| 72 | delete[] resource; |
| 73 | resource = NULL; |
| 74 | } |
| 75 | |
Jamie Madill | d3f0f1e | 2013-09-20 13:31:08 -0400 | [diff] [blame] | 76 | // Provide a less-than function for comparing structs |
| 77 | // Note: struct memory must be initialized to zero, because of packing gaps |
| 78 | template <typename T> |
| 79 | inline bool StructLessThan(const T &a, const T &b) |
| 80 | { |
| 81 | return (memcmp(&a, &b, sizeof(T)) < 0); |
| 82 | } |
| 83 | |
| 84 | // Provide a less-than function for comparing structs |
| 85 | // Note: struct memory must be initialized to zero, because of packing gaps |
| 86 | template <typename T> |
| 87 | inline bool StructEquals(const T &a, const T &b) |
| 88 | { |
| 89 | return (memcmp(&a, &b, sizeof(T)) == 0); |
| 90 | } |
| 91 | |
| 92 | template <typename T> |
| 93 | inline void StructZero(T *obj) |
| 94 | { |
| 95 | memset(obj, 0, sizeof(T)); |
| 96 | } |
| 97 | |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 98 | inline const char* MakeStaticString(const std::string &str) |
| 99 | { |
| 100 | static std::set<std::string> strings; |
| 101 | std::set<std::string>::iterator it = strings.find(str); |
| 102 | if (it != strings.end()) |
| 103 | { |
| 104 | return it->c_str(); |
| 105 | } |
| 106 | |
| 107 | return strings.insert(str).first->c_str(); |
| 108 | } |
| 109 | |
Jamie Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 110 | inline std::string ArrayString(unsigned int i) |
| 111 | { |
| 112 | // We assume UINT_MAX and GL_INVALID_INDEX are equal |
| 113 | // See DynamicHLSL.cpp |
| 114 | if (i == UINT_MAX) |
| 115 | { |
| 116 | return ""; |
| 117 | } |
| 118 | |
| 119 | std::stringstream strstr; |
| 120 | |
| 121 | strstr << "["; |
| 122 | strstr << i; |
| 123 | strstr << "]"; |
| 124 | |
| 125 | return strstr.str(); |
| 126 | } |
| 127 | |
| 128 | inline std::string Str(int i) |
| 129 | { |
| 130 | std::stringstream strstr; |
| 131 | strstr << i; |
| 132 | return strstr.str(); |
| 133 | } |
| 134 | |
Shannon Woods | 8e7d7a3 | 2014-09-02 17:09:08 -0400 | [diff] [blame] | 135 | std::string FormatString(const char *fmt, va_list vararg); |
| 136 | std::string FormatString(const char *fmt, ...); |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 137 | |
alokp@chromium.org | 79fb101 | 2012-04-26 21:07:39 +0000 | [diff] [blame] | 138 | #if defined(_MSC_VER) |
| 139 | #define snprintf _snprintf |
| 140 | #endif |
| 141 | |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 142 | #define VENDOR_ID_AMD 0x1002 |
| 143 | #define VENDOR_ID_INTEL 0x8086 |
| 144 | #define VENDOR_ID_NVIDIA 0x10DE |
| 145 | |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 146 | #define GL_BGRA4_ANGLEX 0x6ABC |
| 147 | #define GL_BGR5_A1_ANGLEX 0x6ABD |
Jamie Madill | 0fda986 | 2013-07-19 16:36:55 -0400 | [diff] [blame] | 148 | #define GL_INT_64_ANGLEX 0x6ABE |
Jamie Madill | 28167c6 | 2013-08-30 13:21:10 -0400 | [diff] [blame] | 149 | #define GL_STRUCT_ANGLEX 0x6ABF |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 150 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 151 | #endif // COMMON_ANGLEUTILS_H_ |