00001 /* 00002 * OptionVars.h 00003 * 00004 * Created on: Apr 21, 2010 00005 * Author: Erik Schuitema 00006 * 00007 * A COptionVar keeps track of whether it has been assigned or not. 00008 * 00009 * Example: 00010 configSection.get("steptime", &mOptionDbl); 00011 logNoticeLn(CLog2("temp"), "After reading steptime, isSet() = " << mOptionDbl.isSet() << ", value = " << mOptionDbl); 00012 mOptionDbl.reset(); 00013 configSection.get("nonexistent", &mOptionDbl); 00014 logNoticeLn(CLog2("temp"), "After reading rubbish, isSet() = " << mOptionDbl.isSet() << ", value = " << mOptionDbl); 00015 00016 Output: 00017 [temp] NTC: After reading steptime, isSet() = 1, value = 0.0133333 00018 [temp] NTC: After reading rubbish, isSet() = 0, value = 0.0133333 00019 * 00020 */ 00021 00022 #ifndef OPTIONVARS_H_ 00023 #define OPTIONVARS_H_ 00024 00025 template<class T> 00026 class COptionVar 00027 { 00028 protected: 00029 bool mSet; 00030 T mValue; 00031 00032 public: 00033 COptionVar() {mValue = 0; mSet=false;} 00034 00035 bool isSet() {return mSet;} 00036 void reset() {mSet=false;} 00037 // The object can be cast to a const reference of its native type T 00038 operator const T&() {return mValue;} 00039 // Assignment of a value of its native type T 00040 COptionVar<T>& operator =(const T& newValue) {mValue = newValue; mSet = true; return *this;} 00041 }; 00042 00043 typedef COptionVar<bool> COptionBool; 00044 typedef COptionVar<int> COptionInt; 00045 typedef COptionVar<double> COptionDouble; 00046 typedef COptionVar<char> COptionChar; 00047 typedef COptionVar<unsigned char> COptionByte; 00048 typedef COptionVar<unsigned short> COptionWord; 00049 00050 #endif /* OPTIONVARS_H_ */