mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
StaticAssert added
- catch people using silly template sizes for FixedList, PackedList
This commit is contained in:
@ -28,6 +28,7 @@ Description
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "uLabel.H"
|
||||
#include "IOstreams.H"
|
||||
#include "PackedBoolList.H"
|
||||
|
||||
@ -38,7 +39,7 @@ using namespace Foam;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Info<< "PackedList max_bits() = " << PackedList<0>::max_bits() << nl;
|
||||
Info<< "PackedList max_bits() = " << PackedList<>::max_bits() << nl;
|
||||
|
||||
Info<< "\ntest allocation with value\n";
|
||||
PackedList<3> list1(5,1);
|
||||
|
||||
@ -43,6 +43,7 @@ SourceFiles
|
||||
#include "uLabel.H"
|
||||
#include "Hash.H"
|
||||
#include "autoPtr.H"
|
||||
#include "StaticAssert.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -70,6 +71,9 @@ template<class T> class SLList;
|
||||
template<class T, unsigned Size>
|
||||
class FixedList
|
||||
{
|
||||
//- Size must be positive (non-zero) and also fit as a signed value
|
||||
StaticAssert(Size && Size <= INT_MAX);
|
||||
|
||||
// Private data
|
||||
|
||||
//- Vector of values of type T of size Size.
|
||||
|
||||
@ -76,9 +76,6 @@ Note
|
||||
SeeAlso
|
||||
Foam::DynamicList
|
||||
|
||||
ToDo
|
||||
Checks for bad template parameters (ie, nBits=0, nBits too large)?
|
||||
|
||||
SourceFiles
|
||||
PackedListI.H
|
||||
PackedList.C
|
||||
@ -89,6 +86,7 @@ SourceFiles
|
||||
#define PackedList_H
|
||||
|
||||
#include "labelList.H"
|
||||
#include "StaticAssert.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -120,6 +118,10 @@ class PackedList
|
||||
typedef unsigned int StorageType;
|
||||
typedef List<StorageType> StorageList;
|
||||
|
||||
//- nBits must be positive (non-zero) and fit within the storage
|
||||
// For simplicity, assume 8-bit bytes
|
||||
StaticAssert(nBits && nBits < (sizeof(StorageType) << 3));
|
||||
|
||||
// Private data
|
||||
|
||||
//- Number of nBits entries
|
||||
|
||||
87
src/OpenFOAM/db/error/StaticAssert.H
Normal file
87
src/OpenFOAM/db/error/StaticAssert.H
Normal file
@ -0,0 +1,87 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation; either version 2 of the License, or (at your
|
||||
option) any later version.
|
||||
|
||||
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OpenFOAM; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Class
|
||||
Foam::StaticAssertFailed
|
||||
|
||||
Description
|
||||
Macros and classes to provide static (compile-time) assertions.
|
||||
|
||||
Ideas from various sources
|
||||
(http://www.ddj.com/cpp/184401547, http://www.boost.org)
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef StaticAssert_H
|
||||
#define StaticAssert_H
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
//- Forward declaration of StaticAssertionFailed.
|
||||
// Leave as an incomplete class so that sizeof(..) fails
|
||||
template<bool Truth> class StaticAssertionFailed;
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class StaticAssertionFailed Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
//- Specialization for successful assertions
|
||||
template<>
|
||||
class StaticAssertionFailed<true>
|
||||
{};
|
||||
|
||||
|
||||
//- Helper class for handling static assertions
|
||||
template<unsigned Test>
|
||||
class StaticAssertionTest {};
|
||||
|
||||
}
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
// internal use:
|
||||
// ~~~~~~~~~~~~~
|
||||
// paste together strings, even if an argument is itself a macro
|
||||
#define StaticAssertMacro(X,Y) StaticAssertMacro1(X,Y)
|
||||
#define StaticAssertMacro1(X,Y) StaticAssertMacro2(X,Y)
|
||||
#define StaticAssertMacro2(X,Y) X##Y
|
||||
|
||||
// external use:
|
||||
// ~~~~~~~~~~~~~
|
||||
/**
|
||||
* @def StaticAssert(Test)
|
||||
* Assert that some test is true at compile-time
|
||||
*/
|
||||
#define StaticAssert(Test) \
|
||||
typedef ::Foam::StaticAssertionTest \
|
||||
< \
|
||||
sizeof( ::Foam::StaticAssertionFailed< ((Test) ? true : false) > ) \
|
||||
> StaticAssertMacro(StaticAssertionTest, __LINE__)
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user