Skip to content

Commit

Permalink
Remove all the duplicate macros and types in command_buffer/common/ty…
Browse files Browse the repository at this point in the history
…pes.h.

(The motivation is that base's COMPILE_ASSERT uses static_assert if
available, while this file's COMPILE_ASSERT didn't.)

BUG=321833
NOTRY=true

Review URL: https://codereview.chromium.org/267963004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@268123 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
thakis@chromium.org committed May 4, 2014
1 parent 8c64991 commit f7fa611
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 184 deletions.
8 changes: 4 additions & 4 deletions gpu/command_buffer/common/cmd_buffer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ inline size_t RoundSizeToMultipleOfEntries(size_t size_in_bytes) {

// Struct that defines the command header in the command buffer.
struct CommandHeader {
Uint32 size:21;
Uint32 command:11;
uint32 size:21;
uint32 command:11;

GPU_EXPORT static const int32 kMaxSize = (1 << 21) - 1;

Expand Down Expand Up @@ -82,8 +82,8 @@ COMPILE_ASSERT(sizeof(CommandHeader) == 4, Sizeof_CommandHeader_is_not_4);
// Union that defines possible command buffer entries.
union CommandBufferEntry {
CommandHeader value_header;
Uint32 value_uint32;
Int32 value_int32;
uint32 value_uint32;
int32 value_int32;
float value_float;
};

Expand Down
180 changes: 4 additions & 176 deletions gpu/command_buffer/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,185 +2,13 @@
// 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
// TODO(thakis): Change all files that include this file to instead include the
// base files below directly and remove this file.

#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
#include "base/basictypes.h"
#include "base/macros.h"

#endif // GPU_COMMAND_BUFFER_COMMON_TYPES_H_
8 changes: 4 additions & 4 deletions gpu/command_buffer/service/gles2_cmd_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4706,7 +4706,7 @@ error::Error GLES2DecoderImpl::HandleBindAttribLocation(
if (name == NULL) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
std::string name_str(name, name_size);
DoBindAttribLocation(program, index, name_str.c_str());
return error::kNoError;
}
Expand Down Expand Up @@ -4771,7 +4771,7 @@ error::Error GLES2DecoderImpl::HandleBindUniformLocationCHROMIUM(
if (name == NULL) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
std::string name_str(name, name_size);
DoBindUniformLocationCHROMIUM(program, location, name_str.c_str());
return error::kNoError;
}
Expand Down Expand Up @@ -7621,7 +7621,7 @@ error::Error GLES2DecoderImpl::HandleGetAttribLocation(
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
std::string name_str(name, name_size);
return GetAttribLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
Expand Down Expand Up @@ -7680,7 +7680,7 @@ error::Error GLES2DecoderImpl::HandleGetUniformLocation(
if (!name) {
return error::kOutOfBounds;
}
String name_str(name, name_size);
std::string name_str(name, name_size);
return GetUniformLocationHelper(
c.program, c.location_shm_id, c.location_shm_offset, name_str);
}
Expand Down

0 comments on commit f7fa611

Please sign in to comment.