1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains cross-platform basic type definitions
7 #ifndef GPU_COMMAND_BUFFER_COMMON_TYPES_H_
8 #define GPU_COMMAND_BUFFER_COMMON_TYPES_H_
10 #if !defined(_MSC_VER)
16 typedef signed char schar
;
17 typedef signed char int8
;
18 // TODO(mbelshe) Remove these type guards. These are
19 // temporary to avoid conflicts with npapi.h.
29 // The NSPR system headers define 64-bit as |long| when possible. In order to
30 // not have typedef mismatches, we do the same on LP64.
31 #if defined(__LP64__) && !defined(__APPLE__) && !defined(__OpenBSD__)
34 typedef long long int64
;
37 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
38 // places. Use the signed types unless your variable represents a bit
39 // pattern (eg a hash value) or you really need the extra bit. Do NOT
40 // use 'unsigned' to express "this value should always be positive";
41 // use assertions for this.
43 typedef unsigned char uint8
;
44 // TODO(mbelshe) Remove these type guards. These are
45 // temporary to avoid conflicts with npapi.h.
48 typedef unsigned short uint16
;
52 typedef unsigned int uint32
;
55 // See the comment above about NSPR and 64-bit.
56 #if defined(__LP64__) && !defined(__APPLE__) && !defined(__OpenBSD__)
57 typedef unsigned long uint64
;
59 typedef unsigned long long uint64
;
62 // A macro to disallow the copy constructor and operator= functions
63 // This should be used in the private: declarations for a class
64 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
65 TypeName(const TypeName&); \
66 void operator=(const TypeName&)
68 // A macro to disallow all the implicit constructors, namely the
69 // default constructor, copy constructor and operator= functions.
71 // This should be used in the private: declarations for a class
72 // that wants to prevent anyone from instantiating it. This is
73 // especially useful for classes containing only static methods.
74 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
76 DISALLOW_COPY_AND_ASSIGN(TypeName)
78 // The arraysize(arr) macro returns the # of elements in an array arr.
79 // The expression is a compile-time constant, and therefore can be
80 // used in defining new arrays, for example. If you use arraysize on
81 // a pointer by mistake, you will get a compile-time error.
83 // One caveat is that arraysize() doesn't accept any array of an
84 // anonymous type or a type defined inside a function. In these rare
85 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
86 // due to a limitation in C++'s template system. The limitation might
87 // eventually be removed, but it hasn't happened yet.
89 // This template function declaration is used in defining arraysize.
90 // Note that the function doesn't need an implementation, as we only
92 template <typename T
, size_t N
>
93 char (&ArraySizeHelper(T (&array
)[N
]))[N
];
95 // That gcc wants both of these prototypes seems mysterious. VC, for
96 // its part, can't decide which to use (another mystery). Matching of
97 // template overloads: the final frontier.
98 #if !defined(_MSC_VER)
99 template <typename T
, size_t N
>
100 char (&ArraySizeHelper(const T (&array
)[N
]))[N
];
103 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
105 // The COMPILE_ASSERT macro can be used to verify that a compile time
106 // expression is true. For example, you could use it to verify the
107 // size of a static array:
109 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
110 // content_type_names_incorrect_size);
112 // or to make sure a struct is smaller than a certain size:
114 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
116 // The second argument to the macro is the name of the variable. If
117 // the expression is false, most compilers will issue a warning/error
118 // containing the name of the variable.
121 struct GpuCompileAssert
{
124 #undef COMPILE_ASSERT
125 #define COMPILE_ASSERT(expr, msg) \
126 typedef GpuCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
128 // Implementation details of COMPILE_ASSERT:
130 // - COMPILE_ASSERT works by defining an array type that has -1
131 // elements (and thus is invalid) when the expression is false.
133 // - The simpler definition
135 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
137 // does not work, as gcc supports variable-length arrays whose sizes
138 // are determined at run-time (this is gcc's extension and not part
139 // of the C++ standard). As a result, gcc fails to reject the
140 // following code with the simple definition:
143 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
144 // // not a compile-time constant.
146 // - By using the type CompileAssert<(bool(expr))>, we ensures that
147 // expr is a compile-time constant. (Template arguments must be
148 // determined at compile-time.)
150 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
151 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
153 // CompileAssert<bool(expr)>
155 // instead, these compilers will refuse to compile
157 // COMPILE_ASSERT(5 > 0, some_message);
159 // (They seem to think the ">" in "5 > 0" marks the end of the
160 // template argument list.)
162 // - The array size is (bool(expr) ? 1 : -1), instead of simply
164 // ((expr) ? 1 : -1).
166 // This is to avoid running into a bug in MS VC 7.1, which
167 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
170 #if defined(_MSC_VER)
172 typedef unsigned short Uint16
;
174 typedef unsigned int Uint32
;
176 typedef int16_t Int16
;
177 typedef uint16_t Uint16
;
178 typedef int32_t Int32
;
179 typedef uint32_t Uint32
;
182 typedef std::string String
;
186 #endif // GPU_COMMAND_BUFFER_COMMON_TYPES_H_