+ add get_optional to mapnik::feature_impl

returns -> boost::optional<mapnik::value const&>
+ use get_optional in feature_kv_iterator to avoid
  throwing exceptions
This commit is contained in:
Artem Pavlenko 2012-02-17 10:46:29 +00:00
parent 63735982ee
commit c6a091937f
2 changed files with 17 additions and 12 deletions

View file

@ -159,16 +159,11 @@ public:
value_type const& get(context_type::key_type const& key) const
{
context_type::map_type::const_iterator itr = ctx_->mapping_.find(key);
if (itr != ctx_->mapping_.end()
&& itr->second < data_.size())
{
return data_[itr->second];
}
if (itr != ctx_->mapping_.end())
return get(itr->second);
else
{
throw std::out_of_range(std::string("Key does not exist: '") + key + "'");
}
}
value_type const& get(std::size_t index) const
{
@ -177,6 +172,13 @@ public:
throw std::out_of_range("Index out of range");
}
boost::optional<value_type const&> get_optional(std::size_t index) const
{
if (index < data_.size())
return boost::optional<value_type const&>(data_[index]);
return boost::optional<value_type const&>();
}
std::size_t size() const
{
return data_.size();

View file

@ -22,6 +22,7 @@
#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/feature.hpp>
#include <boost/optional.hpp>
namespace mapnik {
@ -44,7 +45,9 @@ bool feature_kv_iterator::equal( feature_kv_iterator const& other) const
feature_kv_iterator::value_type const& feature_kv_iterator::dereference() const
{
boost::get<0>(kv_) = itr_->first;
boost::get<1>(kv_) = f_.get(itr_->first);
boost::optional<mapnik::value const&> val = f_.get_optional(itr_->second);
if (val) boost::get<1>(kv_) = *val;
else boost::get<1>(kv_) = value_null();
return kv_;
}