1 //
2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
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
9 #ifndef COMMON_ANGLEUTILS_H_
10 #define COMMON_ANGLEUTILS_H_
11
12 #include "common/platform.h"
13
14 #include <stddef.h>
15 #include <limits.h>
16 #include <string>
17 #include <set>
18 #include <sstream>
19 #include <cstdarg>
20
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
27 template <typename T, size_t N>
ArraySize(T (&)[N])28 inline size_t ArraySize(T(&)[N])
29 {
30 return N;
31 }
32
33 template <typename T, unsigned int N>
SafeRelease(T (& resourceBlock)[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>
SafeRelease(T & resource)43 void SafeRelease(T& resource)
44 {
45 if (resource)
46 {
47 resource->Release();
48 resource = NULL;
49 }
50 }
51
52 template <typename T>
SafeDelete(T * & resource)53 void SafeDelete(T*& resource)
54 {
55 delete resource;
56 resource = NULL;
57 }
58
59 template <typename T>
SafeDeleteContainer(T & resource)60 void SafeDeleteContainer(T& resource)
61 {
62 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
63 {
64 SafeDelete(*i);
65 }
66 resource.clear();
67 }
68
69 template <typename T>
SafeDeleteArray(T * & resource)70 void SafeDeleteArray(T*& resource)
71 {
72 delete[] resource;
73 resource = NULL;
74 }
75
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>
StructLessThan(const T & a,const T & b)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>
StructEquals(const T & a,const T & b)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>
StructZero(T * obj)93 inline void StructZero(T *obj)
94 {
95 memset(obj, 0, sizeof(T));
96 }
97
MakeStaticString(const std::string & str)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
ArrayString(unsigned int i)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
Str(int i)128 inline std::string Str(int i)
129 {
130 std::stringstream strstr;
131 strstr << i;
132 return strstr.str();
133 }
134
135 std::string FormatString(const char *fmt, va_list vararg);
136 std::string FormatString(const char *fmt, ...);
137
138 #if defined(_MSC_VER)
139 #define snprintf _snprintf
140 #endif
141
142 #define VENDOR_ID_AMD 0x1002
143 #define VENDOR_ID_INTEL 0x8086
144 #define VENDOR_ID_NVIDIA 0x10DE
145
146 #define GL_BGRA4_ANGLEX 0x6ABC
147 #define GL_BGR5_A1_ANGLEX 0x6ABD
148 #define GL_INT_64_ANGLEX 0x6ABE
149 #define GL_STRUCT_ANGLEX 0x6ABF
150
151 #endif // COMMON_ANGLEUTILS_H_
152