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
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
// stl
#include <map>
namespace mapnik {
typedef boost::shared_ptr<raster> raster_ptr;
typedef boost::associative_property_map<
std::map<std::string,value
> > properties;
typedef std::map<std::string,int> map_type;
typedef boost::associative_property_map<map_type> base_type;
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:
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:
int id_;
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_() {}
context()
: base_type(mapping_) {}
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
{
id_ = id;
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)
{
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()
@ -87,7 +112,6 @@ public:
return geom_cont_;
}
void add_geometry(geometry_type * geom)
{
geom_cont_.push_back(geom);
@ -108,92 +132,49 @@ public:
return geom_cont_[index];
}
box2d<double> envelope() 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
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<std::string,value> const& props() const
{
return props_;
}
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::stringstream ss;
ss << "feature "
<< id_ << " (" << std::endl;
for (std::map<std::string,value>::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<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();
return out;
}
typedef feature_impl Feature;
}
#endif // MAPNIK_FEATURE_HPP