Switch.H 7.13 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2017-2019 OpenCFD Ltd.
Henry's avatar
Henry committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
-------------------------------------------------------------------------------
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/>.

Class
    Foam::Switch

Description
    A simple wrapper around bool so that it can be read as a word:
    true/false, on/off, yes/no, y/n, t/f, or none.

SourceFiles
    Switch.C

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

#ifndef Switch_H
#define Switch_H

#include "bool.H"
#include "word.H"

42 43 44 45 46 47 48 49 50
// Avoid any pre-processor conflicts with enum names
#undef FALSE
#undef TRUE
#undef NO
#undef YES
#undef OFF
#undef ON
#undef NONE

Henry's avatar
Henry committed
51 52 53 54 55
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

56
// Forward declarations
Henry's avatar
Henry committed
57 58 59
class Switch;
class dictionary;

60 61 62
// IOstream Operators
Istream& operator>>(Istream& is, Switch& sw);
Ostream& operator<<(Ostream& is, const Switch& sw);
Henry's avatar
Henry committed
63 64 65 66 67 68 69 70 71

/*---------------------------------------------------------------------------*\
                           Class Switch Declaration
\*---------------------------------------------------------------------------*/

class Switch
{
public:

72 73 74 75 76
    //- Switch enumerations corresponding to common text representations.
    //  \note the values specified here are critical for its proper behaviour.
    //  The lower bit is tested for the true/false condition.
    //  The values correspond to an index into the predefined output names
    //  for the c_str() method.
Mark Olesen's avatar
Mark Olesen committed
77
    enum switchType : unsigned char
78 79 80 81 82
    {
        FALSE   = 0 /*!< "false" */, TRUE = 1 /*!< "true" */,
        NO      = 2 /*!< "no" */,    YES  = 3 /*!< "yes" */,
        OFF     = 4 /*!< "off" */,   ON   = 5 /*!< "on" */,
        NONE    = 6 /*!< "none" */,
Mark Olesen's avatar
Mark Olesen committed
83
        INVALID = 8 /*!< "invalid" */
84
    };
Henry's avatar
Henry committed
85 86


87
private:
Henry's avatar
Henry committed
88

89
    // Private Data
Henry's avatar
Henry committed
90

91 92
        //- The logic and enumerated text representation stored in a byte
        unsigned char switch_;
Henry's avatar
Henry committed
93

94

95
    // Static Member Functions
Henry's avatar
Henry committed
96

97 98
        //- Return enum value for input string
        static switchType parse(const std::string& str, bool allowBad);
Henry's avatar
Henry committed
99

100

Henry's avatar
Henry committed
101 102 103 104
public:

    // Constructors

105 106
        //- Null constructible as false
        constexpr Switch() noexcept
Henry's avatar
Henry committed
107
        :
108
            switch_(switchType::FALSE)
Henry's avatar
Henry committed
109 110 111
        {}

        //- Construct from enumerated value
112
        constexpr Switch(const switchType sw) noexcept
Henry's avatar
Henry committed
113 114 115 116 117
        :
            switch_(sw)
        {}

        //- Construct from bool
118
        constexpr Switch(const bool b) noexcept
Henry's avatar
Henry committed
119
        :
120
            switch_(b ? switchType::TRUE : switchType::FALSE)
Henry's avatar
Henry committed
121 122 123
        {}

        //- Construct from integer values (treats integer as bool value)
124
        constexpr Switch(const int i) noexcept
Henry's avatar
Henry committed
125
        :
126 127 128 129 130 131
            switch_(i ? switchType::TRUE : switchType::FALSE)
        {}

        //- Construct from string - catches bad input.
        explicit Switch(const std::string& str)
        :
132
            switch_(parse(str, false))
133 134 135 136 137
        {}

        //- Construct from character array - catches bad input.
        explicit Switch(const char* str)
        :
138
            switch_(parse(str, false))
Henry's avatar
Henry committed
139 140
        {}

141
        //- Construct from string.
Henry's avatar
Henry committed
142
        //  Optionally allow bad words, and catch the error elsewhere
Mark Olesen's avatar
Mark Olesen committed
143
        Switch(const std::string& str, bool allowBad)
Henry's avatar
Henry committed
144
        :
145
            switch_(parse(str, allowBad))
Henry's avatar
Henry committed
146 147
        {}

148
        //- Construct from character array.
Henry's avatar
Henry committed
149
        //  Optionally allow bad words, and catch the error elsewhere
Mark Olesen's avatar
Mark Olesen committed
150
        Switch(const char* str, bool allowBad)
Henry's avatar
Henry committed
151
        :
152
            switch_(parse(str, allowBad))
Henry's avatar
Henry committed
153 154
        {}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        //- Construct from dictionary lookup.
        //  FatalError if anything is incorrect.
        Switch
        (
            const word& key,        //!< Lookup key. Uses LITERAL (not REGEX)
            const dictionary& dict  //!< dictionary
        );

        //- Find the key in the dictionary and return the corresponding
        //- switch value, or the default value.
        //  FatalError if anything is incorrect.
        Switch
        (
            const word& key,        //!< Lookup key. Uses LITERAL (not REGEX)
            const dictionary& dict, //!< dictionary
            const Switch defaultValue //!< fallback if not found
        );

Henry's avatar
Henry committed
173
        //- Construct from Istream
174
        explicit Switch(Istream& is);
Henry's avatar
Henry committed
175

176 177 178

    // Helpers

Henry's avatar
Henry committed
179
        //- Construct from dictionary, supplying default value so that if the
180
        //- value is not found, it is added into the dictionary.
Henry's avatar
Henry committed
181 182
        static Switch lookupOrAddToDict
        (
Mark Olesen's avatar
Mark Olesen committed
183 184 185
            const word& name,           //!< Lookup key. Uses REGEX!
            dictionary& dict,           //!< dictionary
            const Switch defaultValue = switchType::FALSE //!< default to add
Henry's avatar
Henry committed
186 187 188 189 190
        );


    // Member Functions

Mark Olesen's avatar
Mark Olesen committed
191
        //- True if the Switch represents a valid enumeration
192 193
        bool valid() const noexcept;

194 195 196
        //- The underlying enumeration value
        switchType type() const noexcept;

197 198
        //- A string representation of the Switch value
        const char* c_str() const noexcept;
Henry's avatar
Henry committed
199

200 201
        //- A string representation of the Switch value
        std::string str() const;
Henry's avatar
Henry committed
202 203

        //- Update the value of the Switch if it is found in the dictionary
Mark Olesen's avatar
Mark Olesen committed
204 205 206 207 208
        bool readIfPresent
        (
            const word& name,           //!< Lookup key. Uses REGEX!
            const dictionary& dict      //!< dictionary
        );
Henry's avatar
Henry committed
209 210 211 212 213


    // Member Operators

        //- Conversion to bool
214
        operator bool() const noexcept
Henry's avatar
Henry committed
215 216 217 218
        {
            return (switch_ & 0x1);
        }

219
        //- Assignment from enumerated value
Mark Olesen's avatar
Mark Olesen committed
220
        Switch& operator=(const switchType sw) noexcept
Henry's avatar
Henry committed
221 222
        {
            switch_ = sw;
Mark Olesen's avatar
Mark Olesen committed
223
            return *this;
Henry's avatar
Henry committed
224 225
        }

226
        //- Assignment from bool
Mark Olesen's avatar
Mark Olesen committed
227
        Switch& operator=(const bool b) noexcept
Henry's avatar
Henry committed
228 229
        {
            switch_ = (b ? Switch::TRUE : Switch::FALSE);
Mark Olesen's avatar
Mark Olesen committed
230
            return *this;
Henry's avatar
Henry committed
231 232 233
        }


234
    // Housekeeping
Henry's avatar
Henry committed
235

236 237
        //- Deprecated(2018-03) text representation of the Switch value
        //  \deprecated(2018-03) - use c_str() method
238
        inline const char* asText() const { return c_str(); };
Henry's avatar
Henry committed
239 240 241 242 243 244 245 246 247 248 249 250
};


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

} // End namespace Foam

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

#endif

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