blob: 1cec9206a4250121cb890f8692f4d2b39aea2c2f [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>
Geoff Langcec35902014-04-16 10:52:36 -040015#include <string>
16#include <set>
Jamie Madillf386bf72013-07-08 14:02:41 -040017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018// A macro to disallow the copy constructor and operator= functions
19// This must be used in the private: declarations for a class
20#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
21 TypeName(const TypeName&); \
22 void operator=(const TypeName&)
23
Ehsan Akhgari10530c32014-07-02 20:37:53 -040024// Macros for writing try catch blocks. Defining ANGLE_NO_EXCEPTIONS
25// when building angle will disable the usage of try/catch for compilers
26// without proper support for them (such as clang-cl).
27#ifdef ANGLE_NO_EXCEPTIONS
28#define ANGLE_TRY if (true)
29#define ANGLE_CATCH_ALL else
30#else
31#define ANGLE_TRY try
32#define ANGLE_CATCH_ALL catch(...)
33#endif
34
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000035template <typename T, unsigned int N>
36inline unsigned int ArraySize(T(&)[N])
37{
38 return N;
39}
40
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000041template <typename T, unsigned int N>
42void SafeRelease(T (&resourceBlock)[N])
43{
44 for (unsigned int i = 0; i < N; i++)
45 {
46 SafeRelease(resourceBlock[i]);
47 }
48}
49
50template <typename T>
51void SafeRelease(T& resource)
52{
53 if (resource)
54 {
55 resource->Release();
56 resource = NULL;
57 }
58}
59
Geoff Langea228632013-07-30 15:17:12 -040060template <typename T>
61void SafeDelete(T*& resource)
62{
63 delete resource;
64 resource = NULL;
65}
66
67template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040068void SafeDeleteContainer(T& resource)
69{
Shannon Woods3dc80202014-06-16 13:29:52 -040070 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040071 {
72 SafeDelete(*i);
73 }
74 resource.clear();
75}
76
77template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040078void SafeDeleteArray(T*& resource)
79{
80 delete[] resource;
81 resource = NULL;
82}
83
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040084// 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 StructLessThan(const T &a, const T &b)
88{
89 return (memcmp(&a, &b, sizeof(T)) < 0);
90}
91
92// Provide a less-than function for comparing structs
93// Note: struct memory must be initialized to zero, because of packing gaps
94template <typename T>
95inline bool StructEquals(const T &a, const T &b)
96{
97 return (memcmp(&a, &b, sizeof(T)) == 0);
98}
99
100template <typename T>
101inline void StructZero(T *obj)
102{
103 memset(obj, 0, sizeof(T));
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
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000118#if defined(_MSC_VER)
119#define snprintf _snprintf
120#endif
121
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000122#define VENDOR_ID_AMD 0x1002
123#define VENDOR_ID_INTEL 0x8086
124#define VENDOR_ID_NVIDIA 0x10DE
125
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000126#define GL_BGRA4_ANGLEX 0x6ABC
127#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400128#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400129#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000130
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000131#endif // COMMON_ANGLEUTILS_H_