blob: 927dea035b46ccffa0b670f8778fcda62cc6d44a [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 Madill55573e12014-11-25 11:21:13 -050014#include <climits>
15#include <cstdarg>
16#include <cstddef>
Geoff Langcec35902014-04-16 10:52:36 -040017#include <string>
18#include <set>
Jamie Madill53cb14d2014-07-08 15:02:35 -040019#include <sstream>
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
Geoff Lange371f1d2014-11-21 11:25:22 -050024#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
25 TypeName(const TypeName&) = delete; \
26 TypeName(TypeName&&) = delete; \
27 void operator=(const TypeName&) = delete; \
28 void operator=(TypeName&&) = delete;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029
Jamie Madill33ea2f92014-08-29 15:15:01 -040030template <typename T, size_t N>
31inline size_t ArraySize(T(&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000032{
33 return N;
34}
35
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000036template <typename T, unsigned int N>
37void SafeRelease(T (&resourceBlock)[N])
38{
39 for (unsigned int i = 0; i < N; i++)
40 {
41 SafeRelease(resourceBlock[i]);
42 }
43}
44
45template <typename T>
46void SafeRelease(T& resource)
47{
48 if (resource)
49 {
50 resource->Release();
51 resource = NULL;
52 }
53}
54
Geoff Langea228632013-07-30 15:17:12 -040055template <typename T>
56void SafeDelete(T*& resource)
57{
58 delete resource;
59 resource = NULL;
60}
61
62template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040063void SafeDeleteContainer(T& resource)
64{
Shannon Woods3dc80202014-06-16 13:29:52 -040065 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040066 {
67 SafeDelete(*i);
68 }
69 resource.clear();
70}
71
72template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040073void SafeDeleteArray(T*& resource)
74{
75 delete[] resource;
76 resource = NULL;
77}
78
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040079// Provide a less-than function for comparing structs
80// Note: struct memory must be initialized to zero, because of packing gaps
81template <typename T>
82inline bool StructLessThan(const T &a, const T &b)
83{
84 return (memcmp(&a, &b, sizeof(T)) < 0);
85}
86
87// Provide a less-than function for comparing structs
88// Note: struct memory must be initialized to zero, because of packing gaps
89template <typename T>
90inline bool StructEquals(const T &a, const T &b)
91{
92 return (memcmp(&a, &b, sizeof(T)) == 0);
93}
94
95template <typename T>
96inline void StructZero(T *obj)
97{
98 memset(obj, 0, sizeof(T));
99}
100
Tibor den Ouden2221f472014-10-22 15:07:05 +0200101template <typename T>
102inline bool IsMaskFlagSet(T mask, T flag)
103{
104 // Handles multibit flags as well
105 return (mask & flag) == flag;
106}
107
Geoff Langcec35902014-04-16 10:52:36 -0400108inline const char* MakeStaticString(const std::string &str)
109{
110 static std::set<std::string> strings;
111 std::set<std::string>::iterator it = strings.find(str);
112 if (it != strings.end())
113 {
114 return it->c_str();
115 }
116
117 return strings.insert(str).first->c_str();
118}
119
Jamie Madill53cb14d2014-07-08 15:02:35 -0400120inline std::string ArrayString(unsigned int i)
121{
122 // We assume UINT_MAX and GL_INVALID_INDEX are equal
123 // See DynamicHLSL.cpp
124 if (i == UINT_MAX)
125 {
126 return "";
127 }
128
129 std::stringstream strstr;
130
131 strstr << "[";
132 strstr << i;
133 strstr << "]";
134
135 return strstr.str();
136}
137
138inline std::string Str(int i)
139{
140 std::stringstream strstr;
141 strstr << i;
142 return strstr.str();
143}
144
Austin Kinross922a9fb2014-10-21 14:26:33 -0700145size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
146
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400147std::string FormatString(const char *fmt, va_list vararg);
148std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400149
Geoff Langdf8fafe2014-11-11 11:11:33 -0500150// snprintf is not defined with MSVC prior to to msvc14
151#if defined(_MSC_VER) && _MSC_VER < 1900
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000152#define snprintf _snprintf
153#endif
154
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000155#define GL_BGRA4_ANGLEX 0x6ABC
156#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400157#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400158#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000159
Geoff Lang7825f612014-11-26 16:19:41 -0500160// Hidden enum for the NULL D3D device type.
161#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
162
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000163#endif // COMMON_ANGLEUTILS_H_