1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
25 #ifndef WW_STATICASSERT_HXX
26 #define WW_STATICASSERT_HXX
27 
28 /*
29  Lifted direct from:
30  Modern C++ Design: Generic Programming and Design Patterns Applied
31  Section 2.1
32  by Andrei Alexandrescu
33 */
34 namespace ww
35 {
36     template<bool> class compile_time_check
37     {
38     public:
compile_time_check(...)39         compile_time_check(...) {}
40     };
41 
42     template<> class compile_time_check<false>
43     {
44     };
45 }
46 
47     /*
48     Similiar to assert, StaticAssert is only in operation when NDEBUG is not
49     defined. It will test its first argument at compile time and on failure
50     report the error message of the second argument, which must be a valid c++
51     classname. i.e. no spaces, punctuation or reserved keywords.
52     */
53 #ifndef NDEBUG
54 #   define StaticAssert(test, errormsg)                         \
55     do {                                                        \
56         struct ERROR_##errormsg {};                             \
57         typedef ww::compile_time_check< (test) != 0 > tmplimpl; \
58         tmplimpl aTemp = tmplimpl(ERROR_##errormsg());          \
59         sizeof(aTemp);                                          \
60     } while (0)
61 #else
62 #   define StaticAssert(test, errormsg)                         \
63     do {} while (0)
64 #endif
65 
66 #endif
67 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
68