]> Creatis software - cpPlugins.git/blob - lib/mstch/visitor/render_section.hpp
Moved to version 1.0
[cpPlugins.git] / lib / mstch / visitor / render_section.hpp
1 #pragma once
2
3 #include <boost/variant/static_visitor.hpp>
4
5 #include "render_context.hpp"
6 #include "mstch/mstch.hpp"
7 #include "utils.hpp"
8 #include "render_node.hpp"
9
10 namespace mstch {
11
12 class render_section: public boost::static_visitor<std::string> {
13  public:
14   enum class flag { none, keep_array };
15   render_section(
16       render_context& ctx,
17       const template_type& section,
18       const delim_type& delims,
19       flag p_flag = flag::none):
20       m_ctx(ctx), m_section(section), m_delims(delims), m_flag(p_flag)
21   {
22   }
23
24   template<class T>
25   std::string operator()(const T& t) const {
26     return render_context::push(m_ctx, t).render(m_section);
27   }
28
29   std::string operator()(const lambda& fun) const {
30     std::string section_str;
31     for (auto& token: m_section)
32       section_str += token.raw();
33     template_type interpreted{fun([this](const mstch::node& n) {
34       return visit(render_node(m_ctx), n);
35     }, section_str), m_delims};
36     return render_context::push(m_ctx).render(interpreted);
37   }
38
39   std::string operator()(const array& array) const {
40     std::string out;
41     if (m_flag == flag::keep_array)
42       return render_context::push(m_ctx, array).render(m_section);
43     else
44       for (auto& item: array)
45         out += visit(render_section(
46             m_ctx, m_section, m_delims, flag::keep_array), item);
47     return out;
48   }
49
50  private:
51   render_context& m_ctx;
52   const template_type& m_section;
53   const delim_type& m_delims;
54   flag m_flag;
55 };
56
57 }