diff --git a/include/mapbox/feature.hpp b/include/mapbox/feature.hpp index 1c93138..decb22a 100644 --- a/include/mapbox/feature.hpp +++ b/include/mapbox/feature.hpp @@ -33,9 +33,37 @@ using value_base = mapbox::util::variant>, mapbox::util::recursive_wrapper>>; -struct value : value_base +struct value : public value_base { - using value_base::value_base; + using array_type = std::vector; + using object_type = std::unordered_map; + + 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 ::value, int> = 0, + typename std::enable_if_t::value, int> = 0> + value(T t) : value_base(int64_t(t)) + { + } + + template ::value, int> = 0, + typename std::enable_if_t::value, int> = 0> + value(T t) : value_base(uint64_t(t)) + { + } + + template ::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(); } }; using property_map = std::unordered_map; diff --git a/test/feature.cpp b/test/feature.cpp index d9d2d5a..2908126 100644 --- a/test/feature.cpp +++ b/test/feature.cpp @@ -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 +void checkType(U&& arg) try +{ + value v{std::forward(arg)}; + CHECK(v); + CHECK(v.template is()); + CHECK(v.template get() == arg); +} +catch (...) +{ + FAIL(); +} + +} // namespace + +TEST_CASE("test value") +{ + CHECK(!value()); + checkType(32); + checkType(32u); + checkType(false); + checkType("hello"); + + value intV{32}; + CHECK_THROWS(intV.get()); +} TEST_CASE("test feature") {