Skip to content

Commit ab5ec65

Browse files
committed
Add iox2::package_version to C++ API
1 parent 5df02b2 commit ab5ec65

4 files changed

Lines changed: 226 additions & 0 deletions

File tree

iceoryx2-cxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ add_library(iceoryx2-cxx-object-lib OBJECT
8484
src/static_config_request_response.cpp
8585
src/subscriber_details.cpp
8686
src/unique_port_id.cpp
87+
src/version.cpp
8788
src/waitset.cpp
8889
src/writer_details.cpp
8990
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2026 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#ifndef IOX2_VERSION_HPP
14+
#define IOX2_VERSION_HPP
15+
16+
#include "iox2/internal/iceoryx2.hpp"
17+
18+
#include <cstdint>
19+
#include <iosfwd>
20+
21+
namespace iox2 {
22+
23+
/// Version number.
24+
struct PackageVersion {
25+
std::uint16_t major;
26+
std::uint16_t minor;
27+
std::uint16_t patch;
28+
};
29+
30+
/// Returns the crates version acquired through the internal environment variables set by cargo,
31+
/// ("CARGO_PKG_VERSION_{MAJOR|MINOR|PATCH}").
32+
PackageVersion package_version();
33+
34+
auto operator<<(std::ostream& stream, const PackageVersion& version) -> std::ostream&;
35+
auto operator==(const PackageVersion& lhs, const PackageVersion& rhs) -> bool;
36+
auto operator<(const PackageVersion& lhs, const PackageVersion& rhs) -> bool;
37+
38+
} // namespace iox2
39+
40+
#endif

iceoryx2-cxx/src/version.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2026 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#include "iox2/version.hpp"
14+
15+
#include <ostream>
16+
17+
namespace iox2 {
18+
19+
PackageVersion package_version() {
20+
iox2_package_version_t const v = iox2_package_version();
21+
return PackageVersion { v.major, v.minor, v.patch };
22+
}
23+
24+
auto operator<<(std::ostream& stream, const PackageVersion& version) -> std::ostream& {
25+
return stream << version.major << '.' << version.minor << '.' << version.patch;
26+
}
27+
28+
auto operator==(const PackageVersion& lhs, const PackageVersion& rhs) -> bool {
29+
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch;
30+
}
31+
32+
auto operator<(const PackageVersion& lhs, const PackageVersion& rhs) -> bool {
33+
if (lhs.major != rhs.major) {
34+
return lhs.major < rhs.major;
35+
} else if (lhs.minor != rhs.minor) {
36+
return lhs.minor < rhs.minor;
37+
} else {
38+
return lhs.patch < rhs.patch;
39+
}
40+
}
41+
42+
} // namespace iox2
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright (c) 2026 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#include "iox2/version.hpp"
14+
#include "test.hpp"
15+
16+
#include <sstream>
17+
18+
namespace {
19+
using namespace iox2;
20+
21+
TEST(VersionTest, version_obtains_version_number) {
22+
ASSERT_EQ(package_version().major, 0);
23+
ASSERT_EQ(package_version().minor, 8);
24+
ASSERT_EQ(package_version().patch, 999);
25+
}
26+
27+
TEST(VersionTest, version_numbers_compare_equal_if_all_components_are_equal) {
28+
PackageVersion sut1;
29+
sut1.major = 1;
30+
sut1.minor = 2;
31+
sut1.patch = 3;
32+
PackageVersion sut2;
33+
sut2.major = 1;
34+
sut2.minor = 2;
35+
sut2.patch = 3;
36+
EXPECT_EQ(sut1, sut1);
37+
EXPECT_EQ(sut1, sut2);
38+
EXPECT_EQ(sut2, sut1);
39+
sut1.major = 25;
40+
sut1.minor = 22;
41+
sut1.patch = 0;
42+
sut2.major = 25;
43+
sut2.minor = 22;
44+
sut2.patch = 0;
45+
EXPECT_EQ(sut1, sut1);
46+
EXPECT_EQ(sut1, sut2);
47+
EXPECT_EQ(sut2, sut1);
48+
}
49+
50+
TEST(VersionTest, version_numbers_do_not_compare_equal_if_major_version_differs) {
51+
PackageVersion sut1;
52+
sut1.major = 1;
53+
sut1.minor = 2;
54+
sut1.patch = 3;
55+
PackageVersion sut2;
56+
sut2.major = 0;
57+
sut2.minor = 2;
58+
sut2.patch = 3;
59+
EXPECT_FALSE(sut1 == sut2);
60+
EXPECT_FALSE(sut2 == sut1);
61+
sut1.major = 99;
62+
sut2.major = 6;
63+
EXPECT_FALSE(sut1 == sut2);
64+
EXPECT_FALSE(sut2 == sut1);
65+
}
66+
67+
TEST(VersionTest, version_numbers_do_not_compare_equal_if_minor_version_differs) {
68+
PackageVersion sut1;
69+
sut1.major = 1;
70+
sut1.minor = 2;
71+
sut1.patch = 3;
72+
PackageVersion sut2;
73+
sut2.major = 1;
74+
sut2.minor = 0;
75+
sut2.patch = 3;
76+
EXPECT_FALSE(sut1 == sut2);
77+
EXPECT_FALSE(sut2 == sut1);
78+
sut1.minor = 99;
79+
sut2.minor = 6;
80+
EXPECT_FALSE(sut1 == sut2);
81+
EXPECT_FALSE(sut2 == sut1);
82+
}
83+
84+
TEST(VersionTest, version_numbers_do_not_compare_equal_if_patch_version_differs) {
85+
PackageVersion sut1;
86+
sut1.major = 1;
87+
sut1.minor = 2;
88+
sut1.patch = 3;
89+
PackageVersion sut2;
90+
sut2.major = 1;
91+
sut2.minor = 2;
92+
sut2.patch = 0;
93+
EXPECT_FALSE(sut1 == sut2);
94+
EXPECT_FALSE(sut2 == sut1);
95+
sut1.patch = 99;
96+
sut2.patch = 6;
97+
EXPECT_FALSE(sut1 == sut2);
98+
EXPECT_FALSE(sut2 == sut1);
99+
}
100+
101+
TEST(VersionTest, version_numbers_less_compares_lexicographically) {
102+
PackageVersion sut1;
103+
sut1.major = 1;
104+
sut1.minor = 2;
105+
sut1.patch = 3;
106+
PackageVersion sut2;
107+
sut2.major = 2;
108+
sut2.minor = 2;
109+
sut2.patch = 3;
110+
EXPECT_LT(sut1, sut2);
111+
EXPECT_FALSE(sut2 < sut1);
112+
sut2.major = sut1.major;
113+
sut2.minor = 3;
114+
EXPECT_LT(sut1, sut2);
115+
EXPECT_FALSE(sut2 < sut1);
116+
sut2.minor = sut1.minor;
117+
sut2.patch = 4;
118+
EXPECT_LT(sut1, sut2);
119+
EXPECT_FALSE(sut2 < sut1);
120+
sut2.patch = sut1.patch;
121+
EXPECT_FALSE(sut1 < sut2);
122+
EXPECT_FALSE(sut2 < sut1);
123+
}
124+
125+
TEST(VersionTest, version_numbers_ostream_insertion_produces_version_string) {
126+
std::stringstream sstr;
127+
PackageVersion sut;
128+
sut.major = 0;
129+
sut.minor = 0;
130+
sut.patch = 0;
131+
sstr << sut;
132+
ASSERT_FALSE(sstr.fail());
133+
EXPECT_STREQ(sstr.str().c_str(), "0.0.0");
134+
sstr = std::stringstream {};
135+
sut.major = 22;
136+
sut.minor = 4;
137+
sut.patch = 102;
138+
sstr << sut;
139+
ASSERT_FALSE(sstr.fail());
140+
EXPECT_STREQ(sstr.str().c_str(), "22.4.102");
141+
}
142+
143+
} // namespace

0 commit comments

Comments
 (0)