1 // Copyright 2014 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.
5 #ifndef MOJO_PUBLIC_C_SYSTEM_MACROS_H_
6 #define MOJO_PUBLIC_C_SYSTEM_MACROS_H_
10 // Annotate a variable indicating it's okay if it's unused.
13 // MOJO_ALLOW_UNUSED_LOCAL(x);
14 #define MOJO_ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0
16 // Annotate a function indicating that the caller must examine the return value.
18 // int foo() MOJO_WARN_UNUSED_RESULT;
19 // Note that it can only be used on the prototype, and not the definition.
21 #define MOJO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
23 #define MOJO_WARN_UNUSED_RESULT
27 // Used to explicitly mark the return value of a function as unused. If you are
28 // really sure you don't want to do anything with the return value of a function
29 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
31 // scoped_ptr<MyType> my_var = ...;
32 // if (TakeOwnership(my_var.get()) == SUCCESS)
33 // mojo_ignore_result(my_var.release());
36 inline void mojo_ignore_result(const T&) {
40 // Assert things at compile time. (|msg| should be a valid identifier name.)
41 // This macro is currently C++-only, but we want to use it in the C core.h.
43 // MOJO_STATIC_ASSERT(sizeof(Foo) == 12, "Foo has invalid size");
44 #if defined(__cplusplus)
45 #define MOJO_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
47 #define MOJO_STATIC_ASSERT(expr, msg)
50 // Like the C++11 |alignof| operator.
51 #if __cplusplus >= 201103L
52 #define MOJO_ALIGNOF(type) alignof(type)
53 #elif defined(__GNUC__)
54 #define MOJO_ALIGNOF(type) __alignof__(type)
55 #elif defined(_MSC_VER)
56 // The use of |sizeof| is to work around a bug in MSVC 2010 (see
57 // http://goo.gl/isH0C; supposedly fixed since then).
58 #define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type))
60 #error "Please define MOJO_ALIGNOF() for your compiler."
63 // Specify the alignment of a |struct|, etc.
65 // struct MOJO_ALIGNAS(8) Foo { ... };
66 // Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a
67 // type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the
68 // non-C++11 MSVS version).
69 #if __cplusplus >= 201103L
70 #define MOJO_ALIGNAS(alignment) alignas(alignment)
71 #elif defined(__GNUC__)
72 #define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment)))
73 #elif defined(_MSC_VER)
74 #define MOJO_ALIGNAS(alignment) __declspec(align(alignment))
76 #error "Please define MOJO_ALIGNAS() for your compiler."
79 #endif // MOJO_PUBLIC_C_SYSTEM_MACROS_H_