object support

This commit is contained in:
Daniel Sipka 2015-04-17 02:07:14 +02:00
parent 8fbcd12284
commit 812b4bdb41
29 changed files with 142 additions and 155 deletions

View file

@ -21,12 +21,14 @@ namespace mstch {
} }
protected: protected:
template<class S> template<class S>
void register_methods(S* sub, std::map<std::string,N(S::*)()> methods) { void register_method(std::string name, S* sub, N(S::*method)()) {
for(auto& m: methods) this->methods.insert({name, std::bind(method, sub)});
this->methods.insert(m.first, std::bind(m.second, sub)); }
void register_method(std::string name, const N& node) {
this->methods.insert({name, [node](){return node;}});
} }
private: private:
const std::map<std::string, std::function<N()>> methods; std::map<std::string, std::function<N()>> methods;
}; };
} }

View file

@ -10,7 +10,6 @@ set(SRC
state/in_section.cpp state/in_section.cpp
state/outside_section.cpp state/outside_section.cpp
visitor/get_token.cpp visitor/get_token.cpp
visitor/has_token.cpp
visitor/is_node_empty.cpp visitor/is_node_empty.cpp
visitor/render_node.cpp visitor/render_node.cpp
visitor/render_section.cpp visitor/render_section.cpp

View file

@ -1,7 +1,6 @@
#include "render_context.hpp" #include "render_context.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "state/outside_section.hpp" #include "state/outside_section.hpp"
#include "visitor/has_token.hpp"
#include "visitor/get_token.hpp" #include "visitor/get_token.hpp"
using namespace mstch; using namespace mstch;
@ -44,9 +43,10 @@ mstch::node render_context::find_node(
token.substr(token.rfind('.') + 1), token.substr(token.rfind('.') + 1),
{find_node(token.substr(0, token.rfind('.')), current_nodes)}); {find_node(token.substr(0, token.rfind('.')), current_nodes)});
else else
for (auto& node: current_nodes) for (auto& node: current_nodes) {
if (boost::apply_visitor(visitor::has_token(token), node)) auto ret = boost::apply_visitor(visitor::get_token(token, node), node);
return boost::apply_visitor(visitor::get_token(token, node), node); if(ret.first) return ret.second;
}
return null_node; return null_node;
} }

View file

@ -19,9 +19,10 @@ std::string state::outside_section::render(
break; break;
case token::type::variable: case token::type::variable:
case token::type::unescaped_variable: { case token::type::unescaped_variable: {
return boost::apply_visitor(visitor::render_node((token.token_type() == auto visitor = visitor::render_node((token.token_type() ==
token::type::variable)?flag::escape_html:flag::none), token::type::variable)?flag::escape_html:flag::none);
ctx.get_node(token.content())); auto node = ctx.get_node(token.content());
return boost::apply_visitor(visitor, node);
} }
case token::type::comment: break; case token::type::comment: break;
case token::type::text: case token::type::text:

View file

@ -8,30 +8,36 @@ get_token::get_token(const std::string& token, const mstch::node& node):
{ {
} }
mstch::node get_token::operator()(const boost::blank& blank) const { std::pair<bool,node> get_token::operator()(const boost::blank& blank) const {
return node; return {token == ".", node};
} }
mstch::node get_token::operator()(const int& i) const { std::pair<bool,node> get_token::operator()(const int& i) const {
return node; return {token == ".", node};
} }
mstch::node get_token::operator()(const bool& b) const { std::pair<bool,node> get_token::operator()(const bool& b) const {
return node; return {token == ".", node};
} }
mstch::node get_token::operator()(const std::string& str) const { std::pair<bool,node> get_token::operator()(const std::string& str) const {
return node; return {token == ".", node};
} }
mstch::node get_token::operator()(const array& arr) const { std::pair<bool,node> get_token::operator()(const array& arr) const {
return node; return {token == ".", node};
} }
mstch::node get_token::operator()(const map& map) const { std::pair<bool,node> get_token::operator()(const map& map) const {
return map.at(token); if(map.count(token) == 1)
return {true, map.at(token)};
else
return {false, node};
} }
mstch::node get_token::operator()(const std::shared_ptr<object>& obj) const { std::pair<bool,node> get_token::operator()(const std::shared_ptr<object>& obj) const {
return obj->at(token); if (obj->has(token))
return {true, obj->at(token)};
else
return {false, node};
} }

View file

@ -6,16 +6,16 @@
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class get_token: public boost::static_visitor<node> { class get_token: public boost::static_visitor<std::pair<bool,mstch::node>> {
public: public:
get_token(const std::string& token, const mstch::node& node); get_token(const std::string& token, const mstch::node& node);
mstch::node operator()(const boost::blank& blank) const; std::pair<bool,mstch::node> operator()(const boost::blank& blank) const;
mstch::node operator()(const int& i) const; std::pair<bool,mstch::node> operator()(const int& i) const;
mstch::node operator()(const bool& b) const; std::pair<bool,mstch::node> operator()(const bool& b) const;
mstch::node operator()(const std::string& str) const; std::pair<bool,mstch::node> operator()(const std::string& str) const;
mstch::node operator()(const array& arr) const; std::pair<bool,mstch::node> operator()(const array& arr) const;
mstch::node operator()(const map& map) const; std::pair<bool,mstch::node> operator()(const map& map) const;
mstch::node operator()(const std::shared_ptr<object>& obj) const; std::pair<bool,mstch::node> operator()(const std::shared_ptr<object>& obj) const;
private: private:
const std::string& token; const std::string& token;
mstch::node node; mstch::node node;

View file

@ -1,35 +0,0 @@
#include "has_token.hpp"
using namespace mstch;
using namespace mstch::visitor;
has_token::has_token(const std::string& token): token(token) {
}
bool has_token::operator()(const boost::blank& blank) const {
return false;
}
bool has_token::operator()(const int& i) const {
return token == ".";
}
bool has_token::operator()(const bool& b) const {
return token == ".";
}
bool has_token::operator()(const std::string& str) const {
return token == ".";
}
bool has_token::operator()(const array& arr) const {
return token == ".";
}
bool has_token::operator()(const map& map) const {
return map.count(token) == 1;
}
bool has_token::operator()(const std::shared_ptr<object>& obj) const {
return obj->has(token);
}

View file

@ -1,23 +0,0 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "mstch/mstch.hpp"
namespace mstch {
namespace visitor {
class has_token: public boost::static_visitor<bool> {
public:
has_token(const std::string& token);
bool operator()(const boost::blank& blank) const;
bool operator()(const int& i) const;
bool operator()(const bool& b) const;
bool operator()(const std::string& str) const;
bool operator()(const array& arr) const;
bool operator()(const map& map) const;
bool operator()(const std::shared_ptr<object>& obj) const;
private:
const std::string& token;
};
}
}

View file

@ -3,12 +3,15 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp> #include <boost/blank.hpp>
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
#include "utils.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class render_node: public boost::static_visitor<std::string> { class render_node : public boost::static_visitor<std::string> {
public: public:
enum class flag { none, escape_html }; enum class flag {
none, escape_html
};
render_node(flag p_flag = flag::none); render_node(flag p_flag = flag::none);
std::string operator()(const boost::blank& blank) const; std::string operator()(const boost::blank& blank) const;
std::string operator()(const int& i) const; std::string operator()(const int& i) const;

View file

@ -1,7 +1,7 @@
class comments: public mstch::object { class comments: public mstch::object {
public: public:
comments() { comments() {
register_methods(this, {{"title", &title}}); register_method("title", this, &comments::title);
} }
mstch::node title() { mstch::node title() {

49
test/data/complex.hpp Normal file
View file

@ -0,0 +1,49 @@
class complex_item: public mstch::object {
private:
const std::string name;
const bool current;
const std::string url;
public:
complex_item(const std::string& name, bool current, const std::string& url):
name{name}, current{current}, url{url}
{
register_method("name", {name});
register_method("current", {current});
register_method("url", {url});
register_method("link", this, &complex_item::link);
}
mstch::node link() {
return !current;
}
};
class complex: public mstch::object {
private:
const std::string header;
const mstch::array item;
public:
complex():
header{"Colors"},
item{
std::make_shared<complex_item>("red", true, "#Red"),
std::make_shared<complex_item>("green", false, "#Green"),
std::make_shared<complex_item>("blue", false, "#Blue")
}
{
register_method("header", {header});
register_method("item", {item});
register_method("list", this, &complex::list);
register_method("empty", this, &complex::empty);
}
mstch::node list() {
return item.size() != 0;
}
mstch::node empty() {
return item.size() == 0;
}
};
const auto complex_data = std::make_shared<complex>();

View file

@ -0,0 +1,25 @@
class dot_notation_price: public mstch::object {
private:
const int value;
const mstch::map currency;
public:
dot_notation_price():
value{200}, currency{{"symbol", std::string{"$"}}, {"name", std::string{"USD"}}}
{
register_method("value", {value});
register_method("vat", this, &dot_notation_price::vat);
register_method("currency", {currency});
}
mstch::node vat() {
return static_cast<int>(value * 0.2);
}
};
const auto dot_notation_data = mstch::map{
{"name", std::string{"A Book"}},
{"authors", mstch::array{std::string{"John Power"}, std::string{"Jamie Walsh"}}},
{"price", std::make_shared<dot_notation_price>()},
{"availability", mstch::map{{"status", true}, {"text", std::string{"In Stock"}}}},
{"truthy", mstch::map{{"zero", 0}, {"notTrue", false}}}
};

View file

@ -1,4 +1,4 @@
const auto empty_sections_data = mstch::map{ const auto empty_string_data = mstch::map{
{"description", std::string{"That is all!"}}, {"description", std::string{"That is all!"}},
{"child", mstch::map{ {"child", mstch::map{
{"description", std::string{""}} {"description", std::string{""}}

View file

@ -1 +1 @@
const auto empty_sections_data = mstch::map{}; const auto empty_template_data = mstch::map{};

View file

@ -1,3 +1,3 @@
const auto empty_sections_data = mstch::map{ const auto error_not_found_data = mstch::map{
{"bar", 2} {"bar", 2}
}; };

View file

@ -1,7 +1,8 @@
class escaped: public mstch::object { class escaped: public mstch::object {
public: public:
escaped() { escaped() {
register_methods(this, {{"title", &title}, {"entities", &entities}}); register_method("title", this, &escaped::title);
register_method("entities", this, &escaped::entities);
} }
mstch::node title() { mstch::node title() {

View file

@ -1,3 +1,3 @@
const auto inlcuded_tag_data = mstch::map{ const auto included_tag_data = mstch::map{
{"html", std::string{"I like {{mustache}}"}} {"html", std::string{"I like {{mustache}}"}}
}; };

View file

@ -1,19 +0,0 @@
({
header: function () {
return "Colors";
},
item: [
{name: "red", current: true, url: "#Red"},
{name: "green", current: false, url: "#Green"},
{name: "blue", current: false, url: "#Blue"}
],
link: function () {
return this["current"] !== true;
},
list: function () {
return this.item.length !== 0;
},
empty: function () {
return this.item.length === 0;
}
})

View file

@ -1,23 +0,0 @@
({
name: "A Book",
authors: ["John Power", "Jamie Walsh"],
price: {
value: 200,
vat: function () {
return this.value * 0.2;
},
currency: {
symbol: '$',
name: 'USD'
}
},
availability: {
status: true,
text: "In Stock"
},
// And now, some truthy false values
truthy: {
zero: 0,
notTrue: false
}
})

View file

@ -1,9 +1,8 @@
class nested_higher_order_sections: public mstch::object { class nested_higher_order_sections: public mstch::object {
public: public:
nested_higher_order_sections() { nested_higher_order_sections() {
register_methods(this, { register_method("bold", this, &nested_higher_order_sections::bold);
{"bold", &nested_higher_order_sections::bold}, register_method("person", this, &nested_higher_order_sections::person);
{"person", &nested_higher_order_sections::person}});
} }
mstch::node bold() { mstch::node bold() {

View file

@ -1,9 +1,8 @@
class partial_template: public mstch::object { class partial_template: public mstch::object {
public: public:
partial_template() { partial_template() {
register_methods(this, { register_method("title", this, &partial_template::title);
{"title", &partial_template::title}, register_method("again", this, &partial_template::again);
{"again", &partial_template::again}});
} }
mstch::node title() { mstch::node title() {

View file

@ -1,7 +1,7 @@
class section_functions_in_partials: public mstch::object { class section_functions_in_partials: public mstch::object {
public: public:
section_functions_in_partials() { section_functions_in_partials() {
register_methods(this, {{"bold"}, &section_functions_in_partials::bold}); register_method("bold", this, &section_functions_in_partials::bold);
} }
mstch::node bold() { mstch::node bold() {

View file

@ -1,7 +1,7 @@
class unescaped: public mstch::object { class unescaped: public mstch::object {
public: public:
unescaped() { unescaped() {
register_methods(this, {{"title", &unescaped::title}}); register_method("title", this, &unescaped::title);
} }
mstch::node title() { mstch::node title() {
@ -9,4 +9,4 @@ public:
} }
}; };
const auto unescped_data = std::make_shared<unescaped>(); const auto unescaped_data = std::make_shared<unescaped>();

View file

@ -12,6 +12,7 @@ void wrap_code(std::istream& input, std::ostream& output) {
output << line; output << line;
if(!input.eof()) output << std::endl; if(!input.eof()) output << std::endl;
} }
output << std::endl;
} }
void wrap_string(std::istream& input, std::ostream& output, const std::string& variable_name) { void wrap_string(std::istream& input, std::ostream& output, const std::string& variable_name) {

View file

@ -20,16 +20,18 @@ MSTCH_TEST(array_of_strings)
MSTCH_TEST(backslashes) MSTCH_TEST(backslashes)
MSTCH_TEST(bug_11_eating_whitespace) MSTCH_TEST(bug_11_eating_whitespace)
MSTCH_TEST(bug_length_property) MSTCH_TEST(bug_length_property)
//MSTCH_TEST(comments) MSTCH_TEST(comments)
MSTCH_TEST(complex)
MSTCH_TEST(context_lookup) MSTCH_TEST(context_lookup)
MSTCH_TEST(disappearing_whitespace) MSTCH_TEST(disappearing_whitespace)
MSTCH_TEST(dot_notation)
MSTCH_TEST(double_render) MSTCH_TEST(double_render)
MSTCH_TEST(empty_list) MSTCH_TEST(empty_list)
MSTCH_TEST(empty_sections) MSTCH_TEST(empty_sections)
MSTCH_TEST(empty_string) MSTCH_TEST(empty_string)
MSTCH_TEST(empty_template) MSTCH_TEST(empty_template)
MSTCH_TEST(error_not_found) MSTCH_TEST(error_not_found)
//MSTCH_TEST(escaped) MSTCH_TEST(escaped)
MSTCH_TEST(falsy) MSTCH_TEST(falsy)
MSTCH_TEST(falsy_array) MSTCH_TEST(falsy_array)
MSTCH_TEST(grandparent_context) MSTCH_TEST(grandparent_context)
@ -50,7 +52,7 @@ MSTCH_PARTIAL_TEST(partial_array)
MSTCH_PARTIAL_TEST(partial_array_of_partials) MSTCH_PARTIAL_TEST(partial_array_of_partials)
MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit) MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
MSTCH_PARTIAL_TEST(partial_empty) MSTCH_PARTIAL_TEST(partial_empty)
//MSTCH_PARTIAL_TEST(partial_template) MSTCH_PARTIAL_TEST(partial_template)
MSTCH_TEST(recursion_with_same_names) MSTCH_TEST(recursion_with_same_names)
MSTCH_TEST(reuse_of_enumerables) MSTCH_TEST(reuse_of_enumerables)
MSTCH_TEST(section_as_context) MSTCH_TEST(section_as_context)
@ -58,6 +60,6 @@ MSTCH_TEST(section_as_context)
MSTCH_TEST(string_as_context) MSTCH_TEST(string_as_context)
MSTCH_TEST(two_in_a_row) MSTCH_TEST(two_in_a_row)
MSTCH_TEST(two_sections) MSTCH_TEST(two_sections)
//MSTCH_TEST(unescaped) MSTCH_TEST(unescaped)
MSTCH_TEST(whitespace) MSTCH_TEST(whitespace)
MSTCH_TEST(zero_view) MSTCH_TEST(zero_view)