]> Creatis software - cpPlugins.git/blob - lib/mstch/render_context.hpp
Moved to version 1.0
[cpPlugins.git] / lib / mstch / render_context.hpp
1 #pragma once
2
3 #include <deque>
4 #include <list>
5 #include <sstream>
6 #include <string>
7 #include <stack>
8
9 #include "mstch/mstch.hpp"
10 #include "state/render_state.hpp"
11 #include "template_type.hpp"
12
13 namespace mstch {
14
15 class render_context {
16  public:
17   class push {
18    public:
19     push(render_context& context, const mstch::node& node = {});
20     ~push();
21     std::string render(const template_type& templt);
22    private:
23     render_context& m_context;
24   };
25
26   render_context(
27       const mstch::node& node,
28       const std::map<std::string, template_type>& partials);
29   const mstch::node& get_node(const std::string& token);
30   std::string render(
31       const template_type& templt, const std::string& prefix = "");
32   std::string render_partial(
33       const std::string& partial_name, const std::string& prefix);
34   template<class T, class... Args>
35   void set_state(Args&& ... args) {
36     m_state.top() = std::unique_ptr<render_state>(
37         new T(std::forward<Args>(args)...));
38   }
39
40  private:
41   static const mstch::node null_node;
42   const mstch::node& find_node(
43       const std::string& token,
44       std::list<node const*> current_nodes);
45   std::map<std::string, template_type> m_partials;
46   std::deque<mstch::node> m_nodes;
47   std::list<const mstch::node*> m_node_ptrs;
48   std::stack<std::unique_ptr<render_state>> m_state;
49 };
50
51 }