/** * @brief Convert value to desired type. * @param [in, out] value Value to convert. * @param [in] nType Type to convert to. * @return true if conversion succeeded, false otherwise. * @note Currently converts only strings and integers. * @todo Add other conversions. */ bool COption::ConvertType(varprop::VariantValue & value, varprop::VT_TYPE nType) { if (value.GetType() == varprop::VT_STRING) return ConvertString(value, nType); if (value.GetType() == varprop::VT_INT) return ConvertInteger(value, nType); return false; }
/** * @brief Set option value. * * Set new value for option. If automatic conversion is not allowed, * type of value must match to type set when option was initialised. * @param [in] value New value to set. * @param [in] allowConversion Is automatic type conversion allowed? * @sa COption::Init() */ int COption::Set(varprop::VariantValue value, bool allowConversion) { int retVal = OPT_OK; // Check that type matches varprop::VT_TYPE inType = value.GetType(); if (value.GetType() != m_value.GetType()) { if (allowConversion) { if (ConvertType(value, m_value.GetType())) return Set(value); } _RPTF1(_CRT_ERROR, "Wrong type for option: %s", m_strName.c_str()); return OPT_WRONG_TYPE; } switch (inType) { case varprop::VT_NULL: retVal = OPT_UNKNOWN_TYPE; break; case varprop::VT_BOOL: m_value.SetBool(value.GetBool()); break; case varprop::VT_INT: m_value.SetInt(value.GetInt()); break; case varprop::VT_FLOAT: m_value.SetFloat(value.GetFloat()); break; case varprop::VT_STRING: m_value.SetString(value.GetString()); break; case varprop::VT_TIME: m_value.SetTime(value.GetTime()); break; default: retVal = OPT_UNKNOWN_TYPE; } return retVal; }
/** * @brief Set option value. * * Set new value for option. If automatic conversion is not allowed, * type of value must match to type set when option was initialised. * @param [in] value New value to set. * @param [in] allowConversion Is automatic type conversion allowed? * @sa COption::Init() */ int COption::Set(const varprop::VariantValue& value, bool allowConversion) { int retVal = COption::OPT_OK; // Check that type matches varprop::VT_TYPE inType = value.GetType(); if (value.GetType() != m_value.GetType()) { if (allowConversion) { varprop::VariantValue val(value); if (ConvertType(val, m_value.GetType())) return Set(val); } return COption::OPT_WRONG_TYPE; } switch (inType) { case varprop::VT_NULL: retVal = OPT_UNKNOWN_TYPE; break; case varprop::VT_BOOL: m_value.SetBool(value.GetBool()); break; case varprop::VT_INT: m_value.SetInt(value.GetInt()); break; case varprop::VT_FLOAT: m_value.SetFloat(value.GetFloat()); break; case varprop::VT_STRING: m_value.SetString(value.GetString()); break; case varprop::VT_TIME: m_value.SetTime(value.GetTime()); break; default: retVal = OPT_UNKNOWN_TYPE; } return retVal; }
/** * @brief Set name, value and default value for option * @param [in] name Name of option with full path ("Settings/AutomaticRescan") * @param [in] defaultValue Default value for option. This value * is restored for otion when Reset() is run. * @sa COption::Reset() */ int COption::Init(LPCTSTR name, varprop::VariantValue defaultValue) { int retVal = OPT_OK; m_strName = name; if (m_strName.empty()) return OPT_ERR; // Dont' check type here since we are initing it! varprop::VT_TYPE inType = defaultValue.GetType(); switch (inType) { case varprop::VT_NULL: retVal = OPT_UNKNOWN_TYPE; break; case varprop::VT_BOOL: m_value.SetBool(defaultValue.GetBool()); m_valueDef.SetBool(defaultValue.GetBool()); break; case varprop::VT_INT: m_value.SetInt(defaultValue.GetInt()); m_valueDef.SetInt(defaultValue.GetInt()); break; case varprop::VT_FLOAT: m_value.SetFloat(defaultValue.GetFloat()); m_valueDef.SetFloat(defaultValue.GetFloat()); break; case varprop::VT_STRING: m_value.SetString(defaultValue.GetString()); m_valueDef.SetString(defaultValue.GetString()); break; case varprop::VT_TIME: m_value.SetTime(defaultValue.GetTime()); m_valueDef.SetTime(defaultValue.GetTime()); break; default: retVal = OPT_UNKNOWN_TYPE; } return retVal; }
/** * @brief Change default value for option. * * Set new value for option default value. If automatic conversion is not * allowed, type of value must match to type set when option was initialised. * @param [in] defaultValue New default value. * @sa COption::Init() */ int COption::SetDefault(varprop::VariantValue defaultValue) { int retVal = OPT_OK; // Check that type matches varprop::VT_TYPE inType = defaultValue.GetType(); if (inType != m_valueDef.GetType()) { _RPTF1(_CRT_ERROR, "Wrong type for option: %s!", m_strName.c_str()); return OPT_WRONG_TYPE; } switch (inType) { case varprop::VT_NULL: retVal = OPT_UNKNOWN_TYPE; break; case varprop::VT_BOOL: m_valueDef.SetBool(defaultValue.GetBool()); break; case varprop::VT_INT: m_valueDef.SetInt(defaultValue.GetInt()); break; case varprop::VT_FLOAT: m_valueDef.SetFloat(defaultValue.GetFloat()); break; case varprop::VT_STRING: m_valueDef.SetString(defaultValue.GetString()); break; case varprop::VT_TIME: m_valueDef.SetTime(defaultValue.GetTime()); break; default: retVal = OPT_UNKNOWN_TYPE; } return retVal; }
/** * @brief Change default value for option. * * Set new value for option default value. If automatic conversion is not * allowed, type of value must match to type set when option was initialised. * @param [in] defaultValue New default value. * @sa COption::Init() */ int COption::SetDefault(const varprop::VariantValue& defaultValue) { int retVal = COption::OPT_OK; // Check that type matches varprop::VT_TYPE inType = defaultValue.GetType(); if (inType != m_valueDef.GetType()) { return COption::OPT_WRONG_TYPE; } switch (inType) { case varprop::VT_NULL: retVal = OPT_UNKNOWN_TYPE; break; case varprop::VT_BOOL: m_valueDef.SetBool(defaultValue.GetBool()); break; case varprop::VT_INT: m_valueDef.SetInt(defaultValue.GetInt()); break; case varprop::VT_FLOAT: m_valueDef.SetFloat(defaultValue.GetFloat()); break; case varprop::VT_STRING: m_valueDef.SetString(defaultValue.GetString()); break; case varprop::VT_TIME: m_valueDef.SetTime(defaultValue.GetTime()); break; default: retVal = OPT_UNKNOWN_TYPE; } return retVal; }