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. 4 5 // This file contains cross-platform basic type definitions 6 7 #ifndef GPU_COMMAND_BUFFER_COMMON_TYPES_H_ 8 #define GPU_COMMAND_BUFFER_COMMON_TYPES_H_ 9 10 #if !defined(_MSC_VER) 11 #include <stdint.h> 12 #endif 13 #include <cstddef> 14 #include <string> 15 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. 20 #ifndef _INT16 21 #define _INT16 22 typedef short int16; 23 #endif 24 #ifndef _INT32 25 #define _INT32 26 typedef int int32; 27 #endif 28 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__) 32 typedef long int64; 33 #else 34 typedef long long int64; 35 #endif 36 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. 42 43 typedef unsigned char uint8; 44 // TODO(mbelshe) Remove these type guards. These are 45 // temporary to avoid conflicts with npapi.h. 46 #ifndef _UINT16 47 #define _UINT16 48 typedef unsigned short uint16; 49 #endif 50 #ifndef _UINT32 51 #define _UINT32 52 typedef unsigned int uint32; 53 #endif 54 55 // See the comment above about NSPR and 64-bit. 56 #if defined(__LP64__) && !defined(__APPLE__) && !defined(__OpenBSD__) 57 typedef unsigned long uint64; 58 #else 59 typedef unsigned long long uint64; 60 #endif 61 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&) 67 68 // A macro to disallow all the implicit constructors, namely the 69 // default constructor, copy constructor and operator= functions. 70 // 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) \ 75 TypeName(); \ 76 DISALLOW_COPY_AND_ASSIGN(TypeName) 77 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. 82 // 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. 88 89 // This template function declaration is used in defining arraysize. 90 // Note that the function doesn't need an implementation, as we only 91 // use its type. 92 template <typename T, size_t N> 93 char (&ArraySizeHelper(T (&array)[N]))[N]; 94 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]; 101 #endif 102 103 #define arraysize(array) (sizeof(ArraySizeHelper(array))) 104 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: 108 // 109 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, 110 // content_type_names_incorrect_size); 111 // 112 // or to make sure a struct is smaller than a certain size: 113 // 114 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 115 // 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. 119 120 template <bool> 121 struct GpuCompileAssert { 122 }; 123 124 #undef COMPILE_ASSERT 125 #define COMPILE_ASSERT(expr, msg) \ 126 typedef GpuCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 127 128 // Implementation details of COMPILE_ASSERT: 129 // 130 // - COMPILE_ASSERT works by defining an array type that has -1 131 // elements (and thus is invalid) when the expression is false. 132 // 133 // - The simpler definition 134 // 135 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 136 // 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: 141 // 142 // int foo; 143 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is 144 // // not a compile-time constant. 145 // 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.) 149 // 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 152 // 153 // CompileAssert<bool(expr)> 154 // 155 // instead, these compilers will refuse to compile 156 // 157 // COMPILE_ASSERT(5 > 0, some_message); 158 // 159 // (They seem to think the ">" in "5 > 0" marks the end of the 160 // template argument list.) 161 // 162 // - The array size is (bool(expr) ? 1 : -1), instead of simply 163 // 164 // ((expr) ? 1 : -1). 165 // 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. 168 169 namespace gpu { 170 #if defined(_MSC_VER) 171 typedef short Int16; 172 typedef unsigned short Uint16; 173 typedef int Int32; 174 typedef unsigned int Uint32; 175 #else 176 typedef int16_t Int16; 177 typedef uint16_t Uint16; 178 typedef int32_t Int32; 179 typedef uint32_t Uint32; 180 #endif 181 182 typedef std::string String; 183 184 } // namespace gpu 185 186 #endif // GPU_COMMAND_BUFFER_COMMON_TYPES_H_ 187