Compare commits

...

2 commits

Author SHA1 Message Date
3374f41e87 Allow passing a dynamic context to render(). 2016-03-30 09:28:30 +02:00
647b282b26 Add missing & to parameter 2016-03-30 09:07:09 +02:00
3 changed files with 28 additions and 1 deletions

View file

@ -16,6 +16,23 @@ struct config {
namespace internal { 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> template<class N>
class object_t { class object_t {
public: public:
@ -24,7 +41,7 @@ class object_t {
return cache[name]; return cache[name];
} }
bool has(const std::string name) const { bool has(const std::string& name) const {
return methods.count(name) != 0; return methods.count(name) != 0;
} }
@ -97,9 +114,11 @@ using node = boost::make_recursive_variant<
std::nullptr_t, std::string, int, double, bool, std::nullptr_t, std::string, int, double, bool,
internal::lambda_t<boost::recursive_variant_>, internal::lambda_t<boost::recursive_variant_>,
std::shared_ptr<internal::object_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::map<const std::string, boost::recursive_variant_>,
std::vector<boost::recursive_variant_>>::type; std::vector<boost::recursive_variant_>>::type;
using object = internal::object_t<node>; using object = internal::object_t<node>;
using dynamic = internal::dynamic_t<node>;
using lambda = internal::lambda_t<node>; using lambda = internal::lambda_t<node>;
using map = std::map<const std::string, node>; using map = std::map<const std::string, node>;
using array = std::vector<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); return object->at(m_token);
} }
const mstch::node& operator()(const std::shared_ptr<dynamic>& object) const {
return object->at(m_token);
}
private: private:
const std::string& m_token; const std::string& m_token;
const mstch::node& m_node; const mstch::node& m_node;

View file

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