Go to the documentation of this file.00001 #include "utilmm/configfile/configset.hh"
00002 #include <boost/lexical_cast.hpp>
00003
00004 using namespace std;
00005 using namespace utilmm;
00006
00007 config_set::config_set(config_set* parent_)
00008 : m_parent(parent_) {}
00009
00010 config_set::~config_set()
00011 {
00012 clear();
00013 }
00014
00015 bool config_set::empty() const
00016 { return m_values.empty() && m_children.empty(); }
00017 void config_set::clear()
00018 {
00019 m_values.clear();
00020 while (!m_children.empty())
00021 {
00022 ChildMap::iterator it = m_children.begin();
00023 delete it->second;
00024 m_children.erase(it);
00025 }
00026 }
00027
00028 const config_set* config_set::parent() const { return m_parent; }
00029 config_set* config_set::parent() { return m_parent; }
00030 list<const config_set*> config_set::children(const std::string& name) const
00031 {
00032 typedef list<const config_set *> SetList;
00033 pair<ChildMap::const_iterator, ChildMap::const_iterator>
00034 range = m_children.equal_range(name);
00035
00036 ChildMap::const_iterator
00037 it = range.first,
00038 end = range.second;
00039
00040 SetList ret;
00041 while (it != end)
00042 {
00043 ret.push_back(it -> second);
00044 ++it;
00045 }
00046 return ret;
00047 }
00048 config_set const& config_set::child(std::string const& name) const
00049 {
00050 static config_set empty_set;
00051 subsets every_child = children(name);
00052 if (every_child.empty())
00053 return empty_set;
00054 return *every_child.front();
00055 }
00056
00057 bool config_set::exists(const std::string& name) const
00058 {
00059 return
00060 m_values.find(name) != m_values.end()
00061 || m_children.find(name) != m_children.end();
00062 }
00063
00064 void config_set::insert(std::string const& name, std::string const& value)
00065 { m_values.insert( make_pair(name, value) ); }
00066 void config_set::insert(std::string const& name, std::list<std::string> const& value)
00067 {
00068 list<string>::const_iterator it, end = value.end();
00069 for (it = value.begin(); it != value.end(); ++it)
00070 insert(name, *it);
00071 }
00072 void config_set::insert(std::string const& name, config_set const* child_)
00073 { m_children.insert( make_pair(name, child_) ); }
00074 void config_set::set(std::string const& name, std::string const& value)
00075 {
00076 m_values.erase(name);
00077 insert(name, value);
00078 }
00079 void config_set::set(std::string const& name, std::list<std::string> const& value)
00080 {
00081 m_values.erase(name);
00082 insert(name, value);
00083 }
00084 void config_set::erase(std::string const& name)
00085 { m_values.erase(name); }
00086
00087
00088
00089 template<> bool config_set::convert(const std::string& value)
00090 {
00091 if (value == "true" || value == "1")
00092 return true;
00093 if (value == "false" || value == "0")
00094 return false;
00095
00096 throw boost::bad_lexical_cast();
00097 }
00098
00099 template<>
00100 config_set::stringlist config_set::get(const std::string& name, stringlist const& defval,
00101 boost::enable_if< details::is_list<config_set::stringlist> >::type* dummy) const
00102 {
00103 list<string> values;
00104 for(ValueMap::const_iterator it = m_values.find(name); it != m_values.end() && it->first == name; ++it)
00105 values.push_back(it->second);
00106
00107 if(values.empty())
00108 return defval;
00109
00110 return values;
00111 }
00112