blob: 66b2789536523b4c87e67d56c583ad52efb59927 [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
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000024template <typename T, unsigned int N>
25inline unsigned int ArraySize(T(&)[N])
26{
27 return N;
28}
29
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000030template <typename T, unsigned int N>
31void SafeRelease(T (&resourceBlock)[N])
32{
33 for (unsigned int i = 0; i < N; i++)
34 {
35 SafeRelease(resourceBlock[i]);
36 }
37}
38
39template <typename T>
40void SafeRelease(T& resource)
41{
42 if (resource)
43 {
44 resource->Release();
45 resource = NULL;
46 }
47}
48
Geoff Langea228632013-07-30 15:17:12 -040049template <typename T>
50void SafeDelete(T*& resource)
51{
52 delete resource;
53 resource = NULL;
54}
55
56template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040057void SafeDeleteContainer(T& resource)
58{
Shannon Woods3dc80202014-06-16 13:29:52 -040059 for (typename T::iterator i = resource.begin(); i != resource.end(); i++)
Geoff Lang04fb89a2014-06-09 15:05:36 -040060 {
61 SafeDelete(*i);
62 }
63 resource.clear();
64}
65
66template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040067void SafeDeleteArray(T*& resource)
68{
69 delete[] resource;
70 resource = NULL;
71}
72
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040073// Provide a less-than function for comparing structs
74// Note: struct memory must be initialized to zero, because of packing gaps
75template <typename T>
76inline bool StructLessThan(const T &a, const T &b)
77{
78 return (memcmp(&a, &b, sizeof(T)) < 0);
79}
80
81// Provide a less-than function for comparing structs
82// Note: struct memory must be initialized to zero, because of packing gaps
83template <typename T>
84inline bool StructEquals(const T &a, const T &b)
85{
86 return (memcmp(&a, &b, sizeof(T)) == 0);
87}
88
89template <typename T>
90inline void StructZero(T *obj)
91{
92 memset(obj, 0, sizeof(T));
93}
94
Geoff Langcec35902014-04-16 10:52:36 -040095inline const char* MakeStaticString(const std::string &str)
96{
97 static std::set<std::string> strings;
98 std::set<std::string>::iterator it = strings.find(str);
99 if (it != strings.end())
100 {
101 return it->c_str();
102 }
103
104 return strings.insert(str).first->c_str();
105}
106
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000107#if defined(_MSC_VER)
108#define snprintf _snprintf
109#endif
110
apatrick@chromium.org85e44192012-08-17 20:58:01 +0000111#define VENDOR_ID_AMD 0x1002
112#define VENDOR_ID_INTEL 0x8086
113#define VENDOR_ID_NVIDIA 0x10DE
114
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000115#define GL_BGRA4_ANGLEX 0x6ABC
116#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400117#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400118#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000119
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000120#endif // COMMON_ANGLEUTILS_H_