new leaner feature impl (shared context)

This commit is contained in:
Artem Pavlenko 2012-01-12 09:31:11 +00:00
parent aad26ace9f
commit 500bac1cf8

View file

@ -37,49 +37,74 @@
#endif #endif
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
// stl // stl
#include <map> #include <map>
namespace mapnik { namespace mapnik {
typedef boost::shared_ptr<raster> raster_ptr; typedef boost::shared_ptr<raster> raster_ptr;
typedef boost::associative_property_map< typedef std::map<std::string,int> map_type;
std::map<std::string,value typedef boost::associative_property_map<map_type> base_type;
> > properties;
class feature_impl;
class context : private boost::noncopyable,
public base_type
template <typename T1,typename T2>
struct feature : public properties,
private boost::noncopyable
{ {
friend class feature_impl;
public: public:
typedef T1 geometry_type;
typedef T2 raster_type;
typedef std::map<std::string,value>::value_type value_type;
typedef std::map<std::string,value>::size_type size_type;
typedef std::map<std::string,value>::difference_type difference_type;
private: context()
int id_; : base_type(mapping_) {}
boost::ptr_vector<geometry_type> geom_cont_;
raster_type raster_;
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),
geom_cont_(),
raster_() {}
int id() const void insert(std::string const& name,int index)
{ {
return id_; mapping_.insert(std::make_pair(name,index));
} }
void set_id(int id) private:
map_type mapping_;
};
typedef boost::shared_ptr<context> context_ptr;
class feature_impl : private boost::noncopyable
{
public:
typedef mapnik::value value_type;
typedef std::vector<value_type> 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 <typename T>
void set(std::string const& key, T const& val)
{ {
id_ = id; 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<geometry_type> & paths() boost::ptr_vector<geometry_type> & paths()
@ -87,7 +112,6 @@ public:
return geom_cont_; return geom_cont_;
} }
void add_geometry(geometry_type * geom) void add_geometry(geometry_type * geom)
{ {
geom_cont_.push_back(geom); geom_cont_.push_back(geom);
@ -108,92 +132,49 @@ public:
return geom_cont_[index]; return geom_cont_[index];
} }
box2d<double> envelope() const const raster_ptr& get_raster() const
{
box2d<double> result;
for (unsigned i=0;i<num_geometries();++i)
{
geometry_type const& geom = get_geometry(i);
if (i==0)
{
box2d<double> 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
{ {
return raster_; return raster_;
} }
void set_raster(raster_type const& raster)
{
raster_=raster;
}
std::map<std::string,value> const& props() const void set_raster(raster_ptr const& raster)
{ {
return props_; raster_ = raster;
}
std::map<std::string,value>& props()
{
return props_;
}
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::string to_string() const
{ {
std::stringstream ss; std::stringstream ss;
ss << "feature " ss << "Feature (" << std::endl;
<< id_ << " (" << std::endl; map_type::const_iterator itr = ctx_->mapping_.begin();
for (std::map<std::string,value>::const_iterator itr=props_.begin(); map_type::const_iterator end = ctx_->mapping_.end();
itr != props_.end();++itr) for ( ;itr!=end; ++itr)
{ {
ss << " " << itr->first << ":" << itr->second << std::endl; ss << " " << itr->first << ":" << data_[itr->second] << std::endl;
} }
ss << ")" << std::endl; ss << ")" << std::endl;
return ss.str(); return ss.str();
} }
private:
context_ptr ctx_;
int id_;
boost::ptr_vector<geometry_type> geom_cont_;
raster_ptr raster_;
cont_type data_;
}; };
typedef feature<geometry_type,raster_ptr> 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(); out << f.to_string();
return out; return out;
} }
typedef feature_impl Feature;
} }
#endif // MAPNIK_FEATURE_HPP #endif // MAPNIK_FEATURE_HPP