blob: e77fcc704f5f45b90a3714093e89b4073897a3e4 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00009#ifndef COMMON_ANGLEUTILS_H_
10#define COMMON_ANGLEUTILS_H_
11
Geoff Lang44fa7592014-05-30 11:50:07 -040012#include "common/platform.h"
13
Jamie Madillf386bf72013-07-08 14:02:41 -040014#include <stddef.h>
Jamie Madill93455eb2014-07-08 15:22:50 -040015#include <limits.h>
Geoff Langcec35902014-04-16 10:52:36 -040016#include <string>
17#include <set>
Jamie Madill53cb14d2014-07-08 15:02:35 -040018#include <sstream>
Geoff Langda5777c2014-07-11 09:52:58 -040019#include <cstdarg>
Austin Kinross922a9fb2014-10-21 14:26:33 -070020#include <vector>
Jamie Madillf386bf72013-07-08 14:02:41 -040021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022// 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
Jamie Madill33ea2f92014-08-29 15:15:01 -040028template <typename T, size_t N>
29inline size_t ArraySize(T(&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000030{
31 return N;
32}
33
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000034template <typename T, unsigned int N>
35void SafeRelease(T (&resourceBlock)[N])
36{
37 for (unsigned int i = 0; i < N; i++)
38 {
39 SafeRelease(resourceBlock[i]);
40 }
41}
42
43template <typename T>
44void SafeRelease(T& resource)
45{
46 if (resource)
47 {
48 resource->Release();
49 resource = NULL;
50 }
51}
52
Geoff Langea228632013-07-30 15:17:12 -040053template <typename T>
54void SafeDelete(T*& resource)
55{
56 delete resource;
57 resource = NULL;
58}
59
60template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040061void SafeDeleteContainer(T& resource)
62{
Shannon Woods3dc80202014-06-16 13:29:52 -040063 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040064 {
65 SafeDelete(*i);
66 }
67 resource.clear();
68}
69
70template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040071void SafeDeleteArray(T*& resource)
72{
73 delete[] resource;
74 resource = NULL;
75}
76
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040077// Provide a less-than function for comparing structs
78// Note: struct memory must be initialized to zero, because of packing gaps
79template <typename T>
80inline 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
87template <typename T>
88inline bool StructEquals(const T &a, const T &b)
89{
90 return (memcmp(&a, &b, sizeof(T)) == 0);
91}
92
93template <typename T>
94inline void StructZero(T *obj)
95{
96 memset(obj, 0, sizeof(T));
97}
98
Tibor den Ouden2221f472014-10-22 15:07:05 +020099template <typename T>
100inline bool IsMaskFlagSet(T mask, T flag)
101{
102 // Handles multibit flags as well
103 return (mask & flag) == flag;
104}
105
Geoff Langcec35902014-04-16 10:52:36 -0400106inline 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
Jamie Madill53cb14d2014-07-08 15:02:35 -0400118inline 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
136inline std::string Str(int i)
137{
138 std::stringstream strstr;
139 strstr << i;
140 return strstr.str();
141}
142
Austin Kinross922a9fb2014-10-21 14:26:33 -0700143size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
144
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400145std::string FormatString(const char *fmt, va_list vararg);
146std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400147
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000148#if defined(_MSC_VER)
149#define snprintf _snprintf
150#endif
151
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000152#define VENDOR_ID_AMD 0x1002
153#define VENDOR_ID_INTEL 0x8086
154#define VENDOR_ID_NVIDIA 0x10DE
155
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000156#define GL_BGRA4_ANGLEX 0x6ABC
157#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400158#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400159#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000160
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000161#endif // COMMON_ANGLEUTILS_H_