/* The source code contained in this file is based on the original code by Daniel Sipka (https://github.com/no1msd/mstch). The original license by Daniel Sipka can be read below: The MIT License (MIT) Copyright (c) 2015 Daniel Sipka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include #include #include #include #include #include namespace apache { namespace thrift { namespace mstch { namespace internal { template 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_.find(name) != methods_.end()); } protected: void register_method(std::string name, std::function method) { auto result = methods_.emplace(std::move(name), std::move(method)); if (!result.second) { throw std::runtime_error( "Method already registered: " + result.first->first); } } template void register_methods( S* s, const std::unordered_map& methods) { for (const auto& method : methods) { register_method( method.first, [s, m = method.second]() { return (s->*m)(); }); } } private: std::unordered_map> methods_; mutable std::unordered_map cache_; }; template class is_fun { private: using not_fun = char; using fun_without_args = char[2]; using fun_with_args = char[3]; template struct really_has; template static fun_without_args& test(really_has*); template static fun_with_args& test( really_has*); template static not_fun& test(...); public: static const bool no_args = sizeof(test(0)) == sizeof(fun_without_args); static const bool has_args = sizeof(test(0)) == sizeof(fun_with_args); }; template using node_renderer = std::function; template class lambda_t { public: template /* implicit */ lambda_t( F f, typename std::enable_if::no_args>::type* = 0) : fun([f](node_renderer renderer, const std::string&) { return renderer(f()); }) {} template /* implicit */ lambda_t( F f, typename std::enable_if::has_args>::type* = 0) : fun([f](node_renderer renderer, const std::string& text) { return renderer(f(text)); }) {} std::string operator()( node_renderer renderer, const std::string& text = "") const { return fun(renderer, text); } private: std::function renderer, const std::string&)> fun; }; template using node_base = std::variant< std::nullptr_t, std::string, int, double, bool, internal::lambda_t, std::shared_ptr>, std::map, std::vector>; } // namespace internal struct node : internal::node_base { using base = internal::node_base; using base::base; /* implicit */ node(std::string_view sv) : base(std::string(sv)) {} template decltype(auto) visit(Visitor&&... visitor) { return std::visit(visitor..., static_cast(*this)); } template decltype(auto) visit(Visitor&&... visitor) const { return std::visit(visitor..., static_cast(*this)); } }; template node make_shared_node(A&&... a) { return node(std::make_shared(static_cast(a)...)); } using object = internal::object_t; using lambda = internal::lambda_t; using map = std::map; using array = std::vector; std::string render( const std::string& tmplt, const node& root, const std::map& partials = std::map()); } // namespace mstch } // namespace thrift } // namespace apache