Skip to content

Commit 1f7c519

Browse files
committed
Add == and < operators on CassUuid-s to make them usable in std::set, std::map, etc. containers
1 parent e010b6a commit 1f7c519

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

include/cassandra.h

+5
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ typedef struct CassUuid_ {
144144
cass_uint64_t clock_seq_and_node;
145145
} CassUuid;
146146

147+
#ifdef __cplusplus
148+
// If the current project is a C++ one, expose C++ operators on CassUuid.
149+
#include "uuid_operators.hpp"
150+
#endif
151+
147152
/**
148153
* A cluster object describes the configuration of the Cassandra cluster and is used
149154
* to construct a session instance. Unlike other DataStax drivers the cluster object

include/uuid_operators.hpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright (c) DataStax, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef __UUID_OPERATORS_HPP_INCLUDED__
18+
#define __UUID_OPERATORS_HPP_INCLUDED__
19+
20+
#ifdef __cplusplus
21+
#include "cassandra.h"
22+
23+
/**
24+
* Compare two UUIDs for ordering
25+
*
26+
* This operator is useful to use CassUuid variables as std::map keys, for
27+
* instance.
28+
*
29+
* @param uuid1 A UUID
30+
* @param uuid2 Another UUID
31+
* @return true if, and only if, uuid1 is numerically less or equal than uuid2
32+
*/
33+
bool operator<(const CassUuid& uuid1, const CassUuid& uuid2);
34+
35+
/**
36+
* Compare two UUIDs for equality
37+
*
38+
* This operator is useful to use CassUuid variables as std::map keys, for
39+
* instance.
40+
*
41+
* @param uuid1 A UUID
42+
* @param uuid2 Another UUID
43+
* @return true if, and only if, uuid1 is numerically less or equal than uuid2
44+
*/
45+
bool operator==(const CassUuid& uuid1, const CassUuid& uuid2);
46+
#endif
47+
48+
#endif /* __UUID_OPERATORS_HPP_INCLUDED__ */

src/uuids.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ static uint64_t set_version(uint64_t timestamp, uint8_t version) {
4444
return (timestamp & 0x0FFFFFFFFFFFFFFFLL) | (static_cast<uint64_t>(version) << 60);
4545
}
4646

47+
bool operator<(const CassUuid& uuid1, const CassUuid& uuid2) {
48+
if (uuid1.time_and_version == uuid2.time_and_version)
49+
return uuid1.clock_seq_and_node < uuid2.clock_seq_and_node;
50+
else
51+
return uuid1.time_and_version < uuid2.time_and_version;
52+
}
53+
54+
bool operator==(const CassUuid& uuid1, const CassUuid& uuid2) {
55+
return uuid1.time_and_version == uuid2.time_and_version
56+
&& uuid1.clock_seq_and_node == uuid2.clock_seq_and_node;
57+
}
58+
4759
extern "C" {
4860

4961
CassUuidGen* cass_uuid_gen_new() { return CassUuidGen::to(new UuidGen()); }

tests/src/unit/tests/test_uuids.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,35 @@ TEST(UuidUnitTest, FromStringInvalid) {
167167
EXPECT_EQ(cass_uuid_from_string_n("00-00-00-00-11-11-11-11-22-22-22-22-deadbeaf", 36, &uuid),
168168
CASS_ERROR_LIB_BAD_PARAMS);
169169
}
170+
171+
TEST(UuidUnitTest, Operators) {
172+
CassUuid uuid1 = { 0x0000112233445566LL, 0x0000112233445566LL };
173+
CassUuid uuid2 = { 0x0000112233445566LL, 0x0000112233445566LL };
174+
CassUuid uuid3 = { 0x9988776655443322LL, 0x0000112233445566LL };
175+
CassUuid uuid4 = { 0x9988776655443322LL, 0x9988776655443322LL };
176+
177+
// uuid1 == uuid2
178+
EXPECT_TRUE(uuid1 == uuid2);
179+
// uuid1 != uuid3
180+
EXPECT_FALSE(uuid1 == uuid3);
181+
// uuid1 != uuid4
182+
EXPECT_FALSE(uuid1 == uuid4);
183+
// uuid2 != uuid3
184+
EXPECT_FALSE(uuid2 == uuid3);
185+
// uuid2 != uuid3
186+
EXPECT_FALSE(uuid2 == uuid4);
187+
// uuid3 != uuid4
188+
EXPECT_FALSE(uuid3 == uuid4);
189+
// uuid1 >= uuid2
190+
EXPECT_FALSE(uuid1 < uuid2);
191+
// uuid1 < uuid3
192+
EXPECT_TRUE(uuid1 < uuid3);
193+
// uuid1 < uuid4
194+
EXPECT_TRUE(uuid1 < uuid4);
195+
// uuid2 < uuid3
196+
EXPECT_TRUE(uuid2 < uuid3);
197+
// uuid2 < uuid3
198+
EXPECT_TRUE(uuid2 < uuid4);
199+
// uuid3 < uuid4
200+
EXPECT_TRUE(uuid3 < uuid4);
201+
}

0 commit comments

Comments
 (0)