1582fe818e571fa2571267f5e369715188472f352wu@webrtc.org/*
2582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * libjingle
3582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * Copyright 2013, Google Inc.
4582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *
5582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * Redistribution and use in source and binary forms, with or without
6582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * modification, are permitted provided that the following conditions are met:
7582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *
8582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
9582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *     this list of conditions and the following disclaimer.
10582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
11582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *     this list of conditions and the following disclaimer in the documentation
12582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *     and/or other materials provided with the distribution.
13582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *  3. The name of the author may not be used to endorse or promote products
14582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *     derived from this software without specific prior written permission.
15582fe818e571fa2571267f5e369715188472f352wu@webrtc.org *
16582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25582fe818e571fa2571267f5e369715188472f352wu@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26582fe818e571fa2571267f5e369715188472f352wu@webrtc.org */
27582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
28582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// COMPILE_ASSERT macro, borrowed from google3/base/macros.h.
29582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#ifndef TALK_BASE_COMPILE_ASSERT_H_
30582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#define TALK_BASE_COMPILE_ASSERT_H_
31582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
32582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// The COMPILE_ASSERT macro can be used to verify that a compile time
33582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// expression is true. For example, you could use it to verify the
34582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// size of a static array:
35582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
36582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
37582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//                  content_type_names_incorrect_size);
38582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
39582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// or to make sure a struct is smaller than a certain size:
40582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
41582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
42582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
43582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// The second argument to the macro is the name of the variable. If
44582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// the expression is false, most compilers will issue a warning/error
45582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// containing the name of the variable.
46582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
47582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and
48582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// libjingle are merged.
49582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#if !defined(COMPILE_ASSERT)
50582fe818e571fa2571267f5e369715188472f352wu@webrtc.orgtemplate <bool>
51582fe818e571fa2571267f5e369715188472f352wu@webrtc.orgstruct CompileAssert {
52582fe818e571fa2571267f5e369715188472f352wu@webrtc.org};
53582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
54582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#define COMPILE_ASSERT(expr, msg) \
55582fe818e571fa2571267f5e369715188472f352wu@webrtc.org  typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]  // NOLINT
56582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#endif  // COMPILE_ASSERT
57582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
58582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// Implementation details of COMPILE_ASSERT:
59582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
60582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// - COMPILE_ASSERT works by defining an array type that has -1
61582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   elements (and thus is invalid) when the expression is false.
62582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
63582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// - The simpler definition
64582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
65582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
66582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
67582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   does not work, as gcc supports variable-length arrays whose sizes
68582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   are determined at run-time (this is gcc's extension and not part
69582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   of the C++ standard).  As a result, gcc fails to reject the
70582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   following code with the simple definition:
71582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
72582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     int foo;
73582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
74582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//                               // not a compile-time constant.
75582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
76582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// - By using the type CompileAssert<(bool(expr))>, we ensures that
77582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   expr is a compile-time constant.  (Template arguments must be
78582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   determined at compile-time.)
79582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
80582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// - The outer parentheses in CompileAssert<(bool(expr))> are necessary
81582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
82582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
83582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     CompileAssert<bool(expr)>
84582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
85582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   instead, these compilers will refuse to compile
86582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
87582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     COMPILE_ASSERT(5 > 0, some_message);
88582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
89582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   (They seem to think the ">" in "5 > 0" marks the end of the
90582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   template argument list.)
91582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
92582fe818e571fa2571267f5e369715188472f352wu@webrtc.org// - The array size is (bool(expr) ? 1 : -1), instead of simply
93582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
94582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//     ((expr) ? 1 : -1).
95582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//
96582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   This is to avoid running into a bug in MS VC 7.1, which
97582fe818e571fa2571267f5e369715188472f352wu@webrtc.org//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
98582fe818e571fa2571267f5e369715188472f352wu@webrtc.org
99582fe818e571fa2571267f5e369715188472f352wu@webrtc.org#endif  // TALK_BASE_COMPILE_ASSERT_H_
100