From 8d1336d7df94c930bd63781d7c9fb6eff14dff23 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Mon, 13 Apr 2015 02:34:27 +0200 Subject: [PATCH] optimize --- include/mstch/mstch.hpp | 10 +++++--- src/mstch.cpp | 5 ++-- src/render_context.hpp | 42 +++++++++++++++---------------- src/state/in_inverted_section.hpp | 8 +++--- src/state/in_section.hpp | 8 +++--- src/token.cpp | 6 +---- src/token.hpp | 9 +++---- src/visitor/render_node.hpp | 5 ++-- src/visitor/render_section.hpp | 9 +++---- 9 files changed, 47 insertions(+), 55 deletions(-) diff --git a/include/mstch/mstch.hpp b/include/mstch/mstch.hpp index 0bd292a..b5b0d03 100644 --- a/include/mstch/mstch.hpp +++ b/include/mstch/mstch.hpp @@ -13,7 +13,7 @@ namespace mstch { // to a bool. class boolean { private: - bool state; + const bool state; public: boolean(bool b): state(b) {} operator bool() const { return state; } @@ -21,9 +21,11 @@ namespace mstch { using renderer = std::function; using string_lambda = std::function; - using renderer_lambda = std::function()>; + using renderer_lambda = std::function< + std::function()>; using node = boost::make_recursive_variant< - boost::blank, std::string, int, boolean, string_lambda, renderer_lambda, + boost::blank, std::string, int, boolean, + string_lambda, renderer_lambda, std::map, std::vector>::type; using object = std::map; @@ -31,7 +33,7 @@ namespace mstch { std::string render( const std::string& tmplt, - const object& context, + const object& root, const std::map& partials = std::map()); } diff --git a/src/mstch.cpp b/src/mstch.cpp index af9c84f..e0514b2 100644 --- a/src/mstch.cpp +++ b/src/mstch.cpp @@ -8,9 +8,8 @@ using namespace mstch; std::string mstch::render( const std::string& tmplt, - const object& root_object, + const object& root, const std::map& partials) { - return render_context(root_object, partials) - .render(strip_whitespace(tmplt)); + return render_context(root, partials).render(strip_whitespace(tmplt)); } diff --git a/src/render_context.hpp b/src/render_context.hpp index 0f37693..b05e809 100644 --- a/src/render_context.hpp +++ b/src/render_context.hpp @@ -11,6 +11,26 @@ namespace mstch { class render_context { + public: + class push { + public: + push(render_context& context, const mstch::object& obj = {}); + ~push(); + std::string render(const std::string& tmplt); + private: + render_context& context; + }; + render_context( + const mstch::object& object, + const std::map& partials); + const mstch::node& get_node(const std::string& token); + std::string render(const std::string& tmplt); + std::string render_partial(const std::string& partial_name); + template + void set_state(Args&&... args) { + state.top() = std::unique_ptr( + new T(std::forward(args)...)); + } private: static const mstch::node null_node; const mstch::node& find_node( @@ -24,29 +44,7 @@ namespace mstch { state.push(std::unique_ptr( new T(std::forward(args)...))); } - public: - class push { - private: - render_context& context; - public: - push(render_context& context, const mstch::object& obj = {}); - ~push(); - std::string render(const std::string& tmplt); - }; - render_context( - const mstch::object& object, - const std::map& partials); - const mstch::node& get_node(const std::string& token); - std::string render(const std::string& tmplt); - std::string render_partial(const std::string& partial_name); - template - void set_state(Args&&... args) { - state.top() = std::unique_ptr( - new T(std::forward(args)...)); - } }; - - } #endif //_MSTCH_RENDER_CONTEXT_H_ diff --git a/src/state/in_inverted_section.hpp b/src/state/in_inverted_section.hpp index 84054de..3eaa11c 100644 --- a/src/state/in_inverted_section.hpp +++ b/src/state/in_inverted_section.hpp @@ -7,14 +7,14 @@ namespace mstch { namespace state { class in_inverted_section: public render_state { - private: - const std::string section_name; - std::ostringstream section_text; - int skipped_openings; public: in_inverted_section(const std::string §ion_name); std::string render( render_context &context, const token &token) override; + private: + const std::string section_name; + std::ostringstream section_text; + int skipped_openings; }; } } diff --git a/src/state/in_section.hpp b/src/state/in_section.hpp index 1f17bd2..fa7d25f 100644 --- a/src/state/in_section.hpp +++ b/src/state/in_section.hpp @@ -7,14 +7,14 @@ namespace mstch { namespace state { class in_section: public render_state { - private: - const std::string section_name; - std::ostringstream section_text; - int skipped_openings; public: in_section(const std::string& section_name); std::string render( render_context& context, const token& token) override; + private: + const std::string section_name; + std::ostringstream section_text; + int skipped_openings; }; } } diff --git a/src/token.cpp b/src/token.cpp index 61818c3..e5b88fe 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -1,6 +1,4 @@ #include "token.hpp" - -#include "utils.hpp" #include #include @@ -13,12 +11,10 @@ std::tuple token::token_info(const std::string& inside) { case '/': return std::make_tuple(1, 0, type::section_close); case '&': return std::make_tuple(1, 0, type::unescaped_variable); case '#': return std::make_tuple(1, 0, type::section_open); + case '!': return std::make_tuple(1, 0, type::comment); case '{': if (inside.at(inside.size() - 1) == '}') return std::make_tuple(1, 1, type::unescaped_variable); - else - return std::make_tuple(0, 0, type::variable); - case '!': return std::make_tuple(1, 0, type::comment); default: return std::make_tuple(0, 0, type::variable); } } diff --git a/src/token.hpp b/src/token.hpp index de4721d..78e77f4 100644 --- a/src/token.hpp +++ b/src/token.hpp @@ -10,16 +10,15 @@ namespace mstch { text, variable, section_open, section_close, inverted_section_open, unescaped_variable, comment, partial }; + token(const std::string& raw_token); + type token_type() const; + std::string content() const; + std::string raw() const; private: type type_val; std::string content_val; std::string raw_val; std::tuple token_info(const std::string& inside); - public: - token(const std::string& raw_token); - type token_type() const; - std::string content() const; - std::string raw() const; }; } diff --git a/src/visitor/render_node.hpp b/src/visitor/render_node.hpp index e335979..57f5b0b 100644 --- a/src/visitor/render_node.hpp +++ b/src/visitor/render_node.hpp @@ -12,9 +12,6 @@ namespace mstch { class render_node: public boost::static_visitor { public: enum class flag { escape_html }; - private: - std::set flags; - public: render_node(std::set flags = {}); std::string operator()(const boost::blank& blank) const; std::string operator()(const int& i) const; @@ -24,6 +21,8 @@ namespace mstch { std::string operator()(const object& obj) const; std::string operator()(const string_lambda& lambda) const; std::string operator()(const renderer_lambda& lambda) const; + private: + std::set flags; }; } } diff --git a/src/visitor/render_section.hpp b/src/visitor/render_section.hpp index 99cce62..1c4125d 100644 --- a/src/visitor/render_section.hpp +++ b/src/visitor/render_section.hpp @@ -13,11 +13,6 @@ namespace mstch { class render_section: public boost::static_visitor { public: enum class flag { keep_array }; - private: - render_context& ctx; - std::string section; - std::set flags; - public: render_section( render_context& ctx, const std::string& section, @@ -30,6 +25,10 @@ namespace mstch { std::string operator()(const object& obj) const; std::string operator()(const string_lambda& lambda) const; std::string operator()(const renderer_lambda& lambda) const; + private: + render_context& ctx; + std::string section; + std::set flags; }; } }