Skip to content

Commit 82cdb3f

Browse files
committed
Remove template argument of Cql2Cpp
Change-Id: Ie506852b4006fcc93e1ea4e32546d4e83bdaa240 Signed-off-by: Kunlin Yu <yukunlin@syriusrobotics.com>
1 parent 8be99e3 commit 82cdb3f

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

include/cql2cpp/cql2cpp.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#pragma once
1212

13-
#include <map>
1413
#include <string>
1514
#include <variant>
1615
#include <vector>
@@ -30,10 +29,10 @@
3029

3130
namespace cql2cpp {
3231

33-
template <typename FeatureType>
32+
3433
class Cql2Cpp {
3534
private:
36-
std::map<FeatureSourcePtr, FeatureType> feature_source_2_type_;
35+
std::vector<FeatureSourcePtr> features_;
3736
std::ostream& ostr_;
3837
Evaluator evaluator_;
3938

@@ -44,18 +43,18 @@ class Cql2Cpp {
4443

4544
Cql2Cpp(std::ostream& ostr) : ostr_(ostr) {}
4645

47-
void set_feature_source(const std::map<FeatureSourcePtr, FeatureType> fs2t) {
48-
feature_source_2_type_ = fs2t;
46+
void set_feature_source(const std::vector<FeatureSourcePtr> feature_source) {
47+
features_ = feature_source;
4948
}
5049

51-
void clear() { feature_source_2_type_.clear(); }
50+
void clear() { features_.clear(); }
5251

5352
void RegisterFunctor(const FunctorPtr functor) {
5453
evaluator_.RegisterFunctor(functor);
5554
}
5655

5756
bool filter(const std::string& cql2_query,
58-
std::vector<FeatureType>* result) const {
57+
std::vector<FeatureSourcePtr>* result) const {
5958
// Parse
6059
AstNodePtr root;
6160
if (not Parse(cql2_query, &root, &error_msg_)) return false;
@@ -64,8 +63,8 @@ class Cql2Cpp {
6463
ValueT value;
6564

6665
// Loop over all features
67-
for (const auto& [fs, f] : feature_source_2_type_) {
68-
if (evaluator_.Evaluate(root, fs.get(), &value)) {
66+
for (const auto& f : features_) {
67+
if (evaluator_.Evaluate(root, f.get(), &value)) {
6968
if (std::holds_alternative<bool>(value)) {
7069
if (std::get<bool>(value)) result->emplace_back(f);
7170
} else {

src/main.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main(int argc, char** argv) {
9898
cql2cpp::AstNode::set_ostream(&std::cout);
9999
std::string dot;
100100
std::string error_msg;
101-
cql2cpp::Cql2Cpp<geos::io::GeoJSONFeature> cql2cpp;
101+
cql2cpp::Cql2Cpp cql2cpp;
102102
if (cql2cpp.ToDot(parse_command.get<std::string>("query"), &dot,
103103
&error_msg)) {
104104
if (parse_command.is_used("--output")) {
@@ -146,23 +146,26 @@ int main(int argc, char** argv) {
146146
LOG(INFO) << "load " << fc.getFeatures().size() << " features from "
147147
<< features;
148148

149+
std::vector<cql2cpp::FeatureSourcePtr> fs_feature;
149150
std::map<cql2cpp::FeatureSourcePtr, const geos::io::GeoJSONFeature*>
150-
fs_feature;
151+
fs_to_geojson;
151152
for (const auto& feature : fc.getFeatures()) {
152153
cql2cpp::FeatureSourcePtr fp =
153154
std::make_shared<cql2cpp::FeatureSourceGeoJson>(feature);
154-
fs_feature[fp] = &feature;
155+
fs_feature.emplace_back(fp);
156+
fs_to_geojson[fp] = &feature;
155157
}
156158

157159
std::string query = filter_command.get<std::string>("query");
158160

159-
cql2cpp::Cql2Cpp<const geos::io::GeoJSONFeature*> cql2cpp;
161+
cql2cpp::Cql2Cpp cql2cpp;
160162
cql2cpp.set_feature_source(fs_feature);
161-
std::vector<const geos::io::GeoJSONFeature*> result;
163+
std::vector<cql2cpp::FeatureSourcePtr> result;
162164
if (cql2cpp.filter(query, &result)) {
163165
LOG(INFO) << result.size() << " features match the filter:";
164166
geos::io::GeoJSONWriter writer;
165-
for (const auto& feature : result) LOG(INFO) << writer.write(*feature);
167+
for (const auto& feature : result)
168+
LOG(INFO) << writer.write(*fs_to_geojson[feature]);
166169
} else {
167170
LOG(ERROR) << "filter error: " << cql2cpp.error_msg();
168171
}
@@ -172,7 +175,7 @@ int main(int argc, char** argv) {
172175
std::string query = sql_command.get<std::string>("query");
173176
std::string sql_where;
174177
std::string error_msg;
175-
if (cql2cpp::Cql2Cpp<void*>::ConvertToSQL(query, &sql_where, &error_msg)) {
178+
if (cql2cpp::Cql2Cpp::ConvertToSQL(query, &sql_where, &error_msg)) {
176179
LOG(INFO) << sql_where;
177180
} else {
178181
LOG(ERROR) << error_msg;
@@ -213,7 +216,7 @@ int main(int argc, char** argv) {
213216
cql2cpp::FeatureSourceGeoJson fs(fc.getFeatures().at(index));
214217
std::string query = eval_command.get<std::string>("query");
215218

216-
cql2cpp::Cql2Cpp<const geos::io::GeoJSONFeature*> cql2cpp;
219+
cql2cpp::Cql2Cpp cql2cpp;
217220

218221
std::string dot;
219222
bool eval_result;

test/test_parse.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ParseTest : public testing::Test {
5353

5454
std::string dot;
5555
cql2cpp::AstNode::set_ostream(&std::cout);
56-
bool ret = cql2cpp::Cql2Cpp<void*>::ToDot(query, &dot, nullptr);
56+
bool ret = cql2cpp::Cql2Cpp::ToDot(query, &dot, nullptr);
5757
if (ret and gen_dot_) {
5858
std::string dot_filename = case_name + ".dot";
5959
std::ofstream fout(dot_filename);

test/test_sql.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class SqlTest : public testing::Test {
102102
NULL, 0, &error_msg);
103103

104104
// Parse query to get queryables
105-
cql2cpp::Cql2Cpp<void *> cql2cpp;
105+
cql2cpp::Cql2Cpp cql2cpp;
106106
std::string error_str;
107107
auto root = cql2cpp.ParseAsAst(query, &error_str);
108108

0 commit comments

Comments
 (0)