- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / compile_assert.h
1 /*
2  * libjingle
3  * Copyright 2013, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 // COMPILE_ASSERT macro, borrowed from google3/base/macros.h.
29 #ifndef TALK_BASE_COMPILE_ASSERT_H_
30 #define TALK_BASE_COMPILE_ASSERT_H_
31
32 // The COMPILE_ASSERT macro can be used to verify that a compile time
33 // expression is true. For example, you could use it to verify the
34 // size of a static array:
35 //
36 //   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
37 //                  content_type_names_incorrect_size);
38 //
39 // or to make sure a struct is smaller than a certain size:
40 //
41 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
42 //
43 // The second argument to the macro is the name of the variable. If
44 // the expression is false, most compilers will issue a warning/error
45 // containing the name of the variable.
46
47 // TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and
48 // libjingle are merged.
49 #if !defined(COMPILE_ASSERT)
50 template <bool>
51 struct CompileAssert {
52 };
53
54 #define COMPILE_ASSERT(expr, msg) \
55   typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]  // NOLINT
56 #endif  // COMPILE_ASSERT
57
58 // Implementation details of COMPILE_ASSERT:
59 //
60 // - COMPILE_ASSERT works by defining an array type that has -1
61 //   elements (and thus is invalid) when the expression is false.
62 //
63 // - The simpler definition
64 //
65 //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
66 //
67 //   does not work, as gcc supports variable-length arrays whose sizes
68 //   are determined at run-time (this is gcc's extension and not part
69 //   of the C++ standard).  As a result, gcc fails to reject the
70 //   following code with the simple definition:
71 //
72 //     int foo;
73 //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
74 //                               // not a compile-time constant.
75 //
76 // - By using the type CompileAssert<(bool(expr))>, we ensures that
77 //   expr is a compile-time constant.  (Template arguments must be
78 //   determined at compile-time.)
79 //
80 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary
81 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
82 //
83 //     CompileAssert<bool(expr)>
84 //
85 //   instead, these compilers will refuse to compile
86 //
87 //     COMPILE_ASSERT(5 > 0, some_message);
88 //
89 //   (They seem to think the ">" in "5 > 0" marks the end of the
90 //   template argument list.)
91 //
92 // - The array size is (bool(expr) ? 1 : -1), instead of simply
93 //
94 //     ((expr) ? 1 : -1).
95 //
96 //   This is to avoid running into a bug in MS VC 7.1, which
97 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
98
99 #endif  // TALK_BASE_COMPILE_ASSERT_H_