// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // This file contains cross-platform basic type definitions #ifndef GPU_COMMAND_BUFFER_COMMON_TYPES_H_ #define GPU_COMMAND_BUFFER_COMMON_TYPES_H_ #if !defined(_MSC_VER) #include <stdint.h> #endif #include <cstddef> #include <string> typedef signed char schar; typedef signed char int8; // TODO(mbelshe) Remove these type guards. These are // temporary to avoid conflicts with npapi.h. #ifndef _INT16 #define _INT16 typedef short int16; #endif #ifndef _INT32 #define _INT32 typedef int int32; #endif // The NSPR system headers define 64-bit as |long| when possible. In order to // not have typedef mismatches, we do the same on LP64. #if defined(__LP64__) && !defined(__APPLE__) && !defined(__OpenBSD__) typedef long int64; #else typedef long long int64; #endif // NOTE: unsigned types are DANGEROUS in loops and other arithmetical // places. Use the signed types unless your variable represents a bit // pattern (eg a hash value) or you really need the extra bit. Do NOT // use 'unsigned' to express "this value should always be positive"; // use assertions for this. typedef unsigned char uint8; // TODO(mbelshe) Remove these type guards. These are // temporary to avoid conflicts with npapi.h. #ifndef _UINT16 #define _UINT16 typedef unsigned short uint16; #endif #ifndef _UINT32 #define _UINT32 typedef unsigned int uint32; #endif // See the comment above about NSPR and 64-bit. #if defined(__LP64__) && !defined(__APPLE__) && !defined(__OpenBSD__) typedef unsigned long uint64; #else typedef unsigned long long uint64; #endif // A macro to disallow the copy constructor and operator= functions // This should be used in the private: declarations for a class #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) // A macro to disallow all the implicit constructors, namely the // default constructor, copy constructor and operator= functions. // // This should be used in the private: declarations for a class // that wants to prevent anyone from instantiating it. This is // especially useful for classes containing only static methods. #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ TypeName(); \ DISALLOW_COPY_AND_ASSIGN(TypeName) // The arraysize(arr) macro returns the # of elements in an array arr. // The expression is a compile-time constant, and therefore can be // used in defining new arrays, for example. If you use arraysize on // a pointer by mistake, you will get a compile-time error. // // One caveat is that arraysize() doesn't accept any array of an // anonymous type or a type defined inside a function. In these rare // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is // due to a limitation in C++'s template system. The limitation might // eventually be removed, but it hasn't happened yet. // This template function declaration is used in defining arraysize. // Note that the function doesn't need an implementation, as we only // use its type. template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; // That gcc wants both of these prototypes seems mysterious. VC, for // its part, can't decide which to use (another mystery). Matching of // template overloads: the final frontier. #if !defined(_MSC_VER) template <typename T, size_t N> char (&ArraySizeHelper(const T (&array)[N]))[N]; #endif #define arraysize(array) (sizeof(ArraySizeHelper(array))) // The COMPILE_ASSERT macro can be used to verify that a compile time // expression is true. For example, you could use it to verify the // size of a static array: // // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, // content_type_names_incorrect_size); // // or to make sure a struct is smaller than a certain size: // // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); // // The second argument to the macro is the name of the variable. If // the expression is false, most compilers will issue a warning/error // containing the name of the variable. template <bool> struct GpuCompileAssert { }; #undef COMPILE_ASSERT #define COMPILE_ASSERT(expr, msg) \ typedef GpuCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] // Implementation details of COMPILE_ASSERT: // // - COMPILE_ASSERT works by defining an array type that has -1 // elements (and thus is invalid) when the expression is false. // // - The simpler definition // // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] // // does not work, as gcc supports variable-length arrays whose sizes // are determined at run-time (this is gcc's extension and not part // of the C++ standard). As a result, gcc fails to reject the // following code with the simple definition: // // int foo; // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is // // not a compile-time constant. // // - By using the type CompileAssert<(bool(expr))>, we ensures that // expr is a compile-time constant. (Template arguments must be // determined at compile-time.) // // - The outter parentheses in CompileAssert<(bool(expr))> are necessary // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written // // CompileAssert<bool(expr)> // // instead, these compilers will refuse to compile // // COMPILE_ASSERT(5 > 0, some_message); // // (They seem to think the ">" in "5 > 0" marks the end of the // template argument list.) // // - The array size is (bool(expr) ? 1 : -1), instead of simply // // ((expr) ? 1 : -1). // // This is to avoid running into a bug in MS VC 7.1, which // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. namespace gpu { #if defined(_MSC_VER) typedef short Int16; typedef unsigned short Uint16; typedef int Int32; typedef unsigned int Uint32; #else typedef int16_t Int16; typedef uint16_t Uint16; typedef int32_t Int32; typedef uint32_t Uint32; #endif typedef std::string String; } // namespace gpu #endif // GPU_COMMAND_BUFFER_COMMON_TYPES_H_