Allow passing a dynamic context to render().

This commit is contained in:
King_DuckZ 2016-03-30 09:28:30 +02:00
parent 647b282b26
commit 3374f41e87
3 changed files with 27 additions and 0 deletions

View file

@ -16,6 +16,23 @@ struct config {
namespace internal {
template<class N>
class dynamic_t {
public:
virtual ~dynamic_t() = default;
const N& at (const std::string& name) const {
cache = this->get_value(name);
return cache;
}
bool has(const std::string& name) const {
return this->has_value(name);
}
private:
virtual N get_value(const std::string& name) const = 0;
virtual bool has_value(const std::string& name) const = 0;
mutable N cache;
};
template<class N>
class object_t {
public:
@ -97,9 +114,11 @@ 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::shared_ptr<internal::dynamic_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 dynamic = internal::dynamic_t<node>;
using lambda = internal::lambda_t<node>;
using map = std::map<const std::string, node>;
using array = std::vector<node>;

View file

@ -27,6 +27,10 @@ class get_token: public boost::static_visitor<const mstch::node&> {
return object->at(m_token);
}
const mstch::node& operator()(const std::shared_ptr<dynamic>& object) const {
return object->at(m_token);
}
private:
const std::string& m_token;
const mstch::node& m_node;

View file

@ -24,6 +24,10 @@ class has_token: public boost::static_visitor<bool> {
return object->has(m_token);
}
bool operator()(const std::shared_ptr<dynamic>& object) const {
return object->has(m_token);
}
private:
const std::string& m_token;
};