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
23namespace angle
24{
25
26class NonCopyable
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}
37
38template <typename T, size_t N>
39inline size_t ArraySize(T(&)[N])
40{
41 return N;
42}
43
44template <typename T, unsigned int N>
45void SafeRelease(T (&resourceBlock)[N])
46{
47 for (unsigned int i = 0; i < N; i++)
48 {
49 SafeRelease(resourceBlock[i]);
50 }
51}
52
53template <typename T>
54void SafeRelease(T& resource)
55{
56 if (resource)
57 {
58 resource->Release();
59 resource = NULL;
60 }
61}
62
63template <typename T>
64void SafeDelete(T*& resource)
65{
66 delete resource;
67 resource = NULL;
68}
69
70template <typename T>
71void SafeDeleteContainer(T& resource)
72{
73 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
74 {
75 SafeDelete(*i);
76 }
77 resource.clear();
78}
79
80template <typename T>
81void SafeDeleteArray(T*& resource)
82{
83 delete[] resource;
84 resource = NULL;
85}
86
87// Provide a less-than function for comparing structs
88// Note: struct memory must be initialized to zero, because of packing gaps
89template <typename T>
90inline bool StructLessThan(const T &a, const T &b)
91{
92 return (memcmp(&a, &b, sizeof(T)) < 0);
93}
94
95// Provide a less-than function for comparing structs
96// Note: struct memory must be initialized to zero, because of packing gaps
97template <typename T>
98inline bool StructEquals(const T &a, const T &b)
99{
100 return (memcmp(&a, &b, sizeof(T)) == 0);
101}
102
103template <typename T>
104inline void StructZero(T *obj)
105{
106 memset(obj, 0, sizeof(T));
107}
108
109template <typename T>
110inline bool IsMaskFlagSet(T mask, T flag)
111{
112 // Handles multibit flags as well
113 return (mask & flag) == flag;
114}
115
116inline const char* MakeStaticString(const std::string &str)
117{
118 static std::set<std::string> strings;
119 std::set<std::string>::iterator it = strings.find(str);
120 if (it != strings.end())
121 {
122 return it->c_str();
123 }
124
125 return strings.insert(str).first->c_str();
126}
127
128inline std::string ArrayString(unsigned int i)
129{
130 // We assume UINT_MAX and GL_INVALID_INDEX are equal
131 // See DynamicHLSL.cpp
132 if (i == UINT_MAX)
133 {
134 return "";
135 }
136
137 std::stringstream strstr;
138
139 strstr << "[";
140 strstr << i;
141 strstr << "]";
142
143 return strstr.str();
144}
145
146inline std::string Str(int i)
147{
148 std::stringstream strstr;
149 strstr << i;
150 return strstr.str();
151}
152
153size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
154
155std::string FormatString(const char *fmt, va_list vararg);
156std::string FormatString(const char *fmt, ...);
157
158// snprintf is not defined with MSVC prior to to msvc14
159#if defined(_MSC_VER) && _MSC_VER < 1900
160#define snprintf _snprintf
161#endif
162
163#define GL_BGRA4_ANGLEX 0x6ABC
164#define GL_BGR5_A1_ANGLEX 0x6ABD
165#define GL_INT_64_ANGLEX 0x6ABE
166#define GL_STRUCT_ANGLEX 0x6ABF
167
168// Hidden enum for the NULL D3D device type.
169#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
170
171#endif // COMMON_ANGLEUTILS_H_
172