Home | History | Annotate | Download | only in api
      1 /* Copyright (c) 2012 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 
      6 /**
      7  * Defines the common macros such as assert, inline, ...
      8  */
      9 
     10 #inline c
     11 
     12 /*
     13  * @addtogroup PP
     14  * @{
     15  */
     16 
     17 /* Use PP_INLINE to tell the compiler to inline functions.  The main purpose of
     18  * inline functions in ppapi is to allow us to define convenience functions in
     19  * the ppapi header files, without requiring clients or implementers to link a
     20  * PPAPI C library.  The "inline" keyword is not supported by pre-C99 C
     21  * compilers (such as MS Visual Studio 2008 and older versions of GCC).  MSVS
     22  * supports __forceinline and GCC supports __inline__.  Use of the static
     23  * keyword ensures (in C) that the function is not compiled on its own, which
     24  * could cause multiple definition errors.
     25  *  http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx
     26  *  http://gcc.gnu.org/onlinedocs/gcc/Inline.html
     27  */
     28 #if defined(__cplusplus)
     29 /* The inline keyword is part of C++ and guarantees we won't get multiple
     30  * definition errors.
     31  */
     32 # define PP_INLINE inline
     33 #else
     34 # if defined(_MSC_VER)
     35 #  define PP_INLINE static __forceinline
     36 # else
     37 #  define PP_INLINE static __inline__
     38 # endif
     39 #endif
     40 
     41 /* This is a compile-time assertion useful for ensuring that a given type is
     42    a given number of bytes wide.  The size of the array is designed to be 1
     43    (which should always be valid) if the enum's size is SIZE, and otherwise the
     44    size of the array will be -1 (which all/most compilers should flag as an
     45    error).  This is wrapped inside a struct, because if it is a simple global
     46    we get multiple definition errors at link time.
     47 
     48    NAME is the name of the type without any spaces or the struct or enum
     49    keywords.
     50 
     51    CTYPENAME is the typename required by C.  I.e., for a struct or enum, the
     52    appropriate keyword must be included.
     53 
     54    SIZE is the expected size in bytes.
     55  */
     56 #define PP_COMPILE_ASSERT_SIZE_IN_BYTES_IMPL(NAME, CTYPENAME, SIZE) \
     57 struct PP_Dummy_Struct_For_##NAME { \
     58 char _COMPILE_ASSERT_FAILED_The_type_named_ \
     59 ## NAME ## _is_not_ ## SIZE ## \
     60 _bytes_wide[(sizeof(CTYPENAME) == SIZE) ? 1 : -1]; }
     61 
     62 /* PP_COMPILE_ASSERT_SIZE_IN_BYTES is for typenames that contain no spaces.
     63    E.g.:
     64    PP_COMPILE_ASSERT_SIZE_IN_BYTES(int, 4);
     65    typedef struct { int a; } Foo;
     66    PP_COMPILE_ASSERT_SIZE_IN_BYTES(Foo, 4);
     67  */
     68 #define PP_COMPILE_ASSERT_SIZE_IN_BYTES(NAME, SIZE) \
     69 PP_COMPILE_ASSERT_SIZE_IN_BYTES_IMPL(NAME, NAME, SIZE)
     70 
     71 /* PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES is for typenames that contain 'struct'
     72    in C.  That is, struct names that are not typedefs.
     73    E.g.:
     74    struct Foo { int a; };
     75    PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(Foo, 4);
     76  */
     77 #define PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(NAME, SIZE) \
     78 PP_COMPILE_ASSERT_SIZE_IN_BYTES_IMPL(NAME, struct NAME, SIZE)
     79 
     80 /* PP_COMPILE_ASSERT_ENUM_SIZE_IN_BYTES is for typenames that contain 'enum'
     81    in C.  That is, enum names that are not typedefs.
     82    E.g.:
     83    enum Bar { A = 0, B = 1 };
     84    PP_COMPILE_ASSERT_ENUM_SIZE_IN_BYTES(Foo, 4);
     85  */
     86 #define PP_COMPILE_ASSERT_ENUM_SIZE_IN_BYTES(NAME, SIZE) \
     87 PP_COMPILE_ASSERT_SIZE_IN_BYTES_IMPL(NAME, enum NAME, SIZE)
     88 
     89 /* This is roughly copied from base/compiler_specific.h, and makes it possible
     90    to pass 'this' in a constructor initializer list, when you really mean it.
     91    E.g.:
     92    Foo::Foo(MyInstance* instance)
     93        : PP_ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) {}
     94  */
     95 #if defined(_MSC_VER)
     96 # define PP_ALLOW_THIS_IN_INITIALIZER_LIST(code) \
     97     __pragma(warning(push)) \
     98     __pragma(warning(disable:4355)) \
     99     code \
    100     __pragma(warning(pop))
    101 #else
    102 # define PP_ALLOW_THIS_IN_INITIALIZER_LIST(code) code
    103 #endif
    104 
    105 /**
    106  * @}
    107  * End of addtogroup PP
    108  */
    109 
    110 #endinl
    111