Switch.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "Switch.H"
00028 #include "error.H"
00029 #include "dictionary.H"
00030
00031
00032
00033
00034
00035 const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
00036 {
00037 "false", "true",
00038 "off", "on",
00039 "no", "yes",
00040 "n", "y",
00041 "none", "true",
00042 "invalid"
00043 };
00044
00045
00046
00047
00048 Foam::Switch::switchType Foam::Switch::asEnum(const bool b)
00049 {
00050 return b ? Switch::TRUE : Switch::FALSE;
00051 }
00052
00053
00054 Foam::Switch::switchType Foam::Switch::asEnum
00055 (
00056 const std::string& str,
00057 const bool allowInvalid
00058 )
00059 {
00060 for (int sw = 0; sw < Switch::INVALID; sw++)
00061 {
00062 if (str == names[sw])
00063 {
00064
00065 if (sw == Switch::NO_1 || sw == Switch::NONE)
00066 {
00067 return Switch::NO;
00068 }
00069 else if (sw == Switch::YES_1)
00070 {
00071 return Switch::YES;
00072 }
00073 else
00074 {
00075 return switchType(sw);
00076 }
00077 }
00078 }
00079
00080 if (!allowInvalid)
00081 {
00082 FatalErrorIn("Switch::asEnum(const std::string&)")
00083 << "unknown switch word " << str << nl
00084 << abort(FatalError);
00085 }
00086
00087 return INVALID;
00088 }
00089
00090
00091 bool Foam::Switch::asBool(const switchType sw)
00092 {
00093
00094 return (sw & 0x1);
00095 }
00096
00097
00098 bool Foam::Switch::asBool
00099 (
00100 const std::string& str,
00101 const bool allowInvalid
00102 )
00103 {
00104
00105 switchType sw = asEnum(str, true);
00106
00107 if (sw == Switch::INVALID)
00108 {
00109 if (!allowInvalid)
00110 {
00111 FatalErrorIn("Switch::asBool(const std::string&)")
00112 << "unknown switch word " << str << nl
00113 << abort(FatalError);
00114 }
00115
00116 return false;
00117 }
00118
00119
00120 return (sw & 0x1);
00121 }
00122
00123
00124 const char* Foam::Switch::asText(const bool b)
00125 {
00126 return b ? names[Switch::TRUE] : names[Switch::FALSE];
00127 }
00128
00129
00130 const char* Foam::Switch::asText(const switchType sw)
00131 {
00132 return names[sw];
00133 }
00134
00135
00136 Foam::Switch Foam::Switch::lookupOrAddToDict
00137 (
00138 const word& name,
00139 dictionary& dict,
00140 const Switch& defaultValue
00141 )
00142 {
00143 return dict.lookupOrAddDefault<Switch>(name, defaultValue);
00144 }
00145
00146
00147
00148
00149 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
00150 {
00151 return dict.readIfPresent<Switch>(name, *this);
00152 }
00153
00154
00155
Copyright © 2000-2009 OpenCFD Ltd