FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

NamedEnum.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "NamedEnum.H"
00027 #include <OpenFOAM/stringList.H>
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 template<class Enum, int nEnum>
00032 Foam::NamedEnum<Enum, nEnum>::NamedEnum()
00033 :
00034     HashTable<int>(2*nEnum)
00035 {
00036     for (int i=0; i<nEnum; i++)
00037     {
00038         if (!names[i] || names[i][0] == '\0')
00039         {
00040             stringList goodNames(i);
00041 
00042             for (int j = 0; j < i; j++)
00043             {
00044                 goodNames[j] = names[j];
00045             }
00046 
00047             FatalErrorIn("NamedEnum<Enum, nEnum>::NamedEnum()")
00048                 << "Illegal enumeration name at position " << i << endl
00049                 << "after entries " << goodNames << ".\n"
00050                 << "Possibly your NamedEnum<Enum, nEnum>::names array"
00051                 << " is not of size " << nEnum << endl
00052                 << abort(FatalError);
00053         }
00054         insert(names[i], i);
00055     }
00056 }
00057 
00058 
00059 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00060 
00061 template<class Enum, int nEnum>
00062 Enum Foam::NamedEnum<Enum, nEnum>::read(Istream& is) const
00063 {
00064     word name(is);
00065 
00066     HashTable<int>::const_iterator iter = find(name);
00067 
00068     if (iter == HashTable<int>::end())
00069     {
00070         FatalIOErrorIn
00071         (
00072             "NamedEnum<Enum, nEnum>::read(Istream&) const", is
00073         )   << name << " is not in enumeration: "
00074             << toc() << exit(FatalIOError);
00075     }
00076 
00077     return Enum(iter());
00078 }
00079 
00080 
00081 template<class Enum, int nEnum>
00082 void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
00083 {
00084     os  << operator[](e);
00085 }
00086 
00087 
00088 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines