Switch.H 6.16 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-2018 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 84 85
    {
        FALSE   = 0 /*!< "false" */, TRUE = 1 /*!< "true" */,
        NO      = 2 /*!< "no" */,    YES  = 3 /*!< "yes" */,
        OFF     = 4 /*!< "off" */,   ON   = 5 /*!< "on" */,
        NONE    = 6 /*!< "none" */,
        INVALID = 8 /*!< "invalid" */
    };
Henry's avatar
Henry committed
86 87


88
private:
Henry's avatar
Henry committed
89

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

92 93
        //- The logic and enumerated text representation stored in a byte
        unsigned char switch_;
Henry's avatar
Henry committed
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
143
        Switch(const std::string& str, const 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
150
        Switch(const char* str, const bool allowBad)
Henry's avatar
Henry committed
151
        :
152
            switch_(parse(str, allowBad))
Henry's avatar
Henry committed
153 154 155
        {}

        //- Construct from Istream
156
        explicit Switch(Istream& is);
Henry's avatar
Henry committed
157

158 159 160

    // Helpers

Henry's avatar
Henry committed
161 162 163 164
        //- Construct from dictionary, supplying default value so that if the
        //  value is not found, it is added into the dictionary.
        static Switch lookupOrAddToDict
        (
Mark Olesen's avatar
Mark Olesen committed
165 166
            const word& name,
            dictionary& dict,
Mark Olesen's avatar
Mark Olesen committed
167
            const Switch defaultValue = switchType::FALSE
Henry's avatar
Henry committed
168 169 170 171 172 173
        );


    // Member Functions

        //- Return true if the Switch has a valid value
174 175 176 177
        bool valid() const noexcept;

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

179 180
        //- A string representation of the Switch value
        std::string str() const;
Henry's avatar
Henry committed
181 182

        //- Update the value of the Switch if it is found in the dictionary
Mark Olesen's avatar
Mark Olesen committed
183
        bool readIfPresent(const word& name, const dictionary& dict);
Henry's avatar
Henry committed
184 185 186 187 188


    // Member Operators

        //- Conversion to bool
189
        operator bool() const noexcept
Henry's avatar
Henry committed
190 191 192 193
        {
            return (switch_ & 0x1);
        }

194 195
        //- Assignment from enumerated value
        void operator=(const switchType sw) noexcept
Henry's avatar
Henry committed
196 197 198 199
        {
            switch_ = sw;
        }

200 201
        //- Assignment from bool
        void operator=(const bool b) noexcept
Henry's avatar
Henry committed
202 203 204 205 206
        {
            switch_ = (b ? Switch::TRUE : Switch::FALSE);
        }


207
    // Housekeeping
Henry's avatar
Henry committed
208

209 210
        //- Deprecated(2018-03) text representation of the Switch value
        //  \deprecated(2018-03) - use c_str() method
211
        inline const char* asText() const { return c_str(); };
Henry's avatar
Henry committed
212 213 214 215 216 217 218 219 220 221 222 223
};


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

} // End namespace Foam

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

#endif

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