ConfigNode API: added writeProperties() and clear()
writeProperties() complements readProperties(). It is identical to calling setProperty() for each individual entry, which is also the default implementation. Some ConfigNode implementations can implement this more efficiently. clear() goes beyond repeated removeProperty() calls because it can also wipe out everything else which might have ended up in the node (for example, comments in the .ini format).
This commit is contained in:
parent
589bd162a4
commit
5c3082debf
|
@ -28,6 +28,7 @@ using namespace std;
|
|||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include <syncevo/declarations.h>
|
||||
SE_BEGIN_CXX
|
||||
|
@ -147,6 +148,8 @@ class ConfigNode {
|
|||
}
|
||||
|
||||
|
||||
typedef map<string, string> PropsType;
|
||||
|
||||
/**
|
||||
* Extract all list of all currently defined properties
|
||||
* and their values. Does not include values which were
|
||||
|
@ -156,8 +159,18 @@ class ConfigNode {
|
|||
* @retval props to be filled with key/value pairs; guaranteed
|
||||
* to be empty before the call
|
||||
*/
|
||||
virtual void readProperties(map<string, string> &props) const = 0;
|
||||
typedef map<string, string> PropsType;
|
||||
virtual void readProperties(PropsType &props) const = 0;
|
||||
|
||||
/**
|
||||
* Add the given properties. To replace the content of the
|
||||
* node, call clear() first.
|
||||
*/
|
||||
virtual void writeProperties(const PropsType &props)
|
||||
{
|
||||
BOOST_FOREACH(const PropsType::value_type &entry, props) {
|
||||
setProperty(entry.first, entry.second);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a certain property.
|
||||
|
@ -166,6 +179,11 @@ class ConfigNode {
|
|||
*/
|
||||
virtual void removeProperty(const string &property) = 0;
|
||||
|
||||
/**
|
||||
* Remove all properties.
|
||||
*/
|
||||
virtual void clear() = 0;
|
||||
|
||||
/**
|
||||
* Node exists in backend storage.
|
||||
*/
|
||||
|
|
|
@ -323,6 +323,12 @@ void FileConfigNode::setProperty(const string &property,
|
|||
m_modified = true;
|
||||
}
|
||||
|
||||
void FileConfigNode::clear()
|
||||
{
|
||||
m_lines.clear();
|
||||
m_modified = true;
|
||||
}
|
||||
|
||||
HashFileConfigNode::HashFileConfigNode(const string &path, const string &fileName, bool readonly) :
|
||||
FileBaseConfigNode(path,fileName,readonly)
|
||||
{
|
||||
|
@ -372,6 +378,14 @@ void HashFileConfigNode::readProperties(map<string, string> &props) const {
|
|||
}
|
||||
}
|
||||
|
||||
void HashFileConfigNode::writeProperties(const map<string, string> &props) {
|
||||
if (!props.empty()) {
|
||||
m_props.insert(props.begin(), props.end());
|
||||
m_modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
string HashFileConfigNode::readProperty(const string &property) const {
|
||||
std::map<std::string, std::string>::const_iterator it = m_props.find(property);
|
||||
if (it != m_props.end()) {
|
||||
|
@ -389,6 +403,14 @@ void HashFileConfigNode::removeProperty(const string &property){
|
|||
}
|
||||
}
|
||||
|
||||
void HashFileConfigNode::clear()
|
||||
{
|
||||
if (!m_props.empty()) {
|
||||
m_props.clear();
|
||||
m_modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
void HashFileConfigNode::setProperty(const string &property,
|
||||
const string &newvalue,
|
||||
const string &comment,
|
||||
|
|
|
@ -106,6 +106,7 @@ class FileConfigNode : public FileBaseConfigNode {
|
|||
const string *defValue = NULL);
|
||||
virtual void readProperties(map<string, string> &props) const;
|
||||
virtual void removeProperty(const string &property);
|
||||
virtual void clear();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -132,7 +133,9 @@ class HashFileConfigNode: public FileBaseConfigNode {
|
|||
const string &comment = "",
|
||||
const string *defValue = NULL);
|
||||
virtual void readProperties(map<string, string> &props) const;
|
||||
virtual void writeProperties(const PropsType &props);
|
||||
virtual void removeProperty(const string &property);
|
||||
virtual void clear();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -103,6 +103,12 @@ void FilterConfigNode::removeProperty(const string &property)
|
|||
m_node->removeProperty(property);
|
||||
}
|
||||
|
||||
void FilterConfigNode::clear()
|
||||
{
|
||||
m_filter.clear();
|
||||
m_node->clear();
|
||||
}
|
||||
|
||||
void FilterConfigNode::flush()
|
||||
{
|
||||
if (!m_node.get()) {
|
||||
|
|
|
@ -82,6 +82,7 @@ class FilterConfigNode : public ConfigNode {
|
|||
virtual void readProperties(map<string, string> &props) const;
|
||||
virtual void removeProperty(const string &property);
|
||||
virtual bool exists() const { return m_readOnlyNode->exists(); }
|
||||
virtual void clear();
|
||||
|
||||
private:
|
||||
ConfigFilter m_filter;
|
||||
|
|
|
@ -45,8 +45,10 @@ class HashConfigNode : public ConfigNode {
|
|||
const string &comment = "",
|
||||
const string *defValue = NULL) { m_props[property] = value; }
|
||||
virtual void readProperties(std::map<std::string, std::string> &props) const { props = m_props; }
|
||||
virtual void writeProperties(const PropsType &props) { m_props.insert(props.begin(), props.end()); }
|
||||
virtual void removeProperty(const std::string &property) { m_props.erase(property); }
|
||||
virtual bool exists() const { return true; }
|
||||
virtual void clear() { m_props.clear(); }
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -63,6 +63,7 @@ class PrefixConfigNode : public ConfigNode {
|
|||
virtual void readProperties(map<string, string> &props) const;
|
||||
virtual void removeProperty(const string &property);
|
||||
virtual bool exists() const { return m_readOnlyNode->exists(); }
|
||||
virtual void clear() { m_node->clear(); }
|
||||
|
||||
private:
|
||||
string m_prefix;
|
||||
|
|
|
@ -84,6 +84,7 @@ class SafeConfigNode : public ConfigNode {
|
|||
virtual void readProperties(map<string, string> &props) const;
|
||||
virtual void removeProperty(const string &property);
|
||||
virtual bool exists() const { return m_readOnlyNode->exists(); }
|
||||
virtual void clear() { m_node->clear(); }
|
||||
|
||||
private:
|
||||
boost::shared_ptr<ConfigNode> m_node;
|
||||
|
|
Loading…
Reference in New Issue