Skip to content
Browse files

avoid creating default initialised values if key doesn't exist

  • Loading branch information...
1 parent 28e7c83 commit 0f5ab18ed627c66d56f44fc55fe5ce82c3b1156b @artemp artemp committed Oct 27, 2011
Showing with 42 additions and 4 deletions.
  1. +13 −2 include/mapnik/attribute.hpp
  2. +17 −1 include/mapnik/feature.hpp
  3. +12 −1 include/mapnik/metawriter.hpp
View
15 include/mapnik/attribute.hpp
@@ -23,20 +23,31 @@
#ifndef MAPNIK_ATTRIBUTE_HPP
#define MAPNIK_ATTRIBUTE_HPP
+// mapnik
+#include <mapnik/value.hpp>
+// stl
#include <string>
namespace mapnik {
+static mapnik::value _null_value;
+
struct attribute
{
std::string name_;
explicit attribute(std::string const& name)
: name_(name) {}
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_;}
};
View
18 include/mapnik/feature.hpp
@@ -65,6 +65,7 @@ struct feature : public properties,
std::map<std::string,value> props_;
public:
typedef std::map<std::string,value>::iterator iterator;
+ typedef std::map<std::string,value>::const_iterator const_iterator;
explicit feature(int id)
: properties(props_),
id_(id),
@@ -145,7 +146,7 @@ struct feature : public properties,
{
return props_;
}
-
+
iterator begin()
{
return props_.begin();
@@ -155,7 +156,22 @@ struct feature : public properties,
{
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::stringstream ss;
View
13 include/mapnik/metawriter.hpp
@@ -47,11 +47,22 @@ struct placement;
class metawriter_property_map
{
public:
+ typedef std::map<std::string, UnicodeString> property_map;
+ typedef property_map::const_iterator const_iterator;
+
metawriter_property_map() {}
UnicodeString const& operator[](std::string const& key) const;
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:
- std::map<std::string, UnicodeString> m_;
+ property_map m_;
UnicodeString not_found_;
};

0 comments on commit 0f5ab18

Please sign in to comment.
Something went wrong with that request. Please try again.