diff options
| author | Mistivia <i@mistivia.com> | 2025-01-16 15:43:04 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-01-16 15:53:58 +0800 |
| commit | 8689a7c78c50676ea739f52fbcee9f091709f5c0 (patch) | |
| tree | bbb72c68e5e1753f751133941a402caa43e29a16 /lib/mstch/include | |
| parent | 00afb767ae37488d99f78363d031b898b1932354 (diff) | |
add mstch
Diffstat (limited to 'lib/mstch/include')
| -rw-r--r-- | lib/mstch/include/mstch/mstch.hpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/mstch/include/mstch/mstch.hpp b/lib/mstch/include/mstch/mstch.hpp new file mode 100644 index 0000000..58d3330 --- /dev/null +++ b/lib/mstch/include/mstch/mstch.hpp @@ -0,0 +1,113 @@ +#pragma once + +#include <vector> +#include <map> +#include <string> +#include <memory> +#include <functional> + +#include <boost/variant.hpp> + +namespace mstch { + +struct config { + static std::function<std::string(const std::string&)> escape; +}; + +namespace internal { + +template<class N> +class object_t { + public: + const N& at(const std::string& name) const { + cache[name] = (methods.at(name))(); + return cache[name]; + } + + bool has(const std::string name) const { + return methods.count(name) != 0; + } + + protected: + template<class S> + void register_methods(S* s, std::map<std::string,N(S::*)()> methods) { + for(auto& item: methods) + this->methods.insert({item.first, std::bind(item.second, s)}); + } + + private: + std::map<std::string, std::function<N()>> methods; + mutable std::map<std::string, N> cache; +}; + +template<class T, class N> +class is_fun { + private: + using not_fun = char; + using fun_without_args = char[2]; + using fun_with_args = char[3]; + template <typename U, U> struct really_has; + template <typename C> static fun_without_args& test( + really_has<N(C::*)() const, &C::operator()>*); + template <typename C> static fun_with_args& test( + really_has<N(C::*)(const std::string&) const, + &C::operator()>*); + template <typename> static not_fun& test(...); + + public: + static bool const no_args = sizeof(test<T>(0)) == sizeof(fun_without_args); + static bool const has_args = sizeof(test<T>(0)) == sizeof(fun_with_args); +}; + +template<class N> +using node_renderer = std::function<std::string(const N& n)>; + +template<class N> +class lambda_t { + public: + template<class F> + lambda_t(F f, typename std::enable_if<is_fun<F, N>::no_args>::type* = 0): + fun([f](node_renderer<N> renderer, const std::string&) { + return renderer(f()); + }) + { + } + + template<class F> + lambda_t(F f, typename std::enable_if<is_fun<F, N>::has_args>::type* = 0): + fun([f](node_renderer<N> renderer, const std::string& text) { + return renderer(f(text)); + }) + { + } + + std::string operator()(node_renderer<N> renderer, + const std::string& text = "") const + { + return fun(renderer, text); + } + + private: + std::function<std::string(node_renderer<N> renderer, const std::string&)> fun; +}; + +} + +using node = boost::make_recursive_variant< + std::nullptr_t, std::string, int, double, bool, + internal::lambda_t<boost::recursive_variant_>, + std::shared_ptr<internal::object_t<boost::recursive_variant_>>, + std::map<const std::string, boost::recursive_variant_>, + std::vector<boost::recursive_variant_>>::type; +using object = internal::object_t<node>; +using lambda = internal::lambda_t<node>; +using map = std::map<const std::string, node>; +using array = std::vector<node>; + +std::string render( + const std::string& tmplt, + const node& root, + const std::map<std::string,std::string>& partials = + std::map<std::string,std::string>()); + +} |
