From 21a27f404778681e528cb7a2cf316073c7f7f94e Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Tue, 21 Apr 2015 16:54:54 +0200 Subject: [PATCH] refactor --- src/CMakeLists.txt | 6 ---- src/render_context.cpp | 6 ++-- src/render_context.hpp | 1 - src/state/in_inverted_section.cpp | 43 ---------------------------- src/state/in_inverted_section.hpp | 21 -------------- src/state/in_section.cpp | 35 +++++++++++------------ src/state/in_section.hpp | 4 ++- src/state/outside_section.cpp | 15 +++++----- src/visitor/get_token.cpp | 38 ------------------------- src/visitor/get_token.hpp | 29 +++++++++++++------ src/visitor/has_token.cpp | 35 ----------------------- src/visitor/has_token.hpp | 27 ++++++++++++------ src/visitor/is_node_empty.cpp | 32 --------------------- src/visitor/is_node_empty.hpp | 40 +++++++++++++++++++++----- src/visitor/render_node.cpp | 36 ----------------------- src/visitor/render_node.hpp | 35 +++++++++++++++++------ src/visitor/render_section.cpp | 47 ------------------------------- src/visitor/render_section.hpp | 29 +++++++++++++------ 18 files changed, 149 insertions(+), 330 deletions(-) delete mode 100644 src/state/in_inverted_section.cpp delete mode 100644 src/state/in_inverted_section.hpp delete mode 100644 src/visitor/get_token.cpp delete mode 100644 src/visitor/has_token.cpp delete mode 100644 src/visitor/is_node_empty.cpp delete mode 100644 src/visitor/render_node.cpp delete mode 100644 src/visitor/render_section.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 020409a..8850707 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,14 +6,8 @@ include_directories( ${Boost_INCLUDE_DIR}) set(SRC - state/in_inverted_section.cpp state/in_section.cpp state/outside_section.cpp - visitor/get_token.cpp - visitor/has_token.cpp - visitor/is_node_empty.cpp - visitor/render_node.cpp - visitor/render_section.cpp mstch.cpp render_context.cpp template_type.cpp diff --git a/src/render_context.cpp b/src/render_context.cpp index 838c8bc..ba46b73 100644 --- a/src/render_context.cpp +++ b/src/render_context.cpp @@ -44,9 +44,9 @@ const mstch::node& render_context::find_node( token.substr(token.rfind('.') + 1), {find_node(token.substr(0, token.rfind('.')), current_nodes)}); else - for (auto& n: current_nodes) - if (boost::apply_visitor(visitor::has_token(token), n)) - return boost::apply_visitor(visitor::get_token(token, n), n); + for (auto& node: current_nodes) + if (boost::apply_visitor(visitor::has_token(token), node)) + return boost::apply_visitor(visitor::get_token(token, node), node); return null_node; } diff --git a/src/render_context.hpp b/src/render_context.hpp index 720c313..4dbd9be 100644 --- a/src/render_context.hpp +++ b/src/render_context.hpp @@ -20,7 +20,6 @@ namespace mstch { private: render_context& context; }; - render_context( const mstch::node& node, const std::map& partials); diff --git a/src/state/in_inverted_section.cpp b/src/state/in_inverted_section.cpp deleted file mode 100644 index b10b376..0000000 --- a/src/state/in_inverted_section.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "in_inverted_section.hpp" -#include "outside_section.hpp" -#include "visitor/render_section.hpp" -#include "visitor/is_node_empty.hpp" - -using namespace mstch; - -state::in_inverted_section::in_inverted_section( - const std::string& section_name): - section_name(section_name), skipped_openings(0) -{ -} - -std::string state::in_inverted_section::render( - render_context& ctx, const token& token) -{ - switch(token.token_type()) { - case token::type::section_close: - if(token.content() == section_name && skipped_openings == 0) { - std::string out; - auto& section_node = ctx.get_node(section_name); - if(boost::apply_visitor(visitor::is_node_empty(), section_node)) - out = render_context::push(ctx).render(section); - ctx.set_state(); - return out; - } else { - skipped_openings--; - section << token; - } - break; - case token::type::inverted_section_open: - case token::type::section_open: - skipped_openings++; - case token::type::text: - case token::type::variable: - case token::type::unescaped_variable: - case token::type::comment: - case token::type::partial: - section << token; - break; - }; - return ""; -} diff --git a/src/state/in_inverted_section.hpp b/src/state/in_inverted_section.hpp deleted file mode 100644 index dafd41b..0000000 --- a/src/state/in_inverted_section.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include "render_state.hpp" -#include -#include "template_type.hpp" - -namespace mstch { - namespace state { - class in_inverted_section: public render_state { - public: - in_inverted_section(const std::string& section_name); - std::string render( - render_context& context, const token& token) override; - private: - const std::string section_name; - template_type section; - int skipped_openings; - }; - } -} diff --git a/src/state/in_section.cpp b/src/state/in_section.cpp index b3eb77e..db0cb89 100644 --- a/src/state/in_section.cpp +++ b/src/state/in_section.cpp @@ -5,37 +5,34 @@ using namespace mstch; -state::in_section::in_section(const std::string& section_name): - section_name(section_name), skipped_openings(0) +state::in_section::in_section(type type, const std::string& section_name): + m_type(type), section_name(section_name), skipped_openings(0) { } std::string state::in_section::render(render_context& ctx, const token& token) { - switch(token.token_type()) { - case token::type::section_close: + if(token.token_type() == token::type::section_close) { if(token.content() == section_name && skipped_openings == 0) { - auto& section_node = ctx.get_node(section_name); + auto& node = ctx.get_node(section_name); std::string out; - if (!boost::apply_visitor(visitor::is_node_empty(), section_node)) - out = boost::apply_visitor( - visitor::render_section(ctx, section), section_node); + if(m_type == type::normal) { + if (!boost::apply_visitor(visitor::is_node_empty(), node)) + out = boost::apply_visitor( + visitor::render_section(ctx, section), node); + } else if(m_type == type::inverted) { + if (boost::apply_visitor(visitor::is_node_empty(), node)) + out = render_context::push(ctx).render(section); + } ctx.set_state(); return out; } else { skipped_openings--; - section << token; } - break; - case token::type::inverted_section_open: - case token::type::section_open: + } else if(token.token_type() == token::type::inverted_section_open || + token.token_type() == token::type::section_open) + { skipped_openings++; - case token::type::text: - case token::type::variable: - case token::type::unescaped_variable: - case token::type::comment: - case token::type::partial: - section << token; - break; } + section << token; return ""; } diff --git a/src/state/in_section.hpp b/src/state/in_section.hpp index 54f2400..653d283 100644 --- a/src/state/in_section.hpp +++ b/src/state/in_section.hpp @@ -9,10 +9,12 @@ namespace mstch { namespace state { class in_section: public render_state { public: - in_section(const std::string& section_name); + enum class type { inverted, normal }; + in_section(type type, const std::string& section_name); std::string render( render_context& context, const token& token) override; private: + const type m_type; const std::string section_name; template_type section; int skipped_openings; diff --git a/src/state/outside_section.cpp b/src/state/outside_section.cpp index f792c84..d209527 100644 --- a/src/state/outside_section.cpp +++ b/src/state/outside_section.cpp @@ -1,34 +1,33 @@ #include "visitor/render_node.hpp" #include "outside_section.hpp" #include "in_section.hpp" -#include "in_inverted_section.hpp" #include "render_context.hpp" using namespace mstch; -using flag = visitor::render_node::flag; std::string state::outside_section::render( render_context& ctx, const token& token) { switch(token.token_type()) { case token::type::section_open: - ctx.set_state(token.content()); + ctx.set_state(in_section::type::normal, token.content()); break; case token::type::inverted_section_open: - ctx.set_state(token.content()); + ctx.set_state(in_section::type::inverted, token.content()); break; case token::type::variable: - return boost::apply_visitor(visitor::render_node(flag::escape_html), + return boost::apply_visitor( + visitor::render_node(visitor::render_node::flag::escape_html), ctx.get_node(token.content())); case token::type::unescaped_variable: - return boost::apply_visitor(visitor::render_node(flag::none), + return boost::apply_visitor( + visitor::render_node(visitor::render_node::flag::none), ctx.get_node(token.content())); - case token::type::comment: break; case token::type::text: return token.content(); case token::type::partial: return ctx.render_partial(token.content()); - case token::type::section_close: break; + default: break; } return ""; } diff --git a/src/visitor/get_token.cpp b/src/visitor/get_token.cpp deleted file mode 100644 index efc44d6..0000000 --- a/src/visitor/get_token.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "get_token.hpp" - -using namespace mstch; -using namespace mstch::visitor; - -get_token::get_token(const std::string& token, const mstch::node& node): - token(token), node(node) -{ -} - -const mstch::node& get_token::operator()(const boost::blank& blank) const { - return node; -} - -const mstch::node& get_token::operator()(const int& i) const { - return node; -} - -const mstch::node& get_token::operator()(const bool& b) const { - return node; -} - -const mstch::node& get_token::operator()(const std::string& str) const { - return node; -} - -const mstch::node& get_token::operator()(const array& arr) const { - return node; -} - -const mstch::node& get_token::operator()(const map& map) const { - return map.at(token); -} - -const mstch::node& get_token::operator()(const std::shared_ptr& obj) const { - return obj->at(token); -} - diff --git a/src/visitor/get_token.hpp b/src/visitor/get_token.hpp index b1b0a8a..5e0654d 100644 --- a/src/visitor/get_token.hpp +++ b/src/visitor/get_token.hpp @@ -8,17 +8,30 @@ namespace mstch { namespace visitor { class get_token: public boost::static_visitor { public: - get_token(const std::string& token, const mstch::node& node); - const mstch::node& operator()(const boost::blank& blank) const; - const mstch::node& operator()(const int& i) const; - const mstch::node& operator()(const bool& b) const; - const mstch::node& operator()(const std::string& str) const; - const mstch::node& operator()(const array& arr) const; - const mstch::node& operator()(const map& map) const; - const mstch::node& operator()(const std::shared_ptr& obj) const; + get_token(const std::string& token, const mstch::node& node): + token(token), node(node) + { + } + + template inline + const mstch::node& operator()(const T& t) const { + return node; + } private: const std::string& token; const mstch::node& node; }; + + template<> inline + const mstch::node& get_token::operator()(const map& m) const { + return m.at(token); + } + + template<> inline + const mstch::node& get_token::operator()>( + const std::shared_ptr& obj) const + { + return obj->at(token); + } } } diff --git a/src/visitor/has_token.cpp b/src/visitor/has_token.cpp deleted file mode 100644 index 5ff4f3f..0000000 --- a/src/visitor/has_token.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "has_token.hpp" - -using namespace mstch; -using namespace mstch::visitor; - -has_token::has_token(const std::string& token): token(token) { -} - -bool has_token::operator()(const boost::blank& blank) const { - return token == "."; -} - -bool has_token::operator()(const int& i) const { - return token == "."; -} - -bool has_token::operator()(const bool& b) const { - return token == "."; -} - -bool has_token::operator()(const std::string& str) const { - return token == "."; -} - -bool has_token::operator()(const array& arr) const { - return token == "."; -} - -bool has_token::operator()(const map& map) const { - return map.count(token) == 1; -} - -bool has_token::operator()(const std::shared_ptr& obj) const { - return obj->has(token); -} diff --git a/src/visitor/has_token.hpp b/src/visitor/has_token.hpp index fd333b3..d504058 100644 --- a/src/visitor/has_token.hpp +++ b/src/visitor/has_token.hpp @@ -8,16 +8,27 @@ namespace mstch { namespace visitor { class has_token: public boost::static_visitor { public: - has_token(const std::string& token); - bool operator()(const boost::blank& blank) const; - bool operator()(const int& i) const; - bool operator()(const bool& b) const; - bool operator()(const std::string& str) const; - bool operator()(const array& arr) const; - bool operator()(const map& map) const; - bool operator()(const std::shared_ptr& obj) const; + has_token(const std::string& token): token(token) { + } + + template inline + bool operator()(const T& t) const { + return token == "."; + } private: const std::string& token; }; + + template<> inline + bool has_token::operator()(const map& m) const { + return m.count(token) == 1; + } + + template<> inline + bool has_token::operator()>( + const std::shared_ptr& obj) const + { + return obj->has(token); + } } } diff --git a/src/visitor/is_node_empty.cpp b/src/visitor/is_node_empty.cpp deleted file mode 100644 index b5c2422..0000000 --- a/src/visitor/is_node_empty.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "is_node_empty.hpp" - -using namespace mstch; -using namespace mstch::visitor; - -bool is_node_empty::operator()(const boost::blank& blank) const { - return true; -} - -bool is_node_empty::operator()(const int& i) const { - return i == 0; -} - -bool is_node_empty::operator()(const bool& b) const { - return !b; -} - -bool is_node_empty::operator()(const std::string& str) const { - return str == ""; -} - -bool is_node_empty::operator()(const array& arr) const { - return arr.size() == 0; -} - -bool is_node_empty::operator()(const map& map) const { - return false; -} - -bool is_node_empty::operator()(const std::shared_ptr& obj) const { - return false; -} diff --git a/src/visitor/is_node_empty.hpp b/src/visitor/is_node_empty.hpp index 5f199e3..a303218 100644 --- a/src/visitor/is_node_empty.hpp +++ b/src/visitor/is_node_empty.hpp @@ -8,13 +8,39 @@ namespace mstch { namespace visitor { class is_node_empty: public boost::static_visitor { public: - bool operator()(const boost::blank& blank) const; - bool operator()(const int& i) const; - bool operator()(const bool& b) const; - bool operator()(const std::string& str) const; - bool operator()(const array& arr) const; - bool operator()(const map& map) const; - bool operator()(const std::shared_ptr& obj) const; + template inline + bool operator()(const T& t) const { + return false; + } }; + + template<> inline + bool is_node_empty::operator()( + const boost::blank& blank) const + { + return true; + } + + template<> inline + bool is_node_empty::operator()(const int& i) const { + return i == 0; + } + + template<> inline + bool is_node_empty::operator()(const bool& b) const { + return !b; + } + + template<> inline + bool is_node_empty::operator()( + const std::string& str) const + { + return str == ""; + } + + template<> inline + bool is_node_empty::operator()(const array& arr) const { + return arr.size() == 0; + } } } diff --git a/src/visitor/render_node.cpp b/src/visitor/render_node.cpp deleted file mode 100644 index 791dbd9..0000000 --- a/src/visitor/render_node.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "utils.hpp" -#include "render_node.hpp" - -using namespace mstch; -using namespace mstch::visitor; - -render_node::render_node(flag p_flag): m_flag(p_flag) { -} - -std::string render_node::operator()(const boost::blank& blank) const { - return ""; -} - -std::string render_node::operator()(const int& i) const { - return std::to_string(i); -} - -std::string render_node::operator()(const bool& b) const { - return b?"true":"false"; -} - -std::string render_node::operator()(const std::string& str) const { - return (m_flag == flag::escape_html)?html_escape(str):str; -} - -std::string render_node::operator()(const array& arr) const { - return ""; -} - -std::string render_node::operator()(const map& map) const { - return ""; -} - -std::string render_node::operator()(const std::shared_ptr& obj) const { - return ""; -} diff --git a/src/visitor/render_node.hpp b/src/visitor/render_node.hpp index b3b24c2..34ddc7e 100644 --- a/src/visitor/render_node.hpp +++ b/src/visitor/render_node.hpp @@ -7,21 +7,38 @@ namespace mstch { namespace visitor { - class render_node : public boost::static_visitor { + class render_node: public boost::static_visitor { public: enum class flag { none, escape_html }; - render_node(flag p_flag = flag::none); - std::string operator()(const boost::blank& blank) const; - std::string operator()(const int& i) const; - std::string operator()(const bool& b) const; - std::string operator()(const std::string& str) const; - std::string operator()(const array& arr) const; - std::string operator()(const map& map) const; - std::string operator()(const std::shared_ptr& obj) const; + + render_node(flag p_flag = flag::none): m_flag(p_flag) { + } + + template inline + std::string operator()(const T& t) const { + return ""; + } private: flag m_flag; }; + + template<> inline + std::string render_node::operator()(const int& i) const { + return std::to_string(i); + } + + template<> inline + std::string render_node::operator()(const bool& b) const { + return b?"true":"false"; + } + + template<> inline + std::string render_node::operator()( + const std::string& str) const + { + return (m_flag == flag::escape_html)?html_escape(str):str; + } } } diff --git a/src/visitor/render_section.cpp b/src/visitor/render_section.cpp deleted file mode 100644 index 6616a83..0000000 --- a/src/visitor/render_section.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "render_section.hpp" - -using namespace mstch; -using namespace mstch::visitor; - -render_section::render_section( - render_context& ctx, const template_type& section, flag p_flag): - ctx(ctx), section(section), m_flag(p_flag) -{ -} - -std::string render_section::operator()( - const boost::blank& blank) const -{ - return render_context::push(ctx, mstch::node{}).render(section); -} - -std::string render_section::operator()(const int& i) const { - return render_context::push(ctx, i).render(section); -} - -std::string render_section::operator()(const bool& b) const { - return render_context::push(ctx, b).render(section); -} - -std::string render_section::operator()(const std::string& str) const { - return render_context::push(ctx, str).render(section); -} - -std::string render_section::operator()(const map& map) const { - return render_context::push(ctx, map).render(section); -} - -std::string render_section::operator()(const array& a) const { - std::string out; - if(m_flag == flag::keep_array) - out += render_context::push(ctx, a).render(section); - else - for (auto& item: a) - out += boost::apply_visitor( - render_section(ctx, section, flag::keep_array), item); - return out; -} - -std::string render_section::operator()(const std::shared_ptr& obj) const { - return render_context::push(ctx, obj).render(section); -} diff --git a/src/visitor/render_section.hpp b/src/visitor/render_section.hpp index cacb284..6c4bc0b 100644 --- a/src/visitor/render_section.hpp +++ b/src/visitor/render_section.hpp @@ -13,18 +13,31 @@ namespace mstch { render_section( render_context& ctx, const template_type& section, - flag p_flag = flag::none); - std::string operator()(const boost::blank& blank) const; - std::string operator()(const int& i) const; - std::string operator()(const bool& b) const; - std::string operator()(const std::string& str) const; - std::string operator()(const array& arr) const; - std::string operator()(const map& map) const; - std::string operator()(const std::shared_ptr& obj) const; + flag p_flag = flag::none): + ctx(ctx), section(section), m_flag(p_flag) + { + } + + template inline + std::string operator()(const T& t) const { + return render_context::push(ctx, t).render(section); + } private: render_context& ctx; const template_type& section; flag m_flag; }; + + template<> inline + std::string render_section::operator()(const array& arr) const { + std::string out; + if(m_flag == flag::keep_array) + out += render_context::push(ctx, arr).render(section); + else + for (auto& i: arr) + out += boost::apply_visitor( + render_section(ctx, section, flag::keep_array), i); + return out; + } } }