avoid creating default initialised values if key doesn't exist

This commit is contained in:
Artem Pavlenko 2011-10-27 18:02:55 +01:00
parent a35b9c4eaa
commit a3ba07f6b4
3 changed files with 42 additions and 4 deletions

View file

@ -23,10 +23,15 @@
#ifndef MAPNIK_ATTRIBUTE_HPP #ifndef MAPNIK_ATTRIBUTE_HPP
#define MAPNIK_ATTRIBUTE_HPP #define MAPNIK_ATTRIBUTE_HPP
// mapnik
#include <mapnik/value.hpp>
// stl
#include <string> #include <string>
namespace mapnik { namespace mapnik {
static mapnik::value _null_value;
struct attribute struct attribute
{ {
std::string name_; std::string name_;
@ -34,9 +39,15 @@ struct attribute
: name_(name) {} : name_(name) {}
template <typename V ,typename F> template <typename V ,typename F>
V value(F const& f) const V const& value(F const& f) const
{ {
return f[name_]; typedef typename F::const_iterator const_iterator;
const_iterator itr = f.find(name_);
if (itr != f.end())
{
return itr->second;
}
return _null_value;
} }
std::string const& name() const { return name_;} std::string const& name() const { return name_;}
}; };

View file

@ -65,6 +65,7 @@ private:
std::map<std::string,value> props_; std::map<std::string,value> props_;
public: public:
typedef std::map<std::string,value>::iterator iterator; typedef std::map<std::string,value>::iterator iterator;
typedef std::map<std::string,value>::const_iterator const_iterator;
explicit feature(int id) explicit feature(int id)
: properties(props_), : properties(props_),
id_(id), id_(id),
@ -156,6 +157,21 @@ public:
return props_.end(); return props_.end();
} }
const_iterator begin() const
{
return props_.begin();
}
const_iterator end() const
{
return props_.end();
}
const_iterator find(std::string const& key) const
{
return props_.find(key);
}
std::string to_string() const std::string to_string() const
{ {
std::stringstream ss; std::stringstream ss;

View file

@ -47,11 +47,22 @@ struct placement;
class metawriter_property_map class metawriter_property_map
{ {
public: public:
typedef std::map<std::string, UnicodeString> property_map;
typedef property_map::const_iterator const_iterator;
metawriter_property_map() {} metawriter_property_map() {}
UnicodeString const& operator[](std::string const& key) const; UnicodeString const& operator[](std::string const& key) const;
UnicodeString& operator[](std::string const& key) {return m_[key];} UnicodeString& operator[](std::string const& key) {return m_[key];}
std::map<std::string, UnicodeString>::const_iterator find(std::string const& key) const
{
return m_.find(key);
}
std::map<std::string, UnicodeString>::const_iterator end() const
{
return m_.end();
}
private: private:
std::map<std::string, UnicodeString> m_; property_map m_;
UnicodeString not_found_; UnicodeString not_found_;
}; };