Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / common / angleutils.h
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
20 // A macro to disallow the copy constructor and operator= functions
21 // This must be used in the private: declarations for a class
22 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
23   TypeName(const TypeName&);               \
24   void operator=(const TypeName&)
25
26 template <typename T, unsigned int N>
27 inline unsigned int ArraySize(T(&)[N])
28 {
29     return N;
30 }
31
32 template <typename T, unsigned int N>
33 void SafeRelease(T (&resourceBlock)[N])
34 {
35     for (unsigned int i = 0; i < N; i++)
36     {
37         SafeRelease(resourceBlock[i]);
38     }
39 }
40
41 template <typename T>
42 void SafeRelease(T& resource)
43 {
44     if (resource)
45     {
46         resource->Release();
47         resource = NULL;
48     }
49 }
50
51 template <typename T>
52 void SafeDelete(T*& resource)
53 {
54     delete resource;
55     resource = NULL;
56 }
57
58 template <typename T>
59 void SafeDeleteContainer(T& resource)
60 {
61     for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
62     {
63         SafeDelete(*i);
64     }
65     resource.clear();
66 }
67
68 template <typename T>
69 void SafeDeleteArray(T*& resource)
70 {
71     delete[] resource;
72     resource = NULL;
73 }
74
75 // Provide a less-than function for comparing structs
76 // Note: struct memory must be initialized to zero, because of packing gaps
77 template <typename T>
78 inline bool StructLessThan(const T &a, const T &b)
79 {
80     return (memcmp(&a, &b, sizeof(T)) < 0);
81 }
82
83 // Provide a less-than function for comparing structs
84 // Note: struct memory must be initialized to zero, because of packing gaps
85 template <typename T>
86 inline bool StructEquals(const T &a, const T &b)
87 {
88     return (memcmp(&a, &b, sizeof(T)) == 0);
89 }
90
91 template <typename T>
92 inline void StructZero(T *obj)
93 {
94     memset(obj, 0, sizeof(T));
95 }
96
97 inline const char* MakeStaticString(const std::string &str)
98 {
99     static std::set<std::string> strings;
100     std::set<std::string>::iterator it = strings.find(str);
101     if (it != strings.end())
102     {
103         return it->c_str();
104     }
105
106     return strings.insert(str).first->c_str();
107 }
108
109 inline std::string ArrayString(unsigned int i)
110 {
111     // We assume UINT_MAX and GL_INVALID_INDEX are equal
112     // See DynamicHLSL.cpp
113     if (i == UINT_MAX)
114     {
115         return "";
116     }
117
118     std::stringstream strstr;
119
120     strstr << "[";
121     strstr << i;
122     strstr << "]";
123
124     return strstr.str();
125 }
126
127 inline std::string Str(int i)
128 {
129     std::stringstream strstr;
130     strstr << i;
131     return strstr.str();
132 }
133
134 #if defined(_MSC_VER)
135 #define snprintf _snprintf
136 #endif
137
138 #define VENDOR_ID_AMD 0x1002
139 #define VENDOR_ID_INTEL 0x8086
140 #define VENDOR_ID_NVIDIA 0x10DE
141
142 #define GL_BGRA4_ANGLEX 0x6ABC
143 #define GL_BGR5_A1_ANGLEX 0x6ABD
144 #define GL_INT_64_ANGLEX 0x6ABE
145 #define GL_STRUCT_ANGLEX 0x6ABF
146
147 #endif // COMMON_ANGLEUTILS_H_