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> template<class T>
inline const mstch::node& operator()(const T& t) const { const mstch::node& operator()(const T& t) const {
return node; 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: private:
const std::string& token; const std::string& token;
const mstch::node& node; 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 == "."; 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: private:
const std::string& token; 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 { inline bool operator()(const T& t) const {
return false; return false;
} }
};
template<> bool operator()(const std::nullptr_t& nul) const {
inline bool is_node_empty::operator()(const std::nullptr_t& nul) const {
return true; return true;
} }
template<> bool operator()(const int& value) const {
inline bool is_node_empty::operator()(const int& value) const {
return value == 0; return value == 0;
} }
template<> bool operator()(const double& value) const {
inline bool is_node_empty::operator()(const double& value) const {
return value == 0; return value == 0;
} }
template<> bool operator()(const bool& value) const {
inline bool is_node_empty::operator()(const bool& value) const {
return !value; return !value;
} }
template<> bool operator()(const std::string& value) const {
inline bool is_node_empty::operator()(const std::string& value) const {
return value == ""; return value == "";
} }
template<> bool operator()(const array& array) const {
inline bool is_node_empty::operator()(const array& array) const {
return array.size() == 0; return array.size() == 0;
} }
};
} }

View File

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

View File

@ -24,14 +24,7 @@ class render_section: public boost::static_visitor<std::string> {
return render_context::push(ctx, t).render(section); return render_context::push(ctx, t).render(section);
} }
private: std::string operator()(const lambda& fun) const {
render_context& ctx;
const template_type& section;
flag m_flag;
};
template<>
inline std::string render_section::operator()(const lambda& fun) const {
std::string section_str; std::string section_str;
for(auto& token: section) for(auto& token: section)
section_str += token.raw(); section_str += token.raw();
@ -41,8 +34,7 @@ inline std::string render_section::operator()(const lambda& fun) const {
}); });
} }
template<> std::string operator()(const array& array) const {
inline std::string render_section::operator()(const array& array) const {
std::string out; std::string out;
if(m_flag == flag::keep_array) if(m_flag == flag::keep_array)
return render_context::push(ctx, array).render(section); return render_context::push(ctx, array).render(section);
@ -52,4 +44,10 @@ inline std::string render_section::operator()(const array& array) const {
return out; return out;
} }
private:
render_context& ctx;
const template_type& section;
flag m_flag;
};
} }