From 105e708d4229144e0bc9331c149fc2bc260e0f13 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Wed, 29 Apr 2015 10:53:01 +0200 Subject: [PATCH] double value support --- include/mstch/mstch.hpp | 2 +- src/visitor/is_node_empty.hpp | 5 +++++ src/visitor/render_node.hpp | 8 ++++++++ test/data/dot_notation.hpp | 2 +- test/data/partial_view.hpp | 3 ++- test/data/simple.hpp | 2 +- 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/mstch/mstch.hpp b/include/mstch/mstch.hpp index c35b5ac..f9b42d4 100644 --- a/include/mstch/mstch.hpp +++ b/include/mstch/mstch.hpp @@ -84,7 +84,7 @@ class lambda { }; using node = boost::make_recursive_variant< - std::nullptr_t, std::string, int, bool, lambda, + std::nullptr_t, std::string, int, double, bool, lambda, std::shared_ptr>, std::map, std::vector>::type; diff --git a/src/visitor/is_node_empty.hpp b/src/visitor/is_node_empty.hpp index 25b3c69..43b0756 100644 --- a/src/visitor/is_node_empty.hpp +++ b/src/visitor/is_node_empty.hpp @@ -24,6 +24,11 @@ inline bool is_node_empty::operator()(const int& value) const { return value == 0; } +template<> +inline bool is_node_empty::operator()(const double& value) const { + return value == 0; +} + template<> inline bool is_node_empty::operator()(const bool& value) const { return !value; diff --git a/src/visitor/render_node.hpp b/src/visitor/render_node.hpp index aefd22f..bc494c3 100644 --- a/src/visitor/render_node.hpp +++ b/src/visitor/render_node.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include "mstch/mstch.hpp" @@ -27,6 +28,13 @@ inline std::string render_node::operator()(const int& value) const { return std::to_string(value); } +template<> +inline std::string render_node::operator()(const double& value) const { + std::stringstream ss; + ss << value; + return ss.str(); +} + template<> inline std::string render_node::operator()(const bool& value) const { return value?"true":"false"; diff --git a/test/data/dot_notation.hpp b/test/data/dot_notation.hpp index d21ee50..9a6574d 100644 --- a/test/data/dot_notation.hpp +++ b/test/data/dot_notation.hpp @@ -17,7 +17,7 @@ public: } mstch::node vat() { - return static_cast(m_value * 0.2); + return m_value * 0.2; } mstch::node currency() { diff --git a/test/data/partial_view.hpp b/test/data/partial_view.hpp index b6341ef..09869a8 100644 --- a/test/data/partial_view.hpp +++ b/test/data/partial_view.hpp @@ -1,6 +1,7 @@ class partial_view: public mstch::object { private: int m_value; + public: partial_view(): m_value{10000} { register_methods(this, { @@ -30,7 +31,7 @@ public: } mstch::node taxed_value() { - return static_cast(m_value - (m_value * 0.4)); + return m_value - (m_value * 0.4); } mstch::node in_ca() { diff --git a/test/data/simple.hpp b/test/data/simple.hpp index e7e6088..a2a64c3 100644 --- a/test/data/simple.hpp +++ b/test/data/simple.hpp @@ -21,7 +21,7 @@ public: } mstch::node taxed_value() { - return static_cast(m_value - (m_value * 0.4)); + return m_value - (m_value * 0.4); } mstch::node in_ca() {