ENH: add strings() and words() method to NamedEnum

- for situations where a list of strings/words is more useful than a
  raw array of C-strings
This commit is contained in:
Mark Olesen
2010-11-17 09:12:11 +01:00
parent 7cb9c39b3e
commit 857ee7ffe9
2 changed files with 46 additions and 1 deletions

View File

@ -24,7 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "NamedEnum.H"
#include "stringList.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -85,4 +84,42 @@ void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
}
template<class Enum, int nEnum>
Foam::stringList Foam::NamedEnum<Enum, nEnum>::strings()
{
stringList lst(nEnum);
label nElem = 0;
for (int enumI = 0; enumI < nEnum; ++enumI)
{
if (names[enumI] && names[enumI][0])
{
lst[nElem++] = names[enumI];
}
}
lst.setSize(nElem);
return lst;
}
template<class Enum, int nEnum>
Foam::wordList Foam::NamedEnum<Enum, nEnum>::words()
{
wordList lst(nEnum);
label nElem = 0;
for (int enumI = 0; enumI < nEnum; ++enumI)
{
if (names[enumI] && names[enumI][0])
{
lst[nElem++] = names[enumI];
}
}
lst.setSize(nElem);
return lst;
}
// ************************************************************************* //

View File

@ -37,6 +37,8 @@ SourceFiles
#include "HashTable.H"
#include "StaticAssert.H"
#include "stringList.H"
#include "wordList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -87,6 +89,12 @@ public:
//- Write the name representation of the enumeration to an Ostream
void write(const Enum e, Ostream&) const;
//- The set of names as a list of strings
static stringList strings();
//- The set of names as a list of words
static wordList words();
// Member Operators