blob: 7701a4268572cd59aaa201217aa35a764333e2b8 [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>
Jamie Madillf386bf72013-07-08 14:02:41 -040019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020// 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
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000026template <typename T, unsigned int N>
27inline unsigned int ArraySize(T(&)[N])
28{
29 return N;
30}
31
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000032template <typename T, unsigned int N>
33void SafeRelease(T (&resourceBlock)[N])
34{
35 for (unsigned int i = 0; i < N; i++)
36 {
37 SafeRelease(resourceBlock[i]);
38 }
39}
40
41template <typename T>
42void SafeRelease(T& resource)
43{
44 if (resource)
45 {
46 resource->Release();
47 resource = NULL;
48 }
49}
50
Geoff Langea228632013-07-30 15:17:12 -040051template <typename T>
52void SafeDelete(T*& resource)
53{
54 delete resource;
55 resource = NULL;
56}
57
58template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040059void SafeDeleteContainer(T& resource)
60{
Shannon Woods3dc80202014-06-16 13:29:52 -040061 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040062 {
63 SafeDelete(*i);
64 }
65 resource.clear();
66}
67
68template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040069void SafeDeleteArray(T*& resource)
70{
71 delete[] resource;
72 resource = NULL;
73}
74
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040075// Provide a less-than function for comparing structs
76// Note: struct memory must be initialized to zero, because of packing gaps
77template <typename T>
78inline 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
85template <typename T>
86inline bool StructEquals(const T &a, const T &b)
87{
88 return (memcmp(&a, &b, sizeof(T)) == 0);
89}
90
91template <typename T>
92inline void StructZero(T *obj)
93{
94 memset(obj, 0, sizeof(T));
95}
96
Geoff Langcec35902014-04-16 10:52:36 -040097inline 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
Jamie Madill53cb14d2014-07-08 15:02:35 -0400109inline 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
127inline std::string Str(int i)
128{
129 std::stringstream strstr;
130 strstr << i;
131 return strstr.str();
132}
133
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000134#if defined(_MSC_VER)
135#define snprintf _snprintf
136#endif
137
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000138#define VENDOR_ID_AMD 0x1002
139#define VENDOR_ID_INTEL 0x8086
140#define VENDOR_ID_NVIDIA 0x10DE
141
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000142#define GL_BGRA4_ANGLEX 0x6ABC
143#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400144#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400145#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000146
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000147#endif // COMMON_ANGLEUTILS_H_