Skip to content
Snippets Groups Projects
Enum.C 5.17 KiB
Newer Older
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2017-2019 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 3 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, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "Enum.H"
#include "dictionary.H"

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<class EnumType>
Foam::Enum<EnumType>::Enum
(
Mark OLESEN's avatar
Mark OLESEN committed
    std::initializer_list<std::pair<EnumType, const char*>> list
Mark OLESEN's avatar
Mark OLESEN committed
    keys_(list.size()),
    vals_(list.size())
Mark OLESEN's avatar
Mark OLESEN committed
    label i = 0;
    for (const auto& pair : list)
Mark OLESEN's avatar
Mark OLESEN committed
        keys_[i] = pair.second;
        vals_[i] = int(pair.first);
Mark OLESEN's avatar
Mark OLESEN committed
        ++i;
Mark OLESEN's avatar
Mark OLESEN committed
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class EnumType>
Mark OLESEN's avatar
Mark OLESEN committed
Foam::List<Foam::word> Foam::Enum<EnumType>::sortedToc() const
Mark OLESEN's avatar
Mark OLESEN committed
    List<word> list(keys_);
Mark OLESEN's avatar
Mark OLESEN committed
    Foam::sort(list);
Mark OLESEN's avatar
Mark OLESEN committed
    return list;
Mark OLESEN's avatar
Mark OLESEN committed
EnumType Foam::Enum<EnumType>::get(const word& enumName) const
Mark OLESEN's avatar
Mark OLESEN committed
    const label idx = find(enumName);
Mark OLESEN's avatar
Mark OLESEN committed
    if (idx < 0)
    {
        FatalErrorInFunction
            << enumName << " is not in enumeration: " << *this << nl
            << exit(FatalError);
    }

    return EnumType(vals_[idx]);
template<class EnumType>
EnumType Foam::Enum<EnumType>::get
(
    const word& enumName,
) const
{
    const label idx = find(enumName);

    if (idx < 0)
    {
    }

    return EnumType(vals_[idx]);
}


template<class EnumType>
EnumType Foam::Enum<EnumType>::read(Istream& is) const
{
    const word enumName(is);
Mark OLESEN's avatar
Mark OLESEN committed

    const label idx = find(enumName);

    if (idx < 0)
    {
        FatalIOErrorInFunction(is)
Mark OLESEN's avatar
Mark OLESEN committed
            << enumName << " is not in enumeration: " << *this << nl
Mark OLESEN's avatar
Mark OLESEN committed
    return EnumType(vals_[idx]);
(
    const word& key,
    const dictionary& dict
) const
{
Mark OLESEN's avatar
Mark OLESEN committed
    const word enumName(dict.get<word>(key, keyType::LITERAL));

    const label idx = find(enumName);

    if (idx < 0)
    {
        FatalIOErrorInFunction(dict)
Mark OLESEN's avatar
Mark OLESEN committed
            << enumName << " is not in enumeration: " << *this << nl
Mark OLESEN's avatar
Mark OLESEN committed
    return EnumType(vals_[idx]);
template<class EnumType>
EnumType Foam::Enum<EnumType>::getOrDefault
(
    const word& key,
    const dictionary& dict,
Mark OLESEN's avatar
Mark OLESEN committed
    const bool failsafe
Mark OLESEN's avatar
Mark OLESEN committed
    const entry* eptr = dict.findEntry(key, keyType::LITERAL);

    if (eptr)
Mark OLESEN's avatar
Mark OLESEN committed
        const word enumName(eptr->get<word>());

        const label idx = find(enumName);

        if (idx >= 0)
        {
            return EnumType(vals_[idx]);
        }

        // Found the entry, but failed the name lookup

        if (failsafe)
        {
            IOWarningInFunction(dict)
                << enumName << " is not in enumeration: " << *this << nl
                << "using failsafe " << get(deflt)
                << " (value " << int(deflt) << ')' << endl;
Mark OLESEN's avatar
Mark OLESEN committed
        }
        else
        {
            FatalIOErrorInFunction(dict)
                << enumName << " is not in enumeration: " << *this << nl
                << exit(FatalIOError);
        }
}


template<class EnumType>
Mark OLESEN's avatar
Mark OLESEN committed
bool Foam::Enum<EnumType>::readEntry
(
    const word& key,
    const dictionary& dict,
Mark OLESEN's avatar
Mark OLESEN committed
    EnumType& val,
    const bool mandatory
Mark OLESEN's avatar
Mark OLESEN committed
    const entry* eptr = dict.findEntry(key, keyType::LITERAL);

    if (eptr)
Mark OLESEN's avatar
Mark OLESEN committed
        const word enumName(eptr->get<word>());

        const label idx = find(enumName);
        if (idx >= 0)
Mark OLESEN's avatar
Mark OLESEN committed
            val = EnumType(vals_[idx]);
            return true;
        FatalIOErrorInFunction(dict)
            << enumName << " is not in enumeration: " << *this << nl
            << exit(FatalIOError);
Mark OLESEN's avatar
Mark OLESEN committed
    }
    else if (mandatory)
    {
        FatalIOErrorInFunction(dict)
            << "'" << key << "' not found in dictionary " << dict.name() << nl
            << exit(FatalIOError);
Mark OLESEN's avatar
Mark OLESEN committed
    return false;
Mark OLESEN's avatar
Mark OLESEN committed
bool Foam::Enum<EnumType>::readIfPresent
Mark OLESEN's avatar
Mark OLESEN committed
    const word& key,
    const dictionary& dict,
    EnumType& val
) const
Mark OLESEN's avatar
Mark OLESEN committed
    // Reading is non-mandatory
    return readEntry(key, dict, val, false);
}


// ************************************************************************* //