114 lines
4.6 KiB
C++
114 lines
4.6 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2016 Google Inc.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef BASE_MACROS_H_
|
|
#define BASE_MACROS_H_
|
|
|
|
#include <stddef.h> // For size_t
|
|
|
|
// DISALLOW_COPY_AND_ASSIGN disallows the copy constructor and copy assignment
|
|
// operator. DISALLOW_IMPLICIT_CONSTRUCTORS is like DISALLOW_COPY_AND_ASSIGN,
|
|
// but also disallows the default constructor, intended to help make a
|
|
// class uninstantiable.
|
|
//
|
|
// These must be placed in the private: declarations for a class.
|
|
//
|
|
// Note: New code should prefer static_assert over COMPILE_ASSERT.
|
|
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
|
TypeName(const TypeName&) = delete; \
|
|
TypeName& operator=(const TypeName&) = delete
|
|
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
|
TypeName() = delete; \
|
|
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.
|
|
//
|
|
// 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.
|
|
#ifndef COMPILER_MSVC
|
|
template <typename T, size_t N>
|
|
char (&ArraySizeHelper(const T (&array)[N]))[N];
|
|
#endif
|
|
|
|
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
|
|
|
|
// A macro to turn a symbol into a std::string
|
|
#define AS_STRING(x) AS_STRING_INTERNAL(x)
|
|
#define AS_STRING_INTERNAL(x) #x
|
|
|
|
// The following enum should be used only as a constructor argument to indicate
|
|
// that the variable has static storage class, and that the constructor should
|
|
// do nothing to its state. It indicates to the reader that it is legal to
|
|
// declare a static instance of the class, provided the constructor is given
|
|
// the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
|
|
// static variable that has a constructor or a destructor because invocation
|
|
// order is undefined. However, IF the type can be initialized by filling with
|
|
// zeroes (which the loader does for static variables), AND the type's
|
|
// destructor does nothing to the storage, then a constructor for static
|
|
// initialization can be declared as
|
|
// explicit MyClass(base::LinkerInitialized x) {}
|
|
// and invoked as
|
|
// static MyClass my_variable_name(base::LINKER_INITIALIZED);
|
|
namespace base {
|
|
enum LinkerInitialized { LINKER_INITIALIZED };
|
|
}
|
|
|
|
// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
|
|
// between switch labels:
|
|
// switch (x) {
|
|
// case 40:
|
|
// case 41:
|
|
// if (truth_is_out_there) {
|
|
// ++x;
|
|
// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
|
|
// // comments.
|
|
// } else {
|
|
// return x;
|
|
// }
|
|
// case 42:
|
|
// ...
|
|
//
|
|
// As shown in the example above, the FALLTHROUGH_INTENDED macro should be
|
|
// followed by a semicolon. It is designed to mimic control-flow statements
|
|
// like 'break;', so it can be placed in most places where 'break;' can, but
|
|
// only if there are no statements on the execution path between it and the
|
|
// next switch label.
|
|
//
|
|
// When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
|
|
// expanded to [[clang::fallthrough]] attribute, which is analysed when
|
|
// performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
|
|
// See clang documentation on language extensions for details:
|
|
// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
|
|
//
|
|
// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
|
|
// effect on diagnostics.
|
|
//
|
|
// In either case this macro has no effect on runtime behavior and performance
|
|
// of code.
|
|
#if defined(__clang__) && defined(LANG_CXX11) && defined(__has_warning)
|
|
#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
|
|
#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef FALLTHROUGH_INTENDED
|
|
#define FALLTHROUGH_INTENDED do { } while (0)
|
|
#endif
|
|
|
|
#endif // BASE_MACROS_H_
|