Skip to content

Commit

Permalink
make sure that the handler bitfield is always initialised
Browse files Browse the repository at this point in the history
Add a new virtual function apply_start() to BaseHandler which
must forcably be called before running the handler through
osmium::apply (the C++ function).

Fixes #38.
  • Loading branch information
lonvia committed May 25, 2017
1 parent 3db768c commit 284285c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/generic_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BaseHandler : public osmium::handler::Handler {
};

public:
virtual void apply_start() {};
// handler functions
virtual void node(const osmium::Node&) const = 0;
virtual void way(const osmium::Way&) const = 0;
Expand Down Expand Up @@ -163,14 +164,7 @@ struct SimpleHandlerWrap: BaseHandler, wrapper<BaseHandler> {
apply_object(osmium::io::File(cbuf, len, cfmt), locations, idx);
}

private:
void apply_object(osmium::io::File file, bool locations, const std::string &idx)
{
osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nothing;
BaseHandler::pre_handler handler = locations?
BaseHandler::location_handler
:BaseHandler::no_handler;

void apply_start() override {
m_callbacks = osmium::osm_entity_bits::nothing;
if (hasfunc("node"))
m_callbacks |= osmium::osm_entity_bits::node;
Expand All @@ -182,6 +176,18 @@ struct SimpleHandlerWrap: BaseHandler, wrapper<BaseHandler> {
m_callbacks |= osmium::osm_entity_bits::area;
if (hasfunc("changeset"))
m_callbacks |= osmium::osm_entity_bits::changeset;
}


private:
void apply_object(osmium::io::File file, bool locations, const std::string &idx)
{
osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::nothing;
BaseHandler::pre_handler handler = locations?
BaseHandler::location_handler
:BaseHandler::no_handler;

apply_start();

if (m_callbacks & osmium::osm_entity_bits::area)
{
Expand All @@ -202,6 +208,7 @@ struct SimpleHandlerWrap: BaseHandler, wrapper<BaseHandler> {
apply(file, entities, handler, idx);
}


bool hasfunc(char const *name) {
reference_existing_object::apply<SimpleHandlerWrap*>::type converter;
PyObject* obj = converter( this );
Expand Down
1 change: 1 addition & 0 deletions lib/merged_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace pyosmium {
class MergeInputReader {
public:
void apply(BaseHandler& handler, bool simplify = true) {
handler.apply_start();
if (simplify) {
objects.sort(osmium::object_order_type_id_reverse_version());
osmium::item_type prev_type = osmium::item_type::undefined;
Expand Down
5 changes: 5 additions & 0 deletions lib/osmium.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ void apply_reader_simple(osmium::io::Reader &rd, T &h) {
osmium::apply(rd, h);
}

void apply_reader_simple(osmium::io::Reader &rd, BaseHandler &h) {
h.apply_start();
osmium::apply(rd, h);
}

template <typename T>
void apply_reader_simple_with_location(osmium::io::Reader &rd,
osmium::handler::NodeLocationsForWays<T> &l,
BaseHandler &h) {
h.apply_start();
osmium::apply(rd, l, h);
}

Expand Down

0 comments on commit 284285c

Please sign in to comment.