StaticAssert.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::StaticAssertionFailed
26 
27 Description
28  Macros and classes to provide static (compile-time) assertions.
29 
30  Ideas from various sources
31  (http://www.ddj.com/cpp/184401547, http://www.boost.org)
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef StaticAssert_H
36 #define StaticAssert_H
37 
38 namespace Foam
39 {
40 
41 //- Forward declaration of StaticAssertionFailed.
42 // Leave as an incomplete class so that sizeof(..) fails
43 template<bool Truth> class StaticAssertionFailed;
44 
45 /*---------------------------------------------------------------------------*\
46  Class StaticAssertionFailed Declaration
47 \*---------------------------------------------------------------------------*/
48 
49 //- Specialization for successful assertions
50 template<>
51 class StaticAssertionFailed<true>
52 {};
53 
54 
55 //- Helper class for handling static assertions
56 template<unsigned Test>
57 class StaticAssertionTest {};
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 } // End namespace Foam
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 // Internal use:
66 // ~~~~~~~~~~~~~
67 
68 // Paste together strings, even if an argument is itself a macro
69 #define StaticAssertMacro(X,Y) StaticAssertMacro1(X,Y)
70 #define StaticAssertMacro1(X,Y) StaticAssertMacro2(X,Y)
71 #define StaticAssertMacro2(X,Y) X##Y
72 
73 // External use:
74 // ~~~~~~~~~~~~~
75 
76 //- Assert that some test is true at compile-time
77 #define StaticAssert(Test) \
78  typedef ::Foam::StaticAssertionTest \
79  < \
80  sizeof( ::Foam::StaticAssertionFailed< ((Test) ? true : false) > ) \
81  > StaticAssertMacro(StaticAssertionTest, __LINE__)
82 
83 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
84 
85 #endif
86 
87 // ************************************************************************* //
Foam::StaticAssertionTest
Helper class for handling static assertions.
Definition: StaticAssert.H:56
Foam::StaticAssertionFailed
Forward declaration of StaticAssertionFailed.
Definition: StaticAssert.H:42
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30