Skip to content

Commit 1a7fc3e

Browse files
committed
Add versions of params_t::set() function and add some tests
It seems superfluous to have all these implementations of the set() function, but there are some problems with MSVC 2019, and this is the workaround. See https://developercommunity.visualstudio.com/t/10242620
1 parent 0e7f92a commit 1a7fc3e

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

src/params.hpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,43 @@ std::string to_string(param_value_t const &value);
3333
class params_t
3434
{
3535
public:
36-
template <typename K, typename V>
36+
// It seems superfluous to have all these implementations of the set()
37+
// function, but there are some problems with MSVC 2019, and this is the
38+
// workaround. See https://developercommunity.visualstudio.com/t/10242620
39+
template <typename K>
40+
void set(K &&key, bool value)
41+
{
42+
m_map.insert_or_assign(std::forward<K>(key), value);
43+
}
44+
45+
template <typename K>
46+
void set(K &&key, double value)
47+
{
48+
m_map.insert_or_assign(std::forward<K>(key), value);
49+
}
50+
51+
template <typename K>
52+
void set(K &&key, float value)
53+
{
54+
m_map.insert_or_assign(std::forward<K>(key), value);
55+
}
56+
57+
template <typename K, typename V,
58+
std::enable_if_t<std::is_integral_v<std::remove_reference_t<V>>,
59+
bool> = true>
60+
void set(K &&key, V value)
61+
{
62+
m_map.insert_or_assign(std::forward<K>(key),
63+
static_cast<int64_t>(value));
64+
}
65+
66+
template <
67+
typename K, typename V,
68+
std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<V>>,
69+
bool> = true>
3770
void set(K &&key, V &&value)
3871
{
39-
m_map.insert_or_assign(std::forward<K>(key), std::forward<V>(value));
72+
m_map.insert_or_assign(std::forward<K>(key), std::string(value));
4073
}
4174

4275
template <typename K>

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ set_test(test-output-pgsql-style-file)
8686
set_test(test-output-pgsql-tablespace LABELS Tablespace)
8787
set_test(test-output-pgsql-validgeom)
8888
set_test(test-output-pgsql-z_order)
89+
set_test(test-params LABELS NoDB)
8990
set_test(test-persistent-cache LABELS NoDB)
9091
set_test(test-pgsql)
9192
set_test(test-pgsql-capabilities)

tests/test-params.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "params.hpp"
13+
14+
TEST_CASE("Set param value", "[NoDB]")
15+
{
16+
param_value_t const p_null{null_param_t()};
17+
param_value_t const p_str{std::string{"foo"}};
18+
param_value_t const p_int{static_cast<int64_t>(26)};
19+
param_value_t const p_double{3.141};
20+
param_value_t const p_true{true};
21+
param_value_t const p_false{false};
22+
23+
REQUIRE(to_string(p_null).empty());
24+
REQUIRE(to_string(p_str) == "foo");
25+
REQUIRE(to_string(p_int) == "26");
26+
REQUIRE(to_string(p_double) == "3.141");
27+
REQUIRE(to_string(p_true) == "true");
28+
REQUIRE(to_string(p_false) == "false");
29+
}
30+
31+
TEST_CASE("Params with different value types", "[NoDB]")
32+
{
33+
params_t params;
34+
REQUIRE_FALSE(params.has("foo"));
35+
36+
params.set("foo", 99);
37+
REQUIRE(params.has("foo"));
38+
REQUIRE(params.get("foo") == param_value_t(static_cast<int64_t>(99)));
39+
REQUIRE(params.get_int64("foo") == 99);
40+
41+
params.set("foo", "astring");
42+
REQUIRE(params.has("foo"));
43+
REQUIRE(params.get("foo") == param_value_t(std::string("astring")));
44+
REQUIRE(params.get_string("foo") == "astring");
45+
REQUIRE_THROWS(params.get_int64("foo"));
46+
}
47+
48+
TEST_CASE("Set params with explicit type", "[NoDB]")
49+
{
50+
params_t params;
51+
52+
params.set("isstring", "hi");
53+
params.set("isint", 567);
54+
params.set("isdouble", 567.0);
55+
params.set("istrue", true);
56+
params.set("isfalse", false);
57+
58+
REQUIRE(params.get_string("isstring") == "hi");
59+
REQUIRE(params.get_int64("isint") == 567);
60+
REQUIRE(params.get_double("isdouble") == Approx(567.0));
61+
REQUIRE(params.get_bool("istrue"));
62+
REQUIRE_FALSE(params.get_bool("isfalse"));
63+
64+
REQUIRE_THROWS(params.get("does not exist"));
65+
}

0 commit comments

Comments
 (0)