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

Reduce code duplication for comparing labels

parent db0007bb
...@@ -53,9 +53,15 @@ public: ...@@ -53,9 +53,15 @@ public:
// -- comparison ------------------------------------------------------------- // -- comparison -------------------------------------------------------------
int compare(const label_view& x) const noexcept; template <class T1, class T2>
static int compare(const T1& lhs, const T2& rhs) noexcept {
auto cmp1 = lhs.name().compare(rhs.name());
return cmp1 != 0 ? cmp1 : lhs.value().compare(rhs.value());
}
int compare(const label_view& other) const noexcept;
int compare(const label& x) const noexcept; int compare(const label& other) const noexcept;
private: private:
size_t name_length_; size_t name_length_;
......
...@@ -42,9 +42,9 @@ public: ...@@ -42,9 +42,9 @@ public:
// -- comparison ------------------------------------------------------------- // -- comparison -------------------------------------------------------------
int compare(const label& x) const noexcept; int compare(const label& other) const noexcept;
int compare(const label_view& x) const noexcept; int compare(const label_view& other) const noexcept;
private: private:
std::string_view name_; std::string_view name_;
......
...@@ -26,13 +26,11 @@ void label::value(std::string_view new_value) { ...@@ -26,13 +26,11 @@ void label::value(std::string_view new_value) {
} }
int label::compare(const label_view& x) const noexcept { int label::compare(const label_view& x) const noexcept {
auto cmp1 = name().compare(x.name()); return compare(*this, x);
return cmp1 != 0 ? cmp1 : value().compare(x.value());
} }
int label::compare(const label& x) const noexcept { int label::compare(const label& x) const noexcept {
auto cmp1 = name().compare(x.name()); return compare(*this, x);
return cmp1 != 0 ? cmp1 : value().compare(x.value());
} }
std::string to_string(const label& x) { std::string to_string(const label& x) {
......
...@@ -8,14 +8,12 @@ ...@@ -8,14 +8,12 @@
namespace caf::telemetry { namespace caf::telemetry {
int label_view::compare(const label_view& x) const noexcept { int label_view::compare(const label_view& other) const noexcept {
auto cmp1 = name().compare(x.name()); return label::compare(*this, other);
return cmp1 != 0 ? cmp1 : value().compare(x.value());
} }
int label_view::compare(const label& x) const noexcept { int label_view::compare(const label& other) const noexcept {
auto cmp1 = name().compare(x.name()); return label::compare(*this, other);
return cmp1 != 0 ? cmp1 : value().compare(x.value());
} }
std::string to_string(const label_view& x) { std::string to_string(const label_view& x) {
......
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