summaryrefslogtreecommitdiff
path: root/include/mbgl/style/value.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/style/value.hpp')
-rw-r--r--include/mbgl/style/value.hpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/include/mbgl/style/value.hpp b/include/mbgl/style/value.hpp
new file mode 100644
index 0000000000..5e6260e5a6
--- /dev/null
+++ b/include/mbgl/style/value.hpp
@@ -0,0 +1,43 @@
+#ifndef MBGL_STYLE_VALUE
+#define MBGL_STYLE_VALUE
+
+#include <mbgl/util/variant.hpp>
+#include <mbgl/util/pbf.hpp>
+
+#include <cstdlib>
+#include <cerrno>
+
+namespace mbgl {
+
+typedef util::variant<bool, int64_t, uint64_t, double, std::string> Value;
+
+std::string toString(const Value &value);
+
+Value parseValue(pbf data);
+
+namespace util {
+inline bool parseNumericString(const std::string &str, double &result) {
+ char *end = nullptr;
+ const char *begin = str.c_str();
+ result = std::strtod(begin, &end);
+ while (*end != '\0' && isspace(*end)) end++; // eat whitespace after the end
+ return errno == 0 && end - begin == long(str.size());
+}
+}
+
+template <typename T>
+T toNumber(const Value &value) {
+ if (value.is<std::string>()) {
+ double val;
+ return util::parseNumericString(value.get<std::string>(), val) ? val : 0;
+ }
+ else if (value.is<bool>()) return value.get<bool>();
+ else if (value.is<int64_t>()) return value.get<int64_t>();
+ else if (value.is<uint64_t>()) return value.get<uint64_t>();
+ else if (value.is<double>()) return value.get<double>();
+ else return 0;
+}
+
+}
+
+#endif