Commit 83484106 authored by Dominik Charousset's avatar Dominik Charousset

Mimic interface of boost::join

parent d5f43de6
...@@ -46,22 +46,44 @@ void split(std::vector<std::string>& result, ...@@ -46,22 +46,44 @@ void split(std::vector<std::string>& result,
bool keep_empties = true); bool keep_empties = true);
template <class Iterator> template <class Iterator>
std::string join(Iterator begin, Iterator end, const std::string& glue) { class iterator_range {
public:
using iterator = Iterator;
iterator_range(iterator first, iterator last) : begin_(first), end_(last) {
// nop
}
iterator begin() const {
return begin_;
}
iterator end() const {
return end_;
}
private:
iterator begin_;
iterator end_;
};
template <class Container>
std::string join(const Container& c, const std::string& glue) {
auto begin = c.begin();
auto end = c.end();
bool first = true; bool first = true;
std::ostringstream oss; std::ostringstream oss;
for ( ; begin != end; ++begin) { for ( ; begin != end; ++begin) {
if (first) first = false; if (first)
else oss << glue; first = false;
else
oss << glue;
oss << *begin; oss << *begin;
} }
return oss.str(); return oss.str();
} }
template <class Container>
std::string join(const Container& c, const std::string& glue) {
return join(c.begin(), c.end(), glue);
}
// end of recursion // end of recursion
inline void splice(std::string&, const std::string&) { inline void splice(std::string&, const std::string&) {
// nop // nop
......
...@@ -28,12 +28,13 @@ std::string either_or_else_type_name(size_t lefts_size, ...@@ -28,12 +28,13 @@ std::string either_or_else_type_name(size_t lefts_size,
const std::string* lefts, const std::string* lefts,
size_t rights_size, size_t rights_size,
const std::string* rights) { const std::string* rights) {
using irange = iterator_range<const std::string*>;
std::string glue = ","; std::string glue = ",";
std::string result; std::string result;
result = "caf::either<"; result = "caf::either<";
result += join(lefts, lefts + lefts_size, glue); result += join(irange{lefts, lefts + lefts_size}, glue);
result += ">::or_else<"; result += ">::or_else<";
result += join(rights, rights + rights_size, glue); result += join(irange{rights, rights + rights_size}, glue);
result += ">"; result += ">";
return result; return result;
} }
......
...@@ -30,21 +30,22 @@ std::string replies_to_type_name(size_t input_size, ...@@ -30,21 +30,22 @@ std::string replies_to_type_name(size_t input_size,
const std::string* output_opt1, const std::string* output_opt1,
size_t output_opt2_size, size_t output_opt2_size,
const std::string* output_opt2) { const std::string* output_opt2) {
using irange = iterator_range<const std::string*>;
std::string glue = ","; std::string glue = ",";
std::string result; std::string result;
// 'void' is not an announced type, hence we check whether uniform_typeid // 'void' is not an announced type, hence we check whether uniform_typeid
// did return a valid pointer to identify 'void' (this has the // did return a valid pointer to identify 'void' (this has the
// possibility of false positives, but those will be catched anyways) // possibility of false positives, but those will be catched anyways)
result = "caf::replies_to<"; result = "caf::replies_to<";
result += join(input, input + input_size, glue); result += join(irange{input, input + input_size}, glue);
if (output_opt2_size == 0) { if (output_opt2_size == 0) {
result += ">::with<"; result += ">::with<";
result += join(output_opt1, output_opt1 + output_opt1_size, glue); result += join(irange{output_opt1, output_opt1 + output_opt1_size}, glue);
} else { } else {
result += ">::with_either<"; result += ">::with_either<";
result += join(output_opt1, output_opt1 + output_opt1_size, glue); result += join(irange{output_opt1, output_opt1 + output_opt1_size}, glue);
result += ">::or_else<"; result += ">::or_else<";
result += join(output_opt2, output_opt2 + output_opt2_size, glue); result += join(irange{output_opt2, output_opt2 + output_opt2_size}, glue);
} }
result += ">"; result += ">";
return result; return result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment