no need for template specialization

This commit is contained in:
Daniel Sipka 2015-04-29 13:47:24 +02:00
parent 105e708d42
commit 8fa398252d
5 changed files with 83 additions and 102 deletions

View File

@ -15,25 +15,21 @@ class get_token: public boost::static_visitor<const mstch::node&> {
}
template<class T>
inline const mstch::node& operator()(const T& t) const {
const mstch::node& operator()(const T& t) const {
return node;
}
const mstch::node& operator()(const map& map) const {
return map.at(token);
}
const mstch::node& operator()(const std::shared_ptr<object>& object) const {
return object->at(token);
}
private:
const std::string& token;
const mstch::node& node;
};
template<>
inline const mstch::node& get_token::operator()(const map& map) const {
return map.at(token);
}
template<>
inline const mstch::node& get_token::operator()(
const std::shared_ptr<object>& object) const
{
return object->at(token);
}
}

View File

@ -16,18 +16,16 @@ class has_token: public boost::static_visitor<bool> {
return token == ".";
}
bool operator()(const map& map) const {
return map.count(token) == 1;
}
bool operator()(const std::shared_ptr<object>& object) const {
return object->has(token);
}
private:
const std::string& token;
};
template<>
inline bool has_token::operator()(const map& map) const {
return map.count(token) == 1;
}
template<>
inline bool has_token::operator()(const std::shared_ptr<object>& object) const {
return object->has(token);
}
}

View File

@ -12,36 +12,30 @@ class is_node_empty: public boost::static_visitor<bool> {
inline bool operator()(const T& t) const {
return false;
}
bool operator()(const std::nullptr_t& nul) const {
return true;
}
bool operator()(const int& value) const {
return value == 0;
}
bool operator()(const double& value) const {
return value == 0;
}
bool operator()(const bool& value) const {
return !value;
}
bool operator()(const std::string& value) const {
return value == "";
}
bool operator()(const array& array) const {
return array.size() == 0;
}
};
template<>
inline bool is_node_empty::operator()(const std::nullptr_t& nul) const {
return true;
}
template<>
inline bool is_node_empty::operator()(const int& value) const {
return value == 0;
}
template<>
inline bool is_node_empty::operator()(const double& value) const {
return value == 0;
}
template<>
inline bool is_node_empty::operator()(const bool& value) const {
return !value;
}
template<>
inline bool is_node_empty::operator()(const std::string& value) const {
return value == "";
}
template<>
inline bool is_node_empty::operator()(const array& array) const {
return array.size() == 0;
}
}

View File

@ -19,35 +19,30 @@ class render_node: public boost::static_visitor<std::string> {
return "";
}
std::string operator()(const int& value) const {
return std::to_string(value);
}
std::string operator()(const double& value) const {
std::stringstream ss;
ss << value;
return ss.str();
}
std::string operator()(const bool& value) const {
return value ? "true" : "false";
}
std::string operator()(const lambda& value) const {
return (m_flag == flag::escape_html) ? html_escape(value()) : value();
}
std::string operator()(const std::string& value) const {
return (m_flag == flag::escape_html) ? html_escape(value) : value;
}
private:
flag m_flag;
};
template<>
inline std::string render_node::operator()(const int& value) const {
return std::to_string(value);
}
template<>
inline std::string render_node::operator()(const double& value) const {
std::stringstream ss;
ss << value;
return ss.str();
}
template<>
inline std::string render_node::operator()(const bool& value) const {
return value?"true":"false";
}
template<>
inline std::string render_node::operator()(const lambda& value) const {
return (m_flag == flag::escape_html) ? html_escape(value()) : value();
}
template<>
inline std::string render_node::operator()(const std::string& value) const {
return (m_flag == flag::escape_html) ? html_escape(value) : value;
}
}

View File

@ -24,14 +24,7 @@ class render_section: public boost::static_visitor<std::string> {
return render_context::push(ctx, t).render(section);
}
private:
render_context& ctx;
const template_type& section;
flag m_flag;
};
template<>
inline std::string render_section::operator()(const lambda& fun) const {
std::string operator()(const lambda& fun) const {
std::string section_str;
for(auto& token: section)
section_str += token.raw();
@ -39,10 +32,9 @@ inline std::string render_section::operator()(const lambda& fun) const {
return fun(section_str, [this](const std::string& str) {
return render_context::push(ctx).render(template_type{str});
});
}
}
template<>
inline std::string render_section::operator()(const array& array) const {
std::string operator()(const array& array) const {
std::string out;
if(m_flag == flag::keep_array)
return render_context::push(ctx, array).render(section);
@ -50,6 +42,12 @@ inline std::string render_section::operator()(const array& array) const {
for (auto& item: array)
out += visit(render_section(ctx, section, flag::keep_array), item);
return out;
}
}
private:
render_context& ctx;
const template_type& section;
flag m_flag;
};
}