• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_SYSTEM_MACROS_H_
6 #define MOJO_PUBLIC_SYSTEM_MACROS_H_
7 
8 #include <stddef.h>
9 
10 // Annotate a variable indicating it's okay if it's unused.
11 // Use like:
12 //   int x MOJO_ALLOW_UNUSED = ...;
13 #if defined(__GNUC__)
14 #define MOJO_ALLOW_UNUSED __attribute__((unused))
15 #else
16 #define MOJO_ALLOW_UNUSED
17 #endif
18 
19 // Annotate a function indicating that the caller must examine the return value.
20 // Use like:
21 //   int foo() MOJO_WARN_UNUSED_RESULT;
22 #if defined(__GNUC__)
23 #define MOJO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
24 #else
25 #define MOJO_WARN_UNUSED_RESULT
26 #endif
27 
28 #ifdef __cplusplus
29 
30 // Annotate a virtual method indicating it must be overriding a virtual method
31 // in the parent class. Use like:
32 //   virtual void foo() OVERRIDE;
33 #if defined(_MSC_VER) || defined(__clang__)
34 #define MOJO_OVERRIDE override
35 #else
36 #define MOJO_OVERRIDE
37 #endif
38 
39 // A macro to disallow the copy constructor and operator= functions.
40 // This should be used in the private: declarations for a class.
41 #define MOJO_DISALLOW_COPY_AND_ASSIGN(TypeName) \
42     TypeName(const TypeName&); \
43     void operator=(const TypeName&)
44 
45 // Used to assert things at compile time.
46 namespace mojo { template <bool> struct CompileAssert {}; }
47 #define MOJO_COMPILE_ASSERT(expr, msg) \
48     typedef ::mojo::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
49 
50 // Used to calculate the number of elements in an array.
51 // (See |arraysize()| in Chromium's base/basictypes.h for more details.)
52 namespace mojo {
53 template <typename T, size_t N>
54 char (&ArraySizeHelper(T (&array)[N]))[N];
55 #if !defined(_MSC_VER)
56 template <typename T, size_t N>
57 char (&ArraySizeHelper(const T (&array)[N]))[N];
58 #endif
59 }  // namespace mojo
60 #define MOJO_ARRAYSIZE(array) (sizeof(::mojo::ArraySizeHelper(array)))
61 
62 // Used to make a type move-only in C++03. See Chromium's base/move.h for more
63 // details.
64 #define MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(type, rvalue_type) \
65  private: \
66   struct rvalue_type { \
67     explicit rvalue_type(type* object) : object(object) {} \
68     type* object; \
69   }; \
70   type(type&); \
71   void operator=(type&); \
72  public: \
73   operator rvalue_type() { return rvalue_type(this); } \
74   type Pass() { return type(rvalue_type(this)); } \
75   typedef void MoveOnlyTypeForCPP03; \
76  private:
77 
78 #endif  // __cplusplus
79 
80 #endif  // MOJO_PUBLIC_SYSTEM_MACROS_H_
81