From 8fa398252d9393817751e83c9a165af98d625834 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Wed, 29 Apr 2015 13:47:24 +0200 Subject: [PATCH] no need for template specialization --- src/visitor/get_token.hpp | 22 ++++++-------- src/visitor/has_token.hpp | 18 +++++------- src/visitor/is_node_empty.hpp | 54 +++++++++++++++------------------- src/visitor/render_node.hpp | 49 ++++++++++++++---------------- src/visitor/render_section.hpp | 42 +++++++++++++------------- 5 files changed, 83 insertions(+), 102 deletions(-) diff --git a/src/visitor/get_token.hpp b/src/visitor/get_token.hpp index 37d0c30..dd7ba70 100644 --- a/src/visitor/get_token.hpp +++ b/src/visitor/get_token.hpp @@ -15,25 +15,21 @@ class get_token: public boost::static_visitor { } template - inline const mstch::node& operator()(const T& t) const { + const mstch::node& operator()(const T& t) const { return node; } + const mstch::node& operator()(const map& map) const { + return map.at(token); + } + + const mstch::node& operator()(const std::shared_ptr& object) const { + return object->at(token); + } + private: const std::string& token; const mstch::node& node; }; -template<> -inline const mstch::node& get_token::operator()(const map& map) const { - return map.at(token); -} - -template<> -inline const mstch::node& get_token::operator()( - const std::shared_ptr& object) const -{ - return object->at(token); -} - } diff --git a/src/visitor/has_token.hpp b/src/visitor/has_token.hpp index 9702ce4..2efb627 100644 --- a/src/visitor/has_token.hpp +++ b/src/visitor/has_token.hpp @@ -16,18 +16,16 @@ class has_token: public boost::static_visitor { return token == "."; } + bool operator()(const map& map) const { + return map.count(token) == 1; + } + + bool operator()(const std::shared_ptr& object) const { + return object->has(token); + } + private: const std::string& token; }; -template<> -inline bool has_token::operator()(const map& map) const { - return map.count(token) == 1; -} - -template<> -inline bool has_token::operator()(const std::shared_ptr& object) const { - return object->has(token); -} - } diff --git a/src/visitor/is_node_empty.hpp b/src/visitor/is_node_empty.hpp index 43b0756..bfe3012 100644 --- a/src/visitor/is_node_empty.hpp +++ b/src/visitor/is_node_empty.hpp @@ -12,36 +12,30 @@ class is_node_empty: public boost::static_visitor { inline bool operator()(const T& t) const { return false; } + + bool operator()(const std::nullptr_t& nul) const { + return true; + } + + bool operator()(const int& value) const { + return value == 0; + } + + bool operator()(const double& value) const { + return value == 0; + } + + bool operator()(const bool& value) const { + return !value; + } + + bool operator()(const std::string& value) const { + return value == ""; + } + + bool operator()(const array& array) const { + return array.size() == 0; + } }; -template<> -inline bool is_node_empty::operator()(const std::nullptr_t& nul) const { - return true; -} - -template<> -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; -} - -template<> -inline bool is_node_empty::operator()(const std::string& value) const { - return value == ""; -} - -template<> -inline bool is_node_empty::operator()(const array& array) const { - return array.size() == 0; -} - } diff --git a/src/visitor/render_node.hpp b/src/visitor/render_node.hpp index bc494c3..0980efd 100644 --- a/src/visitor/render_node.hpp +++ b/src/visitor/render_node.hpp @@ -19,35 +19,30 @@ class render_node: public boost::static_visitor { return ""; } + std::string operator()(const int& value) const { + return std::to_string(value); + } + + std::string operator()(const double& value) const { + std::stringstream ss; + ss << value; + return ss.str(); + } + + std::string operator()(const bool& value) const { + return value ? "true" : "false"; + } + + std::string operator()(const lambda& value) const { + return (m_flag == flag::escape_html) ? html_escape(value()) : value(); + } + + std::string operator()(const std::string& value) const { + return (m_flag == flag::escape_html) ? html_escape(value) : value; + } + private: flag m_flag; }; -template<> -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"; -} - -template<> -inline std::string render_node::operator()(const lambda& value) const { - return (m_flag == flag::escape_html) ? html_escape(value()) : value(); -} - -template<> -inline std::string render_node::operator()(const std::string& value) const { - return (m_flag == flag::escape_html) ? html_escape(value) : value; -} - } diff --git a/src/visitor/render_section.hpp b/src/visitor/render_section.hpp index bcad55d..d28b7b1 100644 --- a/src/visitor/render_section.hpp +++ b/src/visitor/render_section.hpp @@ -24,32 +24,30 @@ class render_section: public boost::static_visitor { return render_context::push(ctx, t).render(section); } + std::string operator()(const lambda& fun) const { + std::string section_str; + for(auto& token: section) + section_str += token.raw(); + + return fun(section_str, [this](const std::string& str) { + return render_context::push(ctx).render(template_type{str}); + }); + } + + std::string operator()(const array& array) const { + std::string out; + if(m_flag == flag::keep_array) + return render_context::push(ctx, array).render(section); + else + for (auto& item: array) + out += visit(render_section(ctx, section, flag::keep_array), item); + return out; + } + private: render_context& ctx; const template_type& section; flag m_flag; }; -template<> -inline std::string render_section::operator()(const lambda& fun) const { - std::string section_str; - for(auto& token: section) - section_str += token.raw(); - - return fun(section_str, [this](const std::string& str) { - return render_context::push(ctx).render(template_type{str}); - }); -} - -template<> -inline std::string render_section::operator()(const array& array) const { - std::string out; - if(m_flag == flag::keep_array) - return render_context::push(ctx, array).render(section); - else - for (auto& item: array) - out += visit(render_section(ctx, section, flag::keep_array), item); - return out; -} - }