webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
angleutils.h
Go to the documentation of this file.
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 <climits>
15 #include <cstdarg>
16 #include <cstddef>
17 #include <string>
18 #include <set>
19 #include <sstream>
20 #include <vector>
21 
22 // A helper class to disallow copy and assignment operators
23 namespace angle
24 {
25 
27 {
28  public:
29  NonCopyable() = default;
30  ~NonCopyable() = default;
31  protected:
32  NonCopyable(const NonCopyable&) = delete;
33  void operator=(const NonCopyable&) = delete;
34 };
35 
36 extern const uintptr_t DirtyPointer;
37 } // namespace angle
38 
39 template <typename T, size_t N>
40 constexpr inline size_t ArraySize(T (&)[N])
41 {
42  return N;
43 }
44 
45 template <typename T, unsigned int N>
46 void SafeRelease(T (&resourceBlock)[N])
47 {
48  for (unsigned int i = 0; i < N; i++)
49  {
50  SafeRelease(resourceBlock[i]);
51  }
52 }
53 
54 template <typename T>
55 void SafeRelease(T& resource)
56 {
57  if (resource)
58  {
59  resource->Release();
60  resource = NULL;
61  }
62 }
63 
64 template <typename T>
65 void SafeDelete(T *&resource)
66 {
67  delete resource;
68  resource = NULL;
69 }
70 
71 template <typename T>
72 void SafeDeleteContainer(T& resource)
73 {
74  for (auto &element : resource)
75  {
77  }
78  resource.clear();
79 }
80 
81 template <typename T>
82 void SafeDeleteArray(T*& resource)
83 {
84  delete[] resource;
85  resource = NULL;
86 }
87 
88 // Provide a less-than function for comparing structs
89 // Note: struct memory must be initialized to zero, because of packing gaps
90 template <typename T>
91 inline bool StructLessThan(const T &a, const T &b)
92 {
93  return (memcmp(&a, &b, sizeof(T)) < 0);
94 }
95 
96 // Provide a less-than function for comparing structs
97 // Note: struct memory must be initialized to zero, because of packing gaps
98 template <typename T>
99 inline bool StructEquals(const T &a, const T &b)
100 {
101  return (memcmp(&a, &b, sizeof(T)) == 0);
102 }
103 
104 template <typename T>
105 inline void StructZero(T *obj)
106 {
107  memset(obj, 0, sizeof(T));
108 }
109 
110 template <typename T>
111 inline bool IsMaskFlagSet(T mask, T flag)
112 {
113  // Handles multibit flags as well
114  return (mask & flag) == flag;
115 }
116 
117 inline const char* MakeStaticString(const std::string &str)
118 {
119 #pragma clang diagnostic push
120 #pragma clang diagnostic ignored "-Wexit-time-destructors"
121  static std::set<std::string> strings;
122 #pragma clang diagnostic pop
123  std::set<std::string>::iterator it = strings.find(str);
124  if (it != strings.end())
125  {
126  return it->c_str();
127  }
128 
129  return strings.insert(str).first->c_str();
130 }
131 
132 inline std::string ArrayString(unsigned int i)
133 {
134  // We assume UINT_MAX and GL_INVALID_INDEX are equal
135  // See DynamicHLSL.cpp
136  if (i == UINT_MAX)
137  {
138  return "";
139  }
140 
141  std::stringstream strstr;
142 
143  strstr << "[";
144  strstr << i;
145  strstr << "]";
146 
147  return strstr.str();
148 }
149 
150 inline std::string Str(int i)
151 {
152  std::stringstream strstr;
153  strstr << i;
154  return strstr.str();
155 }
156 
157 size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
158 
159 std::string FormatString(const char *fmt, va_list vararg);
160 std::string FormatString(const char *fmt, ...);
161 
162 template <typename T>
164 {
165  std::ostringstream o;
166  o << value;
167  return o.str();
168 }
169 
170 // snprintf is not defined with MSVC prior to to msvc14
171 #if defined(_MSC_VER) && _MSC_VER < 1900
172 #define snprintf _snprintf
173 #endif
174 
175 #define GL_BGR565_ANGLEX 0x6ABB
176 #define GL_BGRA4_ANGLEX 0x6ABC
177 #define GL_BGR5_A1_ANGLEX 0x6ABD
178 #define GL_INT_64_ANGLEX 0x6ABE
179 #define GL_STRUCT_ANGLEX 0x6ABF
180 
181 // Hidden enum for the NULL D3D device type.
182 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
183 
184 #define ANGLE_TRY_CHECKED_MATH(result) \
185  if (!result.IsValid()) \
186  { \
187  return gl::Error(GL_INVALID_OPERATION, "Integer overflow."); \
188  }
189 
190 #endif // COMMON_ANGLEUTILS_H_
void SafeDelete(T *&resource)
Definition: angleutils.h:65
const char * MakeStaticString(const std::string &str)
Definition: angleutils.h:117
void StructZero(T *obj)
Definition: angleutils.h:105
uint32_t flag
Definition: ssl_lib.c:2732
~NonCopyable()=default
void operator=(const NonCopyable &)=delete
NonCopyable()=default
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1053
Definition: float-mm.c:54
bool IsMaskFlagSet(T mask, T flag)
Definition: angleutils.h:111
GLint GLuint mask
Definition: gl2.h:480
void SafeDeleteContainer(T &resource)
Definition: angleutils.h:72
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
Definition: Platform.h:33
_W64 unsigned int uintptr_t
Definition: stdint.h:161
std::string Str(int i)
Definition: angleutils.h:150
EGLAttrib * value
Definition: eglext.h:120
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
std::string ToString(const T &value)
Definition: angleutils.h:163
#define N
Definition: gcc-loops.cpp:14
bool StructEquals(const T &a, const T &b)
Definition: angleutils.h:99
constexpr size_t ArraySize(T(&)[N])
Definition: angleutils.h:40
str
Definition: make-dist.py:305
GLsizei const GLchar *const * string
Definition: gl2.h:479
size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector< char > &buffer)
Definition: angleutils.cpp:20
void SafeDeleteArray(T *&resource)
Definition: angleutils.h:82
for i
Definition: complexityMeasures.m:24
const uintptr_t DirtyPointer
Definition: angleutils.cpp:17
FmtSubchunk fmt
Definition: wav_header.cc:53
#define NULL
Definition: common_types.h:41
std::string FormatString(const char *fmt, va_list vararg)
Definition: angleutils.cpp:37
Definition: angleutils.h:26
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
std::string ArrayString(unsigned int i)
Definition: angleutils.h:132
void SafeRelease(T(&resourceBlock)[N])
Definition: angleutils.h:46
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:192
GLsizei const GLchar ** strings
Definition: gl2ext.h:1828
bool StructLessThan(const T &a, const T &b)
Definition: angleutils.h:91