Electroneum
mstch.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <map>
5 #include <string>
6 #include <memory>
7 #include <functional>
8 
9 #include <boost/variant.hpp>
10 
11 namespace mstch {
12 
13 struct config {
14  static std::function<std::string(const std::string&)> escape;
15 };
16 
17 namespace internal {
18 
19 template<class N>
20 class object_t {
21  public:
22  const N& at(const std::string& name) const {
23  cache[name] = (methods.at(name))();
24  return cache[name];
25  }
26 
27  bool has(const std::string name) const {
28  return methods.count(name) != 0;
29  }
30 
31  protected:
32  template<class S>
33  void register_methods(S* s, std::map<std::string,N(S::*)()> methods) {
34  for(auto& item: methods)
35  this->methods.insert({item.first, std::bind(item.second, s)});
36  }
37 
38  private:
39  std::map<std::string, std::function<N()>> methods;
40  mutable std::map<std::string, N> cache;
41 };
42 
43 template<class T, class N>
44 class is_fun {
45  private:
46  using not_fun = char;
47  using fun_without_args = char[2];
48  using fun_with_args = char[3];
49  template <typename U, U> struct really_has;
50  template <typename C> static fun_without_args& test(
51  really_has<N(C::*)() const, &C::operator()>*);
52  template <typename C> static fun_with_args& test(
53  really_has<N(C::*)(const std::string&) const,
54  &C::operator()>*);
55  template <typename> static not_fun& test(...);
56 
57  public:
58  static bool const no_args = sizeof(test<T>(0)) == sizeof(fun_without_args);
59  static bool const has_args = sizeof(test<T>(0)) == sizeof(fun_with_args);
60 };
61 
62 template<class N>
63 using node_renderer = std::function<std::string(const N& n)>;
64 
65 template<class N>
66 class lambda_t {
67  public:
68  template<class F>
69  lambda_t(F f, typename std::enable_if<is_fun<F, N>::no_args>::type* = 0):
70  fun([f](node_renderer<N> renderer, const std::string&) {
71  return renderer(f());
72  })
73  {
74  }
75 
76  template<class F>
77  lambda_t(F f, typename std::enable_if<is_fun<F, N>::has_args>::type* = 0):
78  fun([f](node_renderer<N> renderer, const std::string& text) {
79  return renderer(f(text));
80  })
81  {
82  }
83 
84  std::string operator()(node_renderer<N> renderer,
85  const std::string& text = "") const
86  {
87  return fun(renderer, text);
88  }
89 
90  private:
91  std::function<std::string(node_renderer<N> renderer, const std::string&)> fun;
92 };
93 
94 template <class Key, class Value>
95 struct map : public std::map<Key, Value>
96 {
97  map() {}
98  map(const map<Key, Value>& rhs) : std::map<Key, Value>(rhs) {}
99  map(const std::initializer_list<typename std::map<Key, Value>::value_type>& args) : std::map<Key, Value>(args) {}
100  map& operator=(const map& rhs)
101  {
103  for (auto& i : rhs)
104  std::map<Key, Value>::insert(i);
105  return *this;
106  }
107 };
108 
109 }
110 
111 using node = boost::make_recursive_variant<
112  std::nullptr_t, std::string, int, double, bool, uint64_t, int64_t, uint32_t,
113  internal::lambda_t<boost::recursive_variant_>,
114  std::shared_ptr<internal::object_t<boost::recursive_variant_>>,
115  internal::map<const std::string, boost::recursive_variant_>,
116  std::vector<boost::recursive_variant_>>::type;
120 using array = std::vector<node>;
121 
122 std::string render(
123  const std::string& tmplt,
124  const node& root,
125  const std::map<std::string,std::string>& partials =
126  std::map<std::string,std::string>());
127 
128 }
Definition: mstch.hpp:20
bool has(const std::string name) const
Definition: mstch.hpp:27
static bool const has_args
Definition: mstch.hpp:59
lambda_t(F f, typename std::enable_if< is_fun< F, N >::no_args >::type *=0)
Definition: mstch.hpp:69
#define F(w, k)
Definition: sha512-blocks.c:61
boost::make_recursive_variant< std::nullptr_t, std::string, int, double, bool, uint64_t, int64_t, uint32_t, internal::lambda_t< boost::recursive_variant_ >, std::shared_ptr< internal::object_t< boost::recursive_variant_ > >, internal::map< const std::string, boost::recursive_variant_ >, std::vector< boost::recursive_variant_ > >::type node
Definition: mstch.hpp:116
Definition: mstch.hpp:49
Definition: mstch.hpp:44
std::string operator()(node_renderer< N > renderer, const std::string &text="") const
Definition: mstch.hpp:84
static bool const no_args
Definition: mstch.hpp:58
Definition: block_queue.cpp:41
char not_fun
Definition: mstch.hpp:46
Definition: cryptonote_config.h:156
std::map< std::string, std::function< N()> > methods
Definition: mstch.hpp:39
std::map< std::string, N > cache
Definition: mstch.hpp:40
std::function< std::string(node_renderer< N > renderer, const std::string &)> fun
Definition: mstch.hpp:91
Definition: mstch.hpp:95
std::vector< node > array
Definition: mstch.hpp:120
internal::map< const std::string, node > map
Definition: mstch.hpp:119
Definition: mstch.hpp:11
static std::function< std::string(const std::string &)> escape
Definition: mstch.hpp:14
static fun_without_args & test(really_has< N(C::*)() const, &C::operator()> *)
map(const std::initializer_list< typename std::map< Key, Value >::value_type > &args)
Definition: mstch.hpp:99
char[3] fun_with_args
Definition: mstch.hpp:48
lambda_t(F f, typename std::enable_if< is_fun< F, N >::has_args >::type *=0)
Definition: mstch.hpp:77
map & operator=(const map &rhs)
Definition: mstch.hpp:100
type
Definition: json.h:74
char[2] fun_without_args
Definition: mstch.hpp:47
int bool
Definition: stdbool.h:36
Definition: mstch.hpp:66
map()
Definition: mstch.hpp:97
void register_methods(S *s, std::map< std::string, N(S::*)()> methods)
Definition: mstch.hpp:33
const N & at(const std::string &name) const
Definition: mstch.hpp:22
const char * name
Definition: simplewallet.cpp:180
std::function< std::string(const N &n)> node_renderer
Definition: mstch.hpp:63
void clear(std::string &pass) noexcept
Definition: password.cpp:169
std::string render(const std::string &tmplt, const node &root, const std::map< std::string, std::string > &partials=std::map< std::string, std::string >())
Definition: mstch.cpp:10
map(const map< Key, Value > &rhs)
Definition: mstch.hpp:98
#define s(x, c)
Definition: aesb.c:46