marked const conftree methods

This commit is contained in:
Jean-Francois Dockes 2012-10-26 14:52:17 +02:00
parent 27da60c015
commit d2757f3bce
2 changed files with 59 additions and 39 deletions

View File

@ -201,9 +201,17 @@ ConfSimple::StatusCode ConfSimple::getStatus() const
}
}
bool ConfSimple::sourceChanged()
bool ConfSimple::sourceChanged() const
{
return i_changed(false);
if (!m_filename.empty()) {
struct stat st;
if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) {
return true;
}
}
}
return false;
}
bool ConfSimple::i_changed(bool upd)
@ -408,12 +416,13 @@ int ConfSimple::eraseKey(const string &sk)
// Walk the tree, calling user function at each node
ConfSimple::WalkerCode
ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
void *clidata)
void *clidata) const
{
if (!ok())
return WALK_STOP;
// For all submaps:
for (map<string, map<string, string> >::iterator sit = m_submaps.begin();
for (map<string, map<string, string> >::const_iterator sit =
m_submaps.begin();
sit != m_submaps.end(); sit++) {
// Possibly emit submap name:
@ -422,8 +431,8 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
return WALK_STOP;
// Walk submap
map<string, string> &sm = sit->second;
for (map<string, string>::iterator it = sm.begin();it != sm.end();
const map<string, string> &sm = sit->second;
for (map<string, string>::const_iterator it = sm.begin();it != sm.end();
it++) {
if (walker(clidata, it->first, it->second) == WALK_STOP)
return WALK_STOP;
@ -505,19 +514,19 @@ bool ConfSimple::write(ostream& out) const
return true;
}
void ConfSimple::showall()
void ConfSimple::showall() const
{
if (!ok())
return;
write(std::cout);
}
vector<string> ConfSimple::getNames(const string &sk, const char *pattern)
vector<string> ConfSimple::getNames(const string &sk, const char *pattern) const
{
vector<string> mylist;
if (!ok())
return mylist;
map<string, map<string, string> >::iterator ss;
map<string, map<string, string> >::const_iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
return mylist;
}
@ -531,20 +540,20 @@ vector<string> ConfSimple::getNames(const string &sk, const char *pattern)
return mylist;
}
vector<string> ConfSimple::getSubKeys()
vector<string> ConfSimple::getSubKeys() const
{
vector<string> mylist;
if (!ok())
return mylist;
mylist.reserve(m_submaps.size());
map<string, map<string, string> >::iterator ss;
map<string, map<string, string> >::const_iterator ss;
for (ss = m_submaps.begin(); ss != m_submaps.end(); ss++) {
mylist.push_back(ss->first);
}
return mylist;
}
bool ConfSimple::hasNameAnywhere(const string& nm)
bool ConfSimple::hasNameAnywhere(const string& nm) const
{
vector<string>keys = getSubKeys();
for (vector<string>::const_iterator it = keys.begin();

View File

@ -97,18 +97,18 @@ public:
virtual ~ConfNull() {};
virtual int get(const string &name, string &value,
const string &sk = string()) const = 0;
virtual bool hasNameAnywhere(const string& nm) = 0;
virtual bool hasNameAnywhere(const string& nm) const = 0;
virtual int set(const string &nm, const string &val,
const string &sk = string()) = 0;
virtual bool ok() const = 0;
virtual vector<string> getNames(const string &sk, const char* = 0) = 0;
virtual vector<string> getNames(const string &sk, const char* = 0)const = 0;
virtual int erase(const string &, const string &) = 0;
virtual int eraseKey(const string &) = 0;
virtual void showall() {};
virtual vector<string> getSubKeys() = 0;
virtual vector<string> getSubKeys(bool) = 0;
virtual void showall() const {};
virtual vector<string> getSubKeys() const = 0;
virtual vector<string> getSubKeys(bool) const = 0;
virtual bool holdWrites(bool) = 0;
virtual bool sourceChanged() = 0;
virtual bool sourceChanged() const = 0;
};
/**
@ -143,7 +143,7 @@ public:
virtual ~ConfSimple() {};
/** Origin file changed. Only makes sense if we read the data from a file */
virtual bool sourceChanged();
virtual bool sourceChanged() const;
/**
* Decide if we actually rewrite the backing-store after modifying the
@ -197,30 +197,35 @@ public:
virtual WalkerCode sortwalk(WalkerCode
(*wlkr)(void *cldata, const string &nm,
const string &val),
void *clidata);
void *clidata) const;
/** Print all values to stdout */
virtual void showall();
virtual void showall() const;
/** Return all names in given submap. */
virtual vector<string> getNames(const string &sk, const char *pattern = 0);
virtual vector<string> getNames(const string &sk, const char *pattern = 0)
const;
/** Check if name is present in any submap. This is relatively expensive
* but useful for saving further processing sometimes */
virtual bool hasNameAnywhere(const string& nm);
virtual bool hasNameAnywhere(const string& nm) const;
/**
* Return all subkeys
*/
virtual vector<string> getSubKeys(bool) {return getSubKeys();}
virtual vector<string> getSubKeys();
virtual vector<string> getSubKeys(bool) const
{
return getSubKeys();
}
virtual vector<string> getSubKeys() const;
/** Test for subkey existence */
virtual bool hasSubKey(const string& sk)
virtual bool hasSubKey(const string& sk) const
{
return m_submaps.find(sk) != m_submaps.end();
}
virtual string getFilename() {return m_filename;}
virtual string getFilename() const
{return m_filename;}
/**
* Copy constructor. Expensive but less so than a full rebuild
@ -306,7 +311,8 @@ public:
: ConfSimple(readonly, true) {}
virtual ~ConfTree() {};
ConfTree(const ConfTree& r) : ConfSimple(r) {};
ConfTree& operator=(const ConfTree& r) {
ConfTree& operator=(const ConfTree& r)
{
ConfSimple::operator=(r);
return *this;
}
@ -373,7 +379,7 @@ public:
return *this;
}
virtual bool sourceChanged()
virtual bool sourceChanged() const
{
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
@ -393,9 +399,9 @@ public:
return false;
}
virtual bool hasNameAnywhere(const string& nm)
virtual bool hasNameAnywhere(const string& nm) const
{
typename vector<T*>::iterator it;
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->hasNameAnywhere(nm))
return true;
@ -448,21 +454,23 @@ public:
}
virtual vector<string> getNames(const string &sk, const char *pattern = 0)
const
{
return getNames1(sk, pattern, false);
}
virtual vector<string> getNamesShallow(const string &sk, const char *patt = 0)
virtual vector<string> getNamesShallow(const string &sk,
const char *patt = 0) const
{
return getNames1(sk, patt, true);
}
virtual vector<string> getNames1(const string &sk, const char *pattern,
bool shallow)
bool shallow) const
{
vector<string> nms;
typename vector<T*>::iterator it;
typename vector<T*>::const_iterator it;
bool skfound = false;
for (it = m_confs.begin();it != m_confs.end(); it++) {
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasSubKey(sk)) {
skfound = true;
vector<string> lst = (*it)->getNames(sk, pattern);
@ -477,12 +485,15 @@ public:
return nms;
}
virtual vector<string> getSubKeys(){return getSubKeys(false);}
virtual vector<string> getSubKeys(bool shallow)
virtual vector<string> getSubKeys() const
{
return getSubKeys(false);
}
virtual vector<string> getSubKeys(bool shallow) const
{
vector<string> sks;
typename vector<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end(); it++) {
typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
vector<string> lst;
lst = (*it)->getSubKeys();
sks.insert(sks.end(), lst.begin(), lst.end());