Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/statement.h

overpass_api/statements/statement.h

55 of 72 instrumented lines run by the suite (76.4%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__STATEMENT_H
20#define DE__OSM3S___OVERPASS_API__STATEMENTS__STATEMENT_H
22#include <map>
23#include <set>
24#include <vector>
26#include "../core/datatypes.h"
27#include "../core/parsed_query.h"
28#include "../core/settings.h"
29#include "../data/diff_set.h"
30#include "../dispatch/resource_manager.h"
31#include "../frontend/tokenizer_utils.h"
32#include "../osm-backend/area_updater.h"
35typedef enum { ids_required, ids_useful, prefer_ranges } Query_Filter_Strategy;
37typedef enum { nothing, /*ids_collected,*/ ranges_collected, data_collected } Answer_State;
40template< typename Id >
41struct Id_Constraint
433516507 q Id_Constraint() : invert(true) {}
441519507 q bool empty() const { return !invert && ids.empty(); }
45 void restrict_to(const std::vector< Id >& ids);
47 std::vector< Id > ids;
48 bool invert;
49};
52class Query_Constraint
54 public:
56 virtual Query_Filter_Strategy delivers_data(Resource_Manager& rman) = 0;
58 virtual bool collect_nodes(Resource_Manager& rman, Set& into,
59284193 q const std::vector< Uint64 >& ids, bool invert_ids) { return false; }
60 virtual bool collect(Resource_Manager& rman, Set& into,
61650342 q int type, const std::vector< Uint32_Index >& ids, bool invert_ids) { return false; }
620 virtual bool collect(Resource_Manager& rman, Set& into) { return false; }
64 virtual bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
653221 q { return false; }
66 virtual bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
671211 q { return false; }
69 virtual Ranges< Uint32_Index > get_node_ranges(Resource_Manager& rman)
70270185 q {
71270185 q Ranges< Uint32_Index > result;
72270185 q if (get_ranges(rman, result))
73227181 q return result;
744334 q return Ranges< Uint32_Index >::global();
75270185 q }
76 virtual Ranges< Uint31_Index > get_way_ranges(Resource_Manager& rman)
77349248 q {
78349248 q Ranges< Uint31_Index > result;
79349248 q if (get_ranges(rman, result))
80296237 q return result;
815343 q return Ranges< Uint31_Index >::global();
82349248 q }
83 virtual Ranges< Uint31_Index > get_relation_ranges(Resource_Manager& rman)
84233176 q {
85233176 q Ranges< Uint31_Index > result;
86233176 q if (get_ranges(rman, result))
87196161 q return result;
883732 q return Ranges< Uint31_Index >::global();
89233176 q }
91 virtual bool get_node_ids
92 (Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids)
93261181 q { return false; }
94 virtual bool get_way_ids
95 (Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids)
96342244 q { return false; }
97 virtual bool get_relation_ids
98 (Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids)
99233178 q { return false; }
100 virtual bool get_area_ids
101 (Resource_Manager& rman, std::vector< Area_Skeleton::Id_Type >& ids)
10243 q { return false; }
104 virtual bool get_data(const Statement& query, Resource_Manager& rman, Set& into,
105 const Ranges< Uint32_Index >& ranges,
106 const std::vector< Node::Id_Type >& ids,
107 bool invert_ids)
108268184 q { return false; }
109 virtual bool get_data(const Statement& query, Resource_Manager& rman, Set& into,
110 const Ranges< Uint31_Index >& ranges,
111 int type,
112 const std::vector< Uint32_Index >& ids,
113 bool invert_ids)
114581303 q { return false; }
116 // Cheap filter. No health_check in between needed and should be called first.
1171311 q virtual void filter(Resource_Manager& rman, Set& into) {}
119 // Expensive filter. Health_check may be needed in between. These are called last
120 // to minimize the number of elements that need to be processed.
121133100 q virtual void filter(const Statement& query, Resource_Manager& rman, Set& into) {}
1231910442 q virtual ~Query_Constraint() {}
124};
126/**
127 * The base class for all statements
128 */
129class Statement
131 public:
132 enum QL_Context { generic, in_convert, evaluator_expected, elem_eval_possible, member_eval_possible };
134 enum Eval_Return_Type { non_evaluator, string, container, geometry };
135 static std::string eval_to_string(Statement::Eval_Return_Type eval_type);
137 struct Return_Type_Checker
138 {
139 virtual bool eval_required() const = 0;
140 virtual bool matches(Eval_Return_Type eval_type) const = 0;
141 virtual std::string expectation() const = 0;
14213208210 q virtual ~Return_Type_Checker() {}
143 };
145 struct Single_Return_Type_Checker : Return_Type_Checker
146 {
14710748203 q Single_Return_Type_Checker(Eval_Return_Type expected_) : expected(expected_) {}
14910708200 q virtual bool eval_required() const { return true; }
15010708200 q virtual bool matches(Eval_Return_Type eval_type) const { return eval_type == expected; }
15141 q virtual std::string expectation() const { return eval_to_string(expected); }
1520 virtual ~Single_Return_Type_Checker() {}
154 private:
155 Eval_Return_Type expected;
156 };
158 struct Factory
159 {
1602448612 q Factory(Parsed_Query& global_settings_) : error_output_(error_output), global_settings(global_settings_) {}
161 ~Factory();
163 Statement* create_statement(std::string element, int line_number,
164 const std::map< std::string, std::string >& attributes);
165 Statement* create_evaluator(
166 const Token_Node_Ptr& tree_it, QL_Context tree_context, const Statement::Return_Type_Checker& eval_type);
167 Statement* create_criterion(const Token_Node_Ptr& tree_it,
168 const std::string& type, bool& can_standalone, const std::string& into);
170 std::vector< Statement* > created_statements;
171 Error_Output* error_output_;
172 Parsed_Query& global_settings;
173 };
175 struct Statement_Maker
176 {
177 virtual Statement* create_statement
178 (int line_number, const std::map< std::string, std::string >& attributes, Parsed_Query& global_settings) = 0;
1790 virtual ~Statement_Maker() {}
180 };
182 struct Criterion_Maker
183 {
184 virtual bool can_standalone(const std::string& type) = 0;
185 virtual Statement* create_criterion(const Token_Node_Ptr& tree_it,
186 const std::string& type, const std::string& into,
187 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output) = 0;
1880 virtual ~Criterion_Maker() {}
189 };
191 struct Evaluator_Maker
192 {
193 virtual Statement* create_evaluator(const Token_Node_Ptr& tree_it, QL_Context tree_context,
194 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output) = 0;
1950 virtual ~Evaluator_Maker() {}
196 };
198 static std::map< std::string, Statement_Maker* >& maker_by_name();
199 static std::map< std::string, Criterion_Maker* >& maker_by_ql_criterion();
200 static std::map< std::string, std::vector< Evaluator_Maker* > >& maker_by_token();
201 static std::map< std::string, std::vector< Evaluator_Maker* > >& maker_by_func_name();
20333940612 q Statement(int line_number_) : line_number(line_number_), progress(0) {}
205 virtual void add_statement(Statement* statement, std::string text);
206 virtual void add_final_text(std::string text);
207 virtual std::string get_name() const = 0;
208 virtual std::string get_result_name() const = 0;
209 virtual void execute(Resource_Manager& rman) = 0;
211 // May return 0. The ownership of the Query_Constraint remains at the called
212 // object.
2130 virtual Query_Constraint* get_query_constraint() { return 0; }
21533940612 q virtual ~Statement() {}
2170 int get_progress() const { return progress; }
2180 int get_line_number() const { return line_number; }
2190 int get_startpos() const { return startpos; }
2200 void set_startpos(int pos) { startpos = pos; }
2210 int get_endpos() const { return endpos; }
2220 void set_endpos(int pos) { endpos = pos; }
2230 int get_tagendpos() const { return tagendpos; }
2240 void set_tagendpos(int pos) { tagendpos = pos; }
226 void display_full();
227 void display_starttag();
2290 virtual std::string dump_xml(const std::string&) const { return ""; }
2300 virtual std::string dump_compact_ql(const std::string&) const { return ""; }
2310 virtual std::string dump_pretty_ql(const std::string&) const { return ""; }
2321236378 q virtual std::string dump_ql_in_query(const std::string& indent) const { return dump_compact_ql(indent); }
234 static void set_error_output(Error_Output* error_output_)
2352448612 q {
2362448612 q error_output = error_output_;
2372448612 q }
239 void runtime_error(std::string error) const;
240 void runtime_remark(std::string error) const;
242 const static int NODE = 1;
243 const static int WAY = 2;
244 const static int RELATION = 4;
245 const static int AREA = 8;
246 const static int DERIVED = 16;
248 private:
249 static Error_Output* error_output;
251 int line_number;
252 int startpos, endpos, tagendpos;
253 int progress;
255 protected:
256 void eval_attributes_array
257 (std::string element, std::map< std::string, std::string >& attributes,
258 const std::map< std::string, std::string >& input);
259 void assure_no_text(std::string text, std::string name);
260 void substatement_error(std::string parent, Statement* child);
262 void add_static_error(std::string error);
263 void add_static_remark(std::string remark);
2657911507 q void set_progress(int progress_) { progress = progress_; }
266};
269std::map< std::string, std::string > convert_c_pairs(const char** attr);
272template< class TStatement >
273class Generic_Statement_Maker : public Statement::Statement_Maker
275 public:
276 virtual Statement* create_statement
277 (int line_number, const std::map< std::string, std::string >& attributes, Parsed_Query& global_settings)
27817687612 q {
27917687612 q return new TStatement(line_number, attributes, global_settings);
28017687612 q }
282239904612 q Generic_Statement_Maker(const std::string& name) { Statement::maker_by_name()[name] = this; }
283 virtual ~Generic_Statement_Maker() {}
284};
287class Output_Statement : public Statement
289 public:
2907513607 q Output_Statement(int line_number) : Statement(line_number), output("_") {}
29214770147 q virtual std::string get_result_name() const { return output; }
2943584605 q std::string dump_ql_result_name() const { return output != "_" ? std::string("->.") + output : ""; }
295683477 q std::string dump_xml_result_name() const { return output != "_" ? std::string(" into=\"") + output + "\"" : ""; }
297 protected:
2987192607 q void set_output(std::string output_) { output = output_; }
300 void transfer_output(Resource_Manager& rman, Set& into) const;
301 void transfer_output(Resource_Manager& rman, Diff_Set& into) const;
303 private:
304 std::string output;
305};
308#endif