Skip to content
Snippets Groups Projects
Switch.H 6.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henry's avatar
    Henry committed
    /*---------------------------------------------------------------------------*\
      =========                 |
      \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
       \\    /   O peration     |
    
        \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
    
         \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
    
    Henry's avatar
    Henry committed
    -------------------------------------------------------------------------------
    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"
    
    
    // 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
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    namespace Foam
    {
    
    
    // Forward declarations
    
    Henry's avatar
    Henry committed
    
    class Switch;
    class dictionary;
    
    
    // IOstream Operators
    Istream& operator>>(Istream& is, Switch& sw);
    Ostream& operator<<(Ostream& is, const Switch& sw);
    
    Henry's avatar
    Henry committed
    
    /*---------------------------------------------------------------------------*\
                               Class Switch Declaration
    \*---------------------------------------------------------------------------*/
    
    class Switch
    {
    public:
    
    
        //- 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
        enum switchType : unsigned char
    
        {
            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
    
    
    Henry's avatar
    Henry committed
    
    
            //- The logic and enumerated text representation stored in a byte
            unsigned char switch_;
    
    Henry's avatar
    Henry committed
    
    
        // Static Member Functions
    
    Henry's avatar
    Henry committed
    
    
            //- Return enum value for input string
            static switchType parse(const std::string& str, bool allowBad);
    
    Henry's avatar
    Henry committed
    
    
    Henry's avatar
    Henry committed
    public:
    
        // Constructors
    
    
            //- Null constructible as false
            constexpr Switch() noexcept
    
    Henry's avatar
    Henry committed
            :
    
    Henry's avatar
    Henry committed
            {}
    
            //- Construct from enumerated value
    
            constexpr Switch(const switchType sw) noexcept
    
    Henry's avatar
    Henry committed
            :
                switch_(sw)
            {}
    
            //- Construct from bool
    
            constexpr Switch(const bool b) noexcept
    
    Henry's avatar
    Henry committed
            :
    
                switch_(b ? switchType::TRUE : switchType::FALSE)
    
    Henry's avatar
    Henry committed
            {}
    
            //- Construct from integer values (treats integer as bool value)
    
            constexpr Switch(const int i) noexcept
    
    Henry's avatar
    Henry committed
            :
    
                switch_(i ? switchType::TRUE : switchType::FALSE)
            {}
    
            //- Construct from string - catches bad input.
            explicit Switch(const std::string& str)
            :
    
                switch_(parse(str, false))
    
            {}
    
            //- Construct from character array - catches bad input.
            explicit Switch(const char* str)
            :
    
                switch_(parse(str, false))
    
    Henry's avatar
    Henry committed
            //  Optionally allow bad words, and catch the error elsewhere
    
            Switch(const std::string& str, const bool allowBad)
    
    Henry's avatar
    Henry committed
            :
    
                switch_(parse(str, allowBad))
    
            //- Construct from character array.
    
    Henry's avatar
    Henry committed
            //  Optionally allow bad words, and catch the error elsewhere
    
            Switch(const char* str, const bool allowBad)
    
    Henry's avatar
    Henry committed
            :
    
                switch_(parse(str, allowBad))
    
    Henry's avatar
    Henry committed
            {}
    
            //- Construct from Istream
    
            explicit Switch(Istream& is);
    
    Henry's avatar
    Henry committed
            //- Construct from dictionary, supplying default value so that if the
            //  value is not found, it is added into the dictionary.
            static Switch lookupOrAddToDict
            (
    
                const word& name,
                dictionary& dict,
    
    Mark OLESEN's avatar
    Mark OLESEN committed
                const Switch defaultValue = switchType::FALSE
    
    Henry's avatar
    Henry committed
            );
    
    
        // Member Functions
    
            //- Return true if the Switch has a valid value
    
            bool valid() const noexcept;
    
            //- A string representation of the Switch value
            const char* c_str() const noexcept;
    
            //- A string representation of the Switch value
            std::string str() const;
    
    Henry's avatar
    Henry committed
    
            //- Update the value of the Switch if it is found in the dictionary
    
            bool readIfPresent(const word& name, const dictionary& dict);
    
    Henry's avatar
    Henry committed
    
    
        // Member Operators
    
            //- Conversion to bool
    
            operator bool() const noexcept
    
    Henry's avatar
    Henry committed
            {
                return (switch_ & 0x1);
            }
    
    
            //- Assignment from enumerated value
            void operator=(const switchType sw) noexcept
    
    Henry's avatar
    Henry committed
            {
                switch_ = sw;
            }
    
    
            //- Assignment from bool
            void operator=(const bool b) noexcept
    
    Henry's avatar
    Henry committed
            {
                switch_ = (b ? Switch::TRUE : Switch::FALSE);
            }
    
    
    
            //- A text representation of the Switch value
            //  \deprecated - use c_str() instead (MAR-2018)
            inline const char* asText() const { return c_str(); };
    
    Henry's avatar
    Henry committed
    };
    
    
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    } // End namespace Foam
    
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    #endif
    
    // ************************************************************************* //