Skip to content

Commit b644296

Browse files
committed
mapi_lib: switch SVREID::compare with SVREID::operator<=>
1 parent 448a9e4 commit b644296

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

include/gromox/mapidefs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,8 @@ struct GX_EXPORT SVREID {
11481148
uint64_t message_id;
11491149
uint32_t instance;
11501150

1151-
int compare(const SVREID &) const;
1151+
std::strong_ordering operator<=>(const SVREID &) const;
1152+
inline bool operator==(const SVREID &o) const { return (*this <=> o) == 0; }
11521153
std::string repr(bool verbose = true) const;
11531154
};
11541155

include/gromox/propval.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <compare>
23
#include <cstdint>
34
#include <gromox/common_types.hpp>
45

@@ -7,7 +8,7 @@ extern GX_EXPORT void propval_free(uint16_t type, void *pvalue);
78
extern GX_EXPORT uint32_t propval_size(uint16_t type, const void *pvalue) __attribute__((nonnull(2)));
89
extern GX_EXPORT int propval_compare(const void *, const void *, gromox::proptype_t) __attribute__((nonnull(1,2)));
910
extern GX_EXPORT bool propval_compare_relop(relop, gromox::proptype_t, const void *, const void *) __attribute__((nonnull(3,4)));
10-
extern GX_EXPORT int SVREID_compare(const SVREID *, const SVREID *);
11+
extern GX_EXPORT std::strong_ordering SVREID_compare(const SVREID *, const SVREID *);
1112
namespace gromox {
1213
extern GX_EXPORT bool three_way_eval(enum relop, int);
1314
extern GX_EXPORT bool propval_compare_relop_nullok(relop, uint16_t proptype, const void *, const void *);

lib/mapi/propval.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ std::strong_ordering BINARY::operator<=>(const BINARY &o) const
493493
return cb < o.cb ? std::strong_ordering::less : std::strong_ordering::greater;
494494
}
495495

496-
int SVREID::compare(const SVREID &o) const
496+
std::strong_ordering SVREID::operator<=>(const SVREID &o) const
497497
{
498498
/*
499499
* This performs a FLATUID/bytewise comparison similar to BINARY properties.
@@ -503,10 +503,10 @@ int SVREID::compare(const SVREID &o) const
503503
uint16_t o_len = cpu_to_le16(o.pbin != nullptr ? o.pbin->cb + 1 : 21);
504504
uint8_t flag = pbin == nullptr;
505505
uint8_t o_flag = o.pbin == nullptr;
506-
auto ret = memcmp(&len, &o_len, sizeof(uint16_t));
506+
auto ret = memcmp(&len, &o_len, sizeof(uint16_t)) <=> 0;
507507
if (ret != 0)
508508
return ret;
509-
ret = memcmp(&flag, &o_flag, sizeof(uint8_t));
509+
ret = memcmp(&flag, &o_flag, sizeof(uint8_t)) <=> 0;
510510
if (ret != 0)
511511
return ret;
512512
uint8_t buf[20], o_buf[20];
@@ -521,19 +521,16 @@ int SVREID::compare(const SVREID &o) const
521521
cpu_to_le64p(&o_buf[8], o.message_id);
522522
cpu_to_le32p(&o_buf[16], o.instance);
523523
}
524-
auto c = (flag ? bin : *pbin) <=> (o_flag ? o_bin : *o.pbin);
525-
if (c == 0)
526-
return 0;
527-
return c < 0 ? -1 : 1;
524+
return (flag ? bin : *pbin) <=> (o_flag ? o_bin : *o.pbin);
528525
}
529526

530-
int SVREID_compare(const SVREID *a, const SVREID *b)
527+
std::strong_ordering SVREID_compare(const SVREID *a, const SVREID *b)
531528
{
532529
if (a == nullptr)
533-
return b == nullptr ? 0 : -1;
530+
return b == nullptr ? std::strong_ordering::equal : std::strong_ordering::less;
534531
if (b == nullptr)
535-
return 1;
536-
return a->compare(*b);
532+
return std::strong_ordering::greater;
533+
return *a <=> *b;
537534
}
538535

539536
template<typename T> static std::strong_ordering fpcompare(T x, T y)
@@ -611,8 +608,10 @@ int propval_compare(const void *pvalue1, const void *pvalue2, proptype_t proptyp
611608
auto c = *static_cast<const BINARY *>(pvalue1) <=> *static_cast<const BINARY *>(pvalue2);
612609
return c == 0 ? 0 : c < 0 ? -1 : 1;
613610
}
614-
case PT_SVREID:
615-
return static_cast<const SVREID *>(pvalue1)->compare(*static_cast<const SVREID *>(pvalue2));
611+
case PT_SVREID: {
612+
auto c = *static_cast<const SVREID *>(pvalue1) <=> *static_cast<const SVREID *>(pvalue2);
613+
return c == 0 ? 0 : c < 0 ? -1 : 1;
614+
}
616615
case PT_MV_SHORT: {
617616
auto a = static_cast<const SHORT_ARRAY *>(pvalue1);
618617
auto b = static_cast<const SHORT_ARRAY *>(pvalue2);

tests/utiltest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,11 @@ static int t_cmp_svreid()
334334
ep.g_svreid(&s1);
335335
ep.init(eid2, sizeof(eid2), zalloc, 0);
336336
ep.g_svreid(&s2);
337-
assert(s1.compare(s2) < 0);
338-
assert(s1.compare(s1) == 0);
339-
assert(s2.compare(s1) > 0);
337+
assert(s1 < s2);
338+
assert(s1 == s1);
339+
assert(s2 > s1);
340+
assert(s2 == s2);
341+
assert(s1 != s2);
340342
assert(SVREID_compare(nullptr, &s1) < 0);
341343
assert(SVREID_compare(nullptr, &s2) < 0);
342344
assert(SVREID_compare(nullptr, nullptr) == 0);

0 commit comments

Comments
 (0)