Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions include/mapbox/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,37 @@ using value_base = mapbox::util::variant<null_value_t, bool, uint64_t, int64_t,
mapbox::util::recursive_wrapper<std::vector<value>>,
mapbox::util::recursive_wrapper<std::unordered_map<std::string, value>>>;

struct value : value_base
struct value : public value_base
{
using value_base::value_base;
using array_type = std::vector<value>;
using object_type = std::unordered_map<std::string, value>;

value() : value_base(null_value) {}
value(null_value_t) : value_base(null_value) {}
value(bool v) : value_base(v) {}
value(const char* c) : value_base(std::string(c)) {}
value(std::string str) : value_base(std::move(str)) {}

template <typename T, typename std::enable_if_t<std::is_integral<T>::value, int> = 0,
typename std::enable_if_t<std::is_signed<T>::value, int> = 0>
value(T t) : value_base(int64_t(t))
{
}

template <typename T, typename std::enable_if_t<std::is_integral<T>::value, int> = 0,
typename std::enable_if_t<!std::is_signed<T>::value, int> = 0>
value(T t) : value_base(uint64_t(t))
{
}

template <typename T, typename std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
value(T t) : value_base(double(t))
{
}
value(array_type array) : value_base(std::move(array)) {}
value(object_type object) : value_base(std::move(object)) {}

explicit operator bool() const { return !is<null_value_t>(); }
};

using property_map = std::unordered_map<std::string, value>;
Expand Down
30 changes: 30 additions & 0 deletions test/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ using mapbox::feature::feature;
using mapbox::feature::feature_collection;
using mapbox::feature::null_value;
using mapbox::feature::null_value_t;
using mapbox::feature::value;

namespace {

template <typename T, typename U>
void checkType(U&& arg) try
{
value v{std::forward<U>(arg)};
CHECK(v);
CHECK(v.template is<T>());
CHECK(v.template get<T>() == arg);
}
catch (...)
{
FAIL();
}

} // namespace

TEST_CASE("test value")
{
CHECK(!value());
checkType<int64_t>(32);
checkType<uint64_t>(32u);
checkType<bool>(false);
checkType<std::string>("hello");

value intV{32};
CHECK_THROWS(intV.get<uint64_t>());
}

TEST_CASE("test feature")
{
Expand Down