Commit 6ad52ebb authored by Dominik Charousset's avatar Dominik Charousset

Add to_string and ostream overloads

parent 69bd2505
...@@ -18,8 +18,9 @@ ...@@ -18,8 +18,9 @@
#pragma once #pragma once
#include <string>
#include <functional> #include <functional>
#include <iosfwd>
#include <string>
#include <type_traits> #include <type_traits>
#include "caf/detail/atom_val.hpp" #include "caf/detail/atom_val.hpp"
...@@ -35,7 +36,10 @@ enum class atom_value : uint64_t { ...@@ -35,7 +36,10 @@ enum class atom_value : uint64_t {
}; };
/// @relates atom_value /// @relates atom_value
std::string to_string(const atom_value& what); std::string to_string(atom_value x);
/// @relates atom_value
std::ostream& operator<<(std::ostream& out, atom_value x);
/// @relates atom_value /// @relates atom_value
atom_value to_lowercase(atom_value x); atom_value to_lowercase(atom_value x);
......
...@@ -21,8 +21,10 @@ ...@@ -21,8 +21,10 @@
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <map> #include <map>
#include <ostream>
#include <string> #include <string>
#include "caf/deep_to_string.hpp"
#include "caf/string_view.hpp" #include "caf/string_view.hpp"
namespace caf { namespace caf {
...@@ -328,6 +330,14 @@ private: ...@@ -328,6 +330,14 @@ private:
map_type xs_; map_type xs_;
}; };
// -- free functions -----------------------------------------------------------
// @relates dictionary
template <class T>
std::string to_string(const dictionary<T>& xs) {
return deep_to_string(xs.container());
}
// -- operators ---------------------------------------------------------------- // -- operators ----------------------------------------------------------------
// @relates dictionary // @relates dictionary
...@@ -366,4 +376,10 @@ bool operator>=(const dictionary<T>& xs, const dictionary<T>& ys) { ...@@ -366,4 +376,10 @@ bool operator>=(const dictionary<T>& xs, const dictionary<T>& ys) {
return xs.container() >= ys.container(); return xs.container() >= ys.container();
} }
// @relates dictionary
template <class T>
std::ostream& operator<<(std::ostream& out, const dictionary<T>& xs) {
return out << to_string(xs);
}
} // namespace caf } // namespace caf
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <ostream>
#include "caf/string_view.hpp" #include "caf/string_view.hpp"
...@@ -65,12 +66,19 @@ atom_value atom_from_string(string_view x) { ...@@ -65,12 +66,19 @@ atom_value atom_from_string(string_view x) {
return static_cast<atom_value>(detail::atom_val(buf.data())); return static_cast<atom_value>(detail::atom_val(buf.data()));
} }
std::string to_string(const atom_value& x) { std::string to_string(atom_value x) {
atom_value_buf str; atom_value_buf str;
auto len = decode(str, x); auto len = decode(str, x);
return std::string(str.begin(), str.begin() + len); return std::string(str.begin(), str.begin() + len);
} }
std::ostream& operator<<(std::ostream& out, atom_value x) {
atom_value_buf str;
auto len = decode(str, x);
out.write(str.data(), len);
return out;
}
int compare(atom_value x, atom_value y) { int compare(atom_value x, atom_value y) {
return memcmp(&x, &y, sizeof(atom_value)); return memcmp(&x, &y, sizeof(atom_value));
} }
......
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