1 #ifndef OSMIUM_HANDLER_CHAIN_HPP
2 #define OSMIUM_HANDLER_CHAIN_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <osmium/handler.hpp>
37 
38 #include <tuple>
39 
40 #define OSMIUM_CHAIN_HANDLER_CALL(_func_, _type_) \
41     template <int N, int SIZE, typename THandlers> \
42     struct call_ ## _func_ { \
43         void operator()(THandlers& handlers, osmium::_type_& object) { \
44             std::get<N>(handlers)._func_(object); \
45             call_ ## _func_<N+1, SIZE, THandlers>()(handlers, object); \
46         } \
47     }; \
48     template <int SIZE, typename THandlers> \
49     struct call_ ## _func_<SIZE, SIZE, THandlers> { \
50         void operator()(THandlers&, osmium::_type_&) {} \
51     };
52 
53 namespace osmium {
54 
55     class Node;
56     class Way;
57     class Relation;
58     class Area;
59     class Changeset;
60 
61     namespace handler {
62 
63         /**
64          * This handler allows chaining of any number of handlers into a single
65          * handler.
66          */
67         template <typename... THandler>
68         class ChainHandler : public osmium::handler::Handler {
69 
70             using handlers_type = std::tuple<THandler&...>;
71             handlers_type m_handlers;
72 
73             template <int N, int SIZE, typename THandlers>
74             struct call_flush {
operator ()osmium::handler::ChainHandler::call_flush75                 void operator()(THandlers& handlers) {
76                     std::get<N>(handlers).flush();
77                     call_flush<N + 1, SIZE, THandlers>()(handlers);
78                 }
79             }; // struct call_flush
80 
81             template <int SIZE, typename THandlers>
82             struct call_flush<SIZE, SIZE, THandlers> {
operator ()osmium::handler::ChainHandler::call_flush83                 void operator()(THandlers& /*handlers*/) {}
84             }; // struct call_flush
85 
86             OSMIUM_CHAIN_HANDLER_CALL(node, Node)
87             OSMIUM_CHAIN_HANDLER_CALL(way, Way)
88             OSMIUM_CHAIN_HANDLER_CALL(relation, Relation)
89             OSMIUM_CHAIN_HANDLER_CALL(changeset, Changeset)
90             OSMIUM_CHAIN_HANDLER_CALL(area, Area)
91 
92         public:
93 
ChainHandler(THandler &...handlers)94             explicit ChainHandler(THandler&... handlers) :
95                 m_handlers(handlers...) {
96             }
97 
node(osmium::Node & node)98             void node(osmium::Node& node) {
99                 call_node<0, sizeof...(THandler), handlers_type>()(m_handlers, node);
100             }
101 
way(osmium::Way & way)102             void way(osmium::Way& way) {
103                 call_way<0, sizeof...(THandler), handlers_type>()(m_handlers, way);
104             }
105 
relation(osmium::Relation & relation)106             void relation(osmium::Relation& relation) {
107                 call_relation<0, sizeof...(THandler), handlers_type>()(m_handlers, relation);
108             }
109 
changeset(osmium::Changeset & changeset)110             void changeset(osmium::Changeset& changeset) {
111                 call_changeset<0, sizeof...(THandler), handlers_type>()(m_handlers, changeset);
112             }
113 
area(osmium::Area & area)114             void area(osmium::Area& area) {
115                 call_area<0, sizeof...(THandler), handlers_type>()(m_handlers, area);
116             }
117 
flush()118             void flush() {
119                 call_flush<0, sizeof...(THandler), handlers_type>()(m_handlers);
120             }
121 
122         }; // class ChainHandler
123 
124     } // namespace handler
125 
126 } // namespace osmium
127 
128 #endif // OSMIUM_HANDLER_CHAIN_HPP
129