green refactor: Use shared_ptr strings
This commit is contained in:
parent
d065aff2d3
commit
577a5908fd
2 changed files with 9 additions and 6 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include "node.h"
|
||||
#include "memory.h"
|
||||
|
||||
/*!
|
||||
Caches string values per node based on the node.index().
|
||||
|
|
@ -16,14 +17,15 @@ public:
|
|||
virtual ~NodeCache() { }
|
||||
|
||||
bool contains(const AbstractNode &node) const {
|
||||
return !(*this)[node].empty();
|
||||
if (this->cache.size() > node.index()) return this->cache[node.index()].get();
|
||||
return false;
|
||||
}
|
||||
|
||||
/*! Returns a reference to the cached string copy. NB! don't rely on
|
||||
* this reference to be valid for long - if the cache is resized
|
||||
* internally, existing values are lost. */
|
||||
const std::string & operator[](const AbstractNode &node) const {
|
||||
if (this->cache.size() > node.index()) return this->cache[node.index()];
|
||||
if (this->cache.size() > node.index()) return *this->cache[node.index()];
|
||||
else return this->nullvalue;
|
||||
}
|
||||
|
||||
|
|
@ -32,11 +34,12 @@ public:
|
|||
* internally, existing values are lost. */
|
||||
const std::string &insert(const class AbstractNode &node, const std::string & value) {
|
||||
if (this->cache.size() <= node.index()) this->cache.resize(node.index() + 1);
|
||||
return this->cache[node.index()] = value;
|
||||
this->cache[node.index()].reset(new std::string(value));
|
||||
return *this->cache[node.index()];
|
||||
}
|
||||
|
||||
void remove(const class AbstractNode &node) {
|
||||
if (this->cache.size() > node.index()) this->cache[node.index()] = std::string();
|
||||
if (this->cache.size() > node.index()) this->cache[node.index()].reset();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
|
|
@ -44,6 +47,6 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
std::vector<std::string> cache;
|
||||
std::vector<shared_ptr<std::string> > cache;
|
||||
std::string nullvalue;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
bool NodeDumper::isCached(const AbstractNode &node) const
|
||||
{
|
||||
return !this->cache[node].empty();
|
||||
return this->cache.contains(node);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
Loading…
Reference in a new issue