blob: 22c33bf0dc8d8a3cd5a4749d9b729715762a46a9 [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
Tibor den Ouden2221f472014-10-22 15:07:05 +020098template <typename T>
99inline bool IsMaskFlagSet(T mask, T flag)
100{
101 // Handles multibit flags as well
102 return (mask & flag) == flag;
103}
104
Geoff Langcec35902014-04-16 10:52:36 -0400105inline const char* MakeStaticString(const std::string &str)
106{
107 static std::set<std::string> strings;
108 std::set<std::string>::iterator it = strings.find(str);
109 if (it != strings.end())
110 {
111 return it->c_str();
112 }
113
114 return strings.insert(str).first->c_str();
115}
116
Jamie Madill53cb14d2014-07-08 15:02:35 -0400117inline std::string ArrayString(unsigned int i)
118{
119 // We assume UINT_MAX and GL_INVALID_INDEX are equal
120 // See DynamicHLSL.cpp
121 if (i == UINT_MAX)
122 {
123 return "";
124 }
125
126 std::stringstream strstr;
127
128 strstr << "[";
129 strstr << i;
130 strstr << "]";
131
132 return strstr.str();
133}
134
135inline std::string Str(int i)
136{
137 std::stringstream strstr;
138 strstr << i;
139 return strstr.str();
140}
141
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400142std::string FormatString(const char *fmt, va_list vararg);
143std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400144
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000145#if defined(_MSC_VER)
146#define snprintf _snprintf
147#endif
148
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000149#define VENDOR_ID_AMD 0x1002
150#define VENDOR_ID_INTEL 0x8086
151#define VENDOR_ID_NVIDIA 0x10DE
152
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000153#define GL_BGRA4_ANGLEX 0x6ABC
154#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400155#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400156#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000157
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000158#endif // COMMON_ANGLEUTILS_H_