From 500bac1cf8da0d59d9885cffd77382ce870c98c8 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Thu, 12 Jan 2012 09:31:11 +0000 Subject: [PATCH] new leaner feature impl (shared context) --- include/mapnik/feature.hpp | 191 +++++++++++++++++-------------------- 1 file changed, 86 insertions(+), 105 deletions(-) diff --git a/include/mapnik/feature.hpp b/include/mapnik/feature.hpp index fd8347a3e..a2d153195 100644 --- a/include/mapnik/feature.hpp +++ b/include/mapnik/feature.hpp @@ -37,57 +37,81 @@ #endif #include #include +#include // stl #include namespace mapnik { -typedef boost::shared_ptr raster_ptr; -typedef boost::associative_property_map< -std::map > properties; - -template -struct feature : public properties, - private boost::noncopyable -{ -public: - typedef T1 geometry_type; - typedef T2 raster_type; - typedef std::map::value_type value_type; - typedef std::map::size_type size_type; - typedef std::map::difference_type difference_type; - -private: - int id_; - boost::ptr_vector geom_cont_; - raster_type raster_; - std::map props_; -public: - typedef std::map::iterator iterator; - typedef std::map::const_iterator const_iterator; - explicit feature(int id) - : properties(props_), - id_(id), - geom_cont_(), - raster_() {} - - int id() const - { - return id_; - } - void set_id(int id) + +typedef boost::shared_ptr raster_ptr; +typedef std::map map_type; +typedef boost::associative_property_map base_type; + +class feature_impl; + +class context : private boost::noncopyable, + public base_type + +{ + friend class feature_impl; +public: + + context() + : base_type(mapping_) {} + + void insert(std::string const& name,int index) { - id_ = id; + mapping_.insert(std::make_pair(name,index)); } +private: + map_type mapping_; +}; + +typedef boost::shared_ptr context_ptr; + +class feature_impl : private boost::noncopyable +{ +public: + typedef mapnik::value value_type; + typedef std::vector cont_type; + + feature_impl(context_ptr const& ctx, int id) + : id_(id), + ctx_(ctx), + data_(ctx_->mapping_.size()) + {} + + inline int id() const { return id_;} + void set_id(int id) { id_ = id;} + template + void set(std::string const& key, T const& val) + { + map_type::const_iterator itr = ctx_->mapping_.find(key); + if (itr != ctx_->mapping_.end()) + { + data_[itr->second] = value(val); + } + } + + value_type const& get(std::string const& key) const + { + map_type::const_iterator itr = ctx_->mapping_.find(key); + if (itr != ctx_->mapping_.end()) + { + return data_[itr->second]; + } + static const value_type default_value; + return default_value; + } + boost::ptr_vector & paths() { return geom_cont_; } - - + void add_geometry(geometry_type * geom) { geom_cont_.push_back(geom); @@ -108,92 +132,49 @@ public: return geom_cont_[index]; } - box2d envelope() const - { - box2d result; - for (unsigned i=0;i box = geom.envelope(); - result.init(box.minx(),box.miny(),box.maxx(),box.maxy()); - } - else - { - result.expand_to_include(geom.envelope()); - } - } - return result; - } - - const raster_type& get_raster() const + const raster_ptr& get_raster() const { return raster_; } - - void set_raster(raster_type const& raster) + + + void set_raster(raster_ptr const& raster) { - raster_=raster; - } - - std::map const& props() const - { - return props_; - } - - std::map& props() - { - return props_; + raster_ = raster; } - iterator begin() - { - return props_.begin(); - } - - iterator 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::stringstream ss; - ss << "feature " - << id_ << " (" << std::endl; - for (std::map::const_iterator itr=props_.begin(); - itr != props_.end();++itr) + ss << "Feature (" << std::endl; + map_type::const_iterator itr = ctx_->mapping_.begin(); + map_type::const_iterator end = ctx_->mapping_.end(); + for ( ;itr!=end; ++itr) { - ss << " " << itr->first << ":" << itr->second << std::endl; + ss << " " << itr->first << ":" << data_[itr->second] << std::endl; } ss << ")" << std::endl; return ss.str(); } + +private: + context_ptr ctx_; + int id_; + boost::ptr_vector geom_cont_; + raster_ptr raster_; + + cont_type data_; }; + -typedef feature Feature; - -inline std::ostream& operator<< (std::ostream & out,Feature const& f) +inline std::ostream& operator<< (std::ostream & out,feature_impl const& f) { out << f.to_string(); return out; } + +typedef feature_impl Feature; + } #endif // MAPNIK_FEATURE_HPP