blob: 50a4132a9aa315b16f7ec385327ee3d5c2b9b767 [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>
Jamie Madillf386bf72013-07-08 14:02:41 -040020
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// A macro to disallow the copy constructor and operator= functions
22// This must be used in the private: declarations for a class
23#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
24 TypeName(const TypeName&); \
25 void operator=(const TypeName&)
26
Jamie Madill33ea2f92014-08-29 15:15:01 -040027template <typename T, size_t N>
28inline size_t ArraySize(T(&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000029{
30 return N;
31}
32
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000033template <typename T, unsigned int N>
34void SafeRelease(T (&resourceBlock)[N])
35{
36 for (unsigned int i = 0; i < N; i++)
37 {
38 SafeRelease(resourceBlock[i]);
39 }
40}
41
42template <typename T>
43void SafeRelease(T& resource)
44{
45 if (resource)
46 {
47 resource->Release();
48 resource = NULL;
49 }
50}
51
Geoff Langea228632013-07-30 15:17:12 -040052template <typename T>
53void SafeDelete(T*& resource)
54{
55 delete resource;
56 resource = NULL;
57}
58
59template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040060void SafeDeleteContainer(T& resource)
61{
Shannon Woods3dc80202014-06-16 13:29:52 -040062 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040063 {
64 SafeDelete(*i);
65 }
66 resource.clear();
67}
68
69template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040070void SafeDeleteArray(T*& resource)
71{
72 delete[] resource;
73 resource = NULL;
74}
75
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040076// Provide a less-than function for comparing structs
77// Note: struct memory must be initialized to zero, because of packing gaps
78template <typename T>
79inline bool StructLessThan(const T &a, const T &b)
80{
81 return (memcmp(&a, &b, sizeof(T)) < 0);
82}
83
84// Provide a less-than function for comparing structs
85// Note: struct memory must be initialized to zero, because of packing gaps
86template <typename T>
87inline bool StructEquals(const T &a, const T &b)
88{
89 return (memcmp(&a, &b, sizeof(T)) == 0);
90}
91
92template <typename T>
93inline void StructZero(T *obj)
94{
95 memset(obj, 0, sizeof(T));
96}
97
Geoff Langcec35902014-04-16 10:52:36 -040098inline const char* MakeStaticString(const std::string &str)
99{
100 static std::set<std::string> strings;
101 std::set<std::string>::iterator it = strings.find(str);
102 if (it != strings.end())
103 {
104 return it->c_str();
105 }
106
107 return strings.insert(str).first->c_str();
108}
109
Jamie Madill53cb14d2014-07-08 15:02:35 -0400110inline std::string ArrayString(unsigned int i)
111{
112 // We assume UINT_MAX and GL_INVALID_INDEX are equal
113 // See DynamicHLSL.cpp
114 if (i == UINT_MAX)
115 {
116 return "";
117 }
118
119 std::stringstream strstr;
120
121 strstr << "[";
122 strstr << i;
123 strstr << "]";
124
125 return strstr.str();
126}
127
128inline std::string Str(int i)
129{
130 std::stringstream strstr;
131 strstr << i;
132 return strstr.str();
133}
134
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400135std::string FormatString(const char *fmt, va_list vararg);
136std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400137
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000138#if defined(_MSC_VER)
139#define snprintf _snprintf
140#endif
141
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000142#define VENDOR_ID_AMD 0x1002
143#define VENDOR_ID_INTEL 0x8086
144#define VENDOR_ID_NVIDIA 0x10DE
145
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000146#define GL_BGRA4_ANGLEX 0x6ABC
147#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400148#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400149#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000150
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000151#endif // COMMON_ANGLEUTILS_H_