Switch.H 7.22 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 60

class Switch;
class dictionary;

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

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

class Switch
{
public:

73 74 75 76 77
    //- 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
78
    enum switchType : unsigned char
79 80 81 82 83
    {
        FALSE   = 0 /*!< "false" */, TRUE = 1 /*!< "true" */,
        NO      = 2 /*!< "no" */,    YES  = 3 /*!< "yes" */,
        OFF     = 4 /*!< "off" */,   ON   = 5 /*!< "on" */,
        NONE    = 6 /*!< "none" */,
84 85 86
        INVALID = 8 /*!< "invalid" */,
        DEFAULT_OFF = 0x10 /*!< off/false (as default value) */,
        DEFAULT_ON = 0x11  /*!< on/true (as default value) */
87
    };
Henry's avatar
Henry committed
88 89


90
private:
Henry's avatar
Henry committed
91

92
    // Private Data
Henry's avatar
Henry committed
93

94 95
        //- The logic and enumerated text representation stored in a byte
        unsigned char switch_;
Henry's avatar
Henry committed
96

97

98
    // Static Member Functions
Henry's avatar
Henry committed
99

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

103

Henry's avatar
Henry committed
104 105 106 107
public:

    // Constructors

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

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

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

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

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

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

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

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

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        //- 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
176
        //- Construct from Istream
177
        explicit Switch(Istream& is);
Henry's avatar
Henry committed
178

179 180 181

    // Helpers

Henry's avatar
Henry committed
182
        //- Construct from dictionary, supplying default value so that if the
183
        //- value is not found, it is added into the dictionary.
Henry's avatar
Henry committed
184 185
        static Switch lookupOrAddToDict
        (
Mark Olesen's avatar
Mark Olesen committed
186 187
            const word& name,
            dictionary& dict,
Mark Olesen's avatar
Mark Olesen committed
188
            const Switch defaultValue = switchType::FALSE
Henry's avatar
Henry committed
189 190 191 192 193
        );


    // Member Functions

194
        //- True if the Switch has a valid value
195 196
        bool valid() const noexcept;

197 198 199 200 201 202 203 204 205
        //- The underlying enumeration value
        switchType type() const noexcept;

        //- Underlying enumeration is DEFAULT_ON or DEFAULT_OFF
        bool isDefault() const noexcept;

        //- Underlying enumeration is not DEFAULT_ON or DEFAULT_OFF
        bool nonDefault() const noexcept;

206 207
        //- A string representation of the Switch value
        const char* c_str() const noexcept;
Henry's avatar
Henry committed
208

209 210
        //- A string representation of the Switch value
        std::string str() const;
Henry's avatar
Henry committed
211 212

        //- Update the value of the Switch if it is found in the dictionary
Mark Olesen's avatar
Mark Olesen committed
213
        bool readIfPresent(const word& name, const dictionary& dict);
Henry's avatar
Henry committed
214 215 216 217 218


    // Member Operators

        //- Conversion to bool
219
        operator bool() const noexcept
Henry's avatar
Henry committed
220 221 222 223
        {
            return (switch_ & 0x1);
        }

224 225
        //- Assignment from enumerated value
        void operator=(const switchType sw) noexcept
Henry's avatar
Henry committed
226 227 228 229
        {
            switch_ = sw;
        }

230 231
        //- Assignment from bool
        void operator=(const bool b) noexcept
Henry's avatar
Henry committed
232 233 234 235 236
        {
            switch_ = (b ? Switch::TRUE : Switch::FALSE);
        }


237
    // Housekeeping
Henry's avatar
Henry committed
238

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


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

} // End namespace Foam

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

#endif

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