NamedEnum: Check range of names array index to avoid warning from gcc

This commit is contained in:
Henry Weller
2018-05-11 14:41:22 +01:00
parent 6db30e661c
commit 379c86c85f
2 changed files with 24 additions and 7 deletions

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -120,4 +120,25 @@ Foam::wordList Foam::NamedEnum<Enum, nEnum>::words()
}
template<class Enum, unsigned int nEnum>
const char* Foam::NamedEnum<Enum, nEnum>::operator[](const Enum e) const
{
unsigned int ue = unsigned(e);
if (ue < nEnum)
{
return names[ue];
}
else
{
FatalErrorInFunction
<< "names array index " << ue << " out of range 0-"
<< nEnum - 1
<< exit(FatalError);
return names[0];
}
}
// ************************************************************************* //

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -38,7 +38,6 @@ SourceFiles
#include "HashTable.H"
#include "stringList.H"
#include "wordList.H"
#include <type_traits>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -108,10 +107,7 @@ public:
}
//- Return the name of the given enumeration element
const char* operator[](const Enum e) const
{
return names[unsigned(e)];
}
const char* operator[](const Enum e) const;
};