Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00022
00023 #ifndef ICL_CORE_CONFIG_CONFIG_VALUE_H_INCLUDED
00024 #define ICL_CORE_CONFIG_CONFIG_VALUE_H_INCLUDED
00025
00026 #include <icl_core/BaseTypes.h>
00027 #include <icl_core/TemplateHelper.h>
00028
00029 #include "icl_core_config/ConfigHelper.h"
00030 #include "icl_core_config/ConfigManager.h"
00031 #include "icl_core_config/ConfigValueIface.h"
00032 #include "icl_core_config/Util.h"
00033
00034 #ifdef _IC_BUILDER_OPENSPLICEDDS_
00035 # include "dds_dcps.h"
00036 # include "mapping/String.h"
00037 #endif
00038
00039 # define CONFIG_VALUE(key, value) \
00040 (new icl_core::config::ConfigValue<ICL_CORE_CONFIG_TYPEOF(value)>(key, value))
00041
00042
00043 namespace icl_core {
00044 namespace config {
00045
00049 template <typename T>
00050 class ConfigValue : public impl::ConfigValueIface
00051 {
00052 public:
00056 ConfigValue(const icl_core::String& key,
00057 typename icl_core::ConvertToRef<T>::ToRef value)
00058 : m_key(key),
00059 m_value(value)
00060 { }
00061
00064 virtual ~ConfigValue() { }
00065
00068 virtual bool get(std::string const & prefix, icl_core::logging::LogStream& log_stream) const
00069 {
00070 if (ConfigManager::instance().get(prefix + m_key, m_str_value))
00071 {
00072 try
00073 {
00074 m_value = impl::hexical_cast<T>(m_str_value);
00075 return true;
00076 }
00077 catch (...)
00078 {
00079 return false;
00080 }
00081 }
00082 else
00083 {
00084 return false;
00085 }
00086 }
00087
00090 virtual icl_core::String key() const
00091 {
00092 return m_key;
00093 }
00094
00097 virtual icl_core::String stringValue() const
00098 {
00099 return m_str_value;
00100 }
00101
00102 protected:
00103 icl_core::String m_key;
00104 mutable icl_core::String m_str_value;
00105 typename icl_core::ConvertToRef<T>::ToRef m_value;
00106 };
00107
00108 template<>
00109 inline
00110 bool ConfigValue<bool>::get(std::string const & prefix, icl_core::logging::LogStream& log_stream) const
00111 {
00112 bool result = false;
00113 if (ConfigManager::instance().get(prefix + m_key, m_str_value))
00114 {
00115 try
00116 {
00117 m_value = impl::strict_bool_cast(m_str_value);
00118 result = true;
00119 }
00120 catch (...)
00121 {
00122 result = false;
00123 }
00124 }
00125 else
00126 {
00127 result = false;
00128 }
00129 return result;
00130 }
00131
00132 #ifdef _IC_BUILDER_OPENSPLICEDDS_
00133 template<>
00134 inline
00135 bool ConfigValue<DDS::String>::get(std::string const & prefix, icl_core::logging::LogStream& log_stream) const
00136 {
00137 bool result = false;
00138 if (ConfigManager::instance().get(prefix + m_key, m_str_value))
00139 {
00140 m_value = DDS::string_dup(m_str_value.c_str());
00141 result = true;
00142 }
00143 else
00144 {
00145 result = false;
00146 }
00147 return result;
00148 }
00149 template<>
00150 inline
00151 bool ConfigValue<DDS::String_mgr>::get(std::string const & prefix,
00152 icl_core::logging::LogStream& log_stream) const
00153 {
00154 bool result = false;
00155 if (ConfigManager::instance().get(prefix + m_key, m_str_value))
00156 {
00157 m_value = DDS::string_dup(m_str_value.c_str());
00158 result = true;
00159 }
00160 else
00161 {
00162 result = false;
00163 }
00164 return result;
00165 }
00166 #endif
00167
00168 }}
00169
00170 #endif