Skip to content

Commit 1f54b39

Browse files
committed
Add == and < operators on CassUuid-s
These operators (for C++) make CassUuid instances usable as keys in std::set, std::map, etc. containers.
1 parent e010b6a commit 1f54b39

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

include/cassandra_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__ */
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/dh-exec
22

33
usr/include/*.h
4+
usr/include/*.hpp
45
usr/lib/*.a usr/lib/${DEB_HOST_MULTIARCH}
56
debian/cassandra.pc usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig
67
debian/cassandra_static.pc usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ endif()
255255

256256
# Determine if the header should be installed
257257
if(CASS_INSTALL_HEADER)
258-
file(GLOB CASS_API_HEADER_FILES ${CASS_INCLUDE_DIR}/*.h)
258+
file(GLOB CASS_API_HEADER_FILES ${CASS_INCLUDE_DIR}/*.h ${CASS_INCLUDE_DIR}/*.hpp)
259259
install(FILES ${CASS_API_HEADER_FILES} DESTINATION ${INSTALL_HEADER_DIR})
260260
endif()
261261

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

+33
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <gtest/gtest.h>
1818

1919
#include "cassandra.h"
20+
#include "cassandra_uuid_operators.hpp"
2021
#include "get_time.hpp"
2122
#include "scoped_ptr.hpp"
2223

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

0 commit comments

Comments
 (0)