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