| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef IFOPTIONS_H |
| 21 |
#define IFOPTIONS_H |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <string> |
| 28 |
#include <map> |
| 29 |
#include <iosfwd> |
| 30 |
|
| 31 |
#include "fpp_assert.h" |
| 32 |
|
| 33 |
class Configuration; |
| 34 |
|
| 35 |
|
| 36 |
class ifOption |
| 37 |
{ |
| 38 |
public: |
| 39 |
|
| 40 |
enum ioType { iotBool, iotInt, iotText }; |
| 41 |
|
| 42 |
protected: |
| 43 |
|
| 44 |
std::string optionName; |
| 45 |
|
| 46 |
std::string optionDescription; |
| 47 |
|
| 48 |
std::string defaultValue; |
| 49 |
|
| 50 |
std::string tValue; |
| 51 |
|
| 52 |
ioType type; |
| 53 |
|
| 54 |
int iValue = 0; |
| 55 |
|
| 56 |
bool bValue = 0; |
| 57 |
|
| 58 |
public: |
| 59 |
|
| 60 |
ifOption ( const std::string& name, const std::string& desc, ioType t, const std::string& defVal ); |
| 61 |
|
| 62 |
ifOption ( void ) = delete; |
| 63 |
|
| 64 |
ifOption ( const ifOption& ) = delete; |
| 65 |
|
| 66 |
ifOption& operator = ( const ifOption& ) = delete; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
bool setValue ( bool b ) { bValue = b; return (type != iotBool); } |
| 72 |
|
| 73 |
bool setValue ( int i ) { iValue = i; return (type != iotInt); } |
| 74 |
|
| 75 |
bool setValue ( const std::string& t ) { tValue = t; return (type != iotText); } |
| 76 |
|
| 77 |
bool setAValue ( const std::string& s ); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
bool getBool ( void ) const { fpp_assert ( type == iotBool ); return bValue; } |
| 82 |
|
| 83 |
int getInt ( void ) const { fpp_assert ( type == iotInt ); return iValue; } |
| 84 |
|
| 85 |
const std::string& getText ( void ) const { fpp_assert ( type == iotText ); return tValue; } |
| 86 |
|
| 87 |
|
| 88 |
void printConfString ( std::ostream& o ) const; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
inline ifOption :: ifOption ( const std::string& name, const std::string& desc, ioType t, const std::string& defVal ) |
| 94 |
: optionName(name) |
| 95 |
, optionDescription(desc) |
| 96 |
, defaultValue(defVal) |
| 97 |
, type(t) |
| 98 |
{ |
| 99 |
setAValue (defVal); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
class ifOptionSet |
| 104 |
{ |
| 105 |
protected: |
| 106 |
|
| 107 |
typedef std::map<std::string,ifOption*> OptionSet; |
| 108 |
|
| 109 |
protected: |
| 110 |
|
| 111 |
OptionSet Base; |
| 112 |
|
| 113 |
protected: |
| 114 |
|
| 115 |
const ifOption* locateOption ( const std::string& name ) const |
| 116 |
{ |
| 117 |
auto p = Base.find(name); |
| 118 |
return p == Base.end() ? nullptr : p->second; |
| 119 |
} |
| 120 |
|
| 121 |
public: |
| 122 |
|
| 123 |
ifOptionSet() = default; |
| 124 |
|
| 125 |
~ifOptionSet() |
| 126 |
{ |
| 127 |
for ( auto& p: Base ) |
| 128 |
delete p.second; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
bool RegisterOption ( |
| 133 |
const std::string& name, |
| 134 |
const std::string& desc, |
| 135 |
ifOption::ioType t, |
| 136 |
const std::string& defVal ) |
| 137 |
{ |
| 138 |
if ( locateOption (name) != nullptr ) |
| 139 |
return true; |
| 140 |
Base[name] = new ifOption ( name, desc, t, defVal ); |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
bool initByConfigure ( Configuration& conf, const std::string& Section ); |
| 145 |
|
| 146 |
bool setOption( const std::string& optionName, const std::string& optionValue) |
| 147 |
{ |
| 148 |
auto p = Base.find(optionName); |
| 149 |
if ( p == Base.end() ) |
| 150 |
return true; |
| 151 |
if ( p->second->setAValue(optionValue)) |
| 152 |
return true; |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
bool getBool ( const std::string& optionName ) const |
| 160 |
{ |
| 161 |
const ifOption* p = locateOption ( optionName ); |
| 162 |
fpp_assert ( p != nullptr ); |
| 163 |
return p->getBool (); |
| 164 |
} |
| 165 |
|
| 166 |
int getInt ( const std::string& optionName ) const |
| 167 |
{ |
| 168 |
const ifOption* p = locateOption ( optionName ); |
| 169 |
fpp_assert ( p != nullptr ); |
| 170 |
return p->getInt (); |
| 171 |
} |
| 172 |
|
| 173 |
const std::string& getText ( const std::string& optionName ) const |
| 174 |
{ |
| 175 |
const ifOption* p = locateOption ( optionName ); |
| 176 |
fpp_assert ( p != nullptr ); |
| 177 |
return p->getText (); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void printConfig ( std::ostream& o ) const; |
| 182 |
}; |
| 183 |
|
| 184 |
#endif |