Skip to content

Commit

Permalink
Added type_id() & type_size() methods to the `cetl::unbounded_var…
Browse files Browse the repository at this point in the history
…iant`. #verification #docs #sonar (#127)
  • Loading branch information
serges147 authored Jun 10, 2024
1 parent 6809326 commit 5aa0a5b
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 98 deletions.
100 changes: 100 additions & 0 deletions cetlvast/include/cetlvast/helpers_rtti.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT

#ifndef CETLVAST_HELPERS_RTTI_HPP
#define CETLVAST_HELPERS_RTTI_HPP

#include "cetl/rtti.hpp"

#include <complex>
#include <cstdint>
#include <functional>
#include <string>
#include <vector>

namespace cetl
{

// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)

// 6B02B2B9-610B-414E-9304-E7FC5BC0D061
template <>
constexpr type_id type_id_getter<bool>() noexcept
{
return {0x6B, 0x02, 0xB2, 0xB9, 0x61, 0x0B, 0x41, 0x4E, 0x93, 0x04, 0xE7, 0xFC, 0x5B, 0xC0, 0xD0, 0x61};
}
// AA3F7C4D-0E44-43CB-AB4C-2AE19E646F91
template <>
constexpr type_id type_id_getter<int>() noexcept
{
return {0xAA, 0x3F, 0x7C, 0x4D, 0x0E, 0x44, 0x43, 0xCB, 0xAB, 0x4C, 0x2A, 0xE1, 0x9E, 0x64, 0x6F, 0x91};
}
// 42844900-45ED-41A0-AA63-D6A42B60B343
template <>
constexpr type_id type_id_getter<float>() noexcept
{
return {0x42, 0x84, 0x49, 0x00, 0x45, 0xED, 0x41, 0xA0, 0xAA, 0x63, 0xD6, 0xA4, 0x2B, 0x60, 0xB3, 0x43};
}
// 6B5BE490-194C-4E2E-B8DE-3BB15CC52777
template <>
constexpr type_id type_id_getter<double>() noexcept
{
return {0x6B, 0x5B, 0xE4, 0x90, 0x19, 0x4C, 0x4E, 0x2E, 0xB8, 0xDE, 0x3B, 0xB1, 0x5C, 0xC5, 0x27, 0x77};
}

// 05855903-D323-41C3-8C58-691E035507D8
template <>
constexpr type_id type_id_getter<char>() noexcept
{
return {0x05, 0x85, 0x59, 0x03, 0xD3, 0x23, 0x41, 0xC3, 0x8C, 0x58, 0x69, 0x1E, 0x03, 0x55, 0x07, 0xD8};
}
// 6BC0579E-B665-480A-AFB0-45DB755A143E
template <>
constexpr type_id type_id_getter<std::uint8_t>() noexcept
{
return {0x6B, 0xC0, 0x57, 0x9E, 0xB6, 0x65, 0x48, 0x0A, 0xAF, 0xB0, 0x45, 0xDB, 0x75, 0x5A, 0x14, 0x3E};
}
// 3C22EF31-63C0-4710-9AAE-966E89134C19
template <>
constexpr type_id type_id_getter<std::uint16_t>() noexcept
{
return {0x3C, 0x22, 0xEF, 0x31, 0x63, 0xC0, 0x47, 0x10, 0x9A, 0xAE, 0x96, 0x6E, 0x89, 0x13, 0x4C, 0x19};
}
// 89A2F7BC-5BEA-47BF-96C4-CFFA3A2DBBB2
template <>
constexpr type_id type_id_getter<std::uint32_t>() noexcept
{
return {0x89, 0xA2, 0xF7, 0xBC, 0x5B, 0xEA, 0x47, 0xBF, 0x96, 0xC4, 0xCF, 0xFA, 0x3A, 0x2D, 0xBB, 0xB2};
}
// A0672C3A-C6D2-4BF5-990A-1A4601264D60
template <>
constexpr type_id type_id_getter<std::string>() noexcept
{
return {0xA0, 0x67, 0x2C, 0x3A, 0xC6, 0xD2, 0x4B, 0xF5, 0x99, 0x0A, 0x1A, 0x46, 0x01, 0x26, 0x4D, 0x60};
}
// 473A0E53-86AB-4426-9F32-732D519F940D
template <>
constexpr type_id type_id_getter<std::complex<double>>() noexcept
{
return {0x47, 0x3A, 0x0E, 0x53, 0x86, 0xAB, 0x44, 0x26, 0x9F, 0x32, 0x73, 0x2D, 0x51, 0x9F, 0x94, 0x0D};
}
// D30E9194-8ECB-4831-9B31-F73C031DBFFB
template <>
constexpr type_id type_id_getter<std::function<const char*()>>() noexcept
{
return {0xD3, 0x0E, 0x91, 0x94, 0x8E, 0xCB, 0x48, 0x31, 0x9B, 0x31, 0xF7, 0x3C, 0x03, 0x1D, 0xBF, 0xFB};
}
// 63E796F8-AAFC-4E61-B545-99CE28B796FD
template <>
constexpr type_id type_id_getter<std::vector<char>>() noexcept
{
return {0x63, 0xE7, 0x96, 0xF8, 0xAA, 0xFC, 0x4E, 0x61, 0xB5, 0x45, 0x99, 0xCE, 0x28, 0xB7, 0x96, 0xFD};
}

// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)

} // namespace cetl

#endif // CETLVAST_HELPERS_RTTI_HPP
2 changes: 1 addition & 1 deletion cetlvast/suites/compile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set(ALL_TESTS_RUN "")

foreach(COMPILE_TEST ${COMPILE_TESTS})
define_compile_failure_test(TEST_SOURCE ${COMPILE_TEST}
EXTRA_TEST_LIBS cetl
EXTRA_TEST_LIBS cetl cetlvast
OUT_TEST_BUILD_TARGET COMPILE_TEST_BUILD_TARGET
OUT_TEST_PRECHECK_TARGET COMPILE_TEST_PRECHECK_TARGET)
list(APPEND ALL_TESTS_BUILD "${COMPILE_TEST_BUILD_TARGET}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
///

#include "cetl/unbounded_variant.hpp"
#include "cetlvast/helpers_rtti.hpp"

#include <cstdint>

namespace cetl
{
template <>
constexpr type_id type_id_value<uint8_t>{};

template <>
constexpr type_id type_id_value<uint16_t>{};

} // namespace cetl

int main()
{
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
///

#include "cetl/unbounded_variant.hpp"
#include "cetlvast/helpers_rtti.hpp"

#include <cstdint>

namespace cetl
{
template <>
constexpr type_id type_id_value<uint8_t>{};

template <>
constexpr type_id type_id_value<uint16_t>{};

} // namespace cetl

int main()
{
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;
Expand Down
11 changes: 1 addition & 10 deletions cetlvast/suites/compile/test_unbounded_variant_footprint_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
///

#include "cetl/unbounded_variant.hpp"
#include "cetlvast/helpers_rtti.hpp"

#include <cstdint>

namespace cetl
{
template <>
constexpr type_id type_id_value<uint8_t>{};

template <>
constexpr type_id type_id_value<uint16_t>{};

} // namespace cetl

int main()
{
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;
Expand Down
21 changes: 17 additions & 4 deletions cetlvast/suites/docs/examples/example_10_unbounded_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@
//! [example_10_unbounded_variant_type_id]
namespace cetl
{

template <>
constexpr type_id type_id_value<bool> = {1};
constexpr type_id type_id_getter<bool>() noexcept
{
return {1};
}
template <>
constexpr type_id type_id_value<int> = {2};
constexpr type_id type_id_getter<int>() noexcept
{
return {2};
}
template <>
constexpr type_id type_id_value<float> = {3};
constexpr type_id type_id_getter<float>() noexcept
{
return {3};
}
template <>
constexpr type_id type_id_value<double> = {4};
constexpr type_id type_id_getter<double>() noexcept
{
return {4};
}

} // namespace cetl
//! [example_10_unbounded_variant_type_id]
Expand Down
83 changes: 40 additions & 43 deletions cetlvast/suites/unittest/test_unbounded_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/// SPDX-License-Identifier: MIT

#include <cetl/unbounded_variant.hpp>
#include <cetlvast/helpers_rtti.hpp>
#include <cetlvast/memory_resource_mock.hpp>
#include <cetlvast/tracking_memory_resource.hpp>

Expand All @@ -28,6 +29,7 @@ using cetl::get_if;
using cetl::make_unbounded_variant;
using cetl::type_id;
using cetl::type_id_type;
using cetl::type_id_value;
using cetl::rtti_helper;

using testing::_;
Expand Down Expand Up @@ -94,7 +96,7 @@ struct side_effect_stats
}
};

struct MyBase : rtti_helper<type_id_type<0x0>>
struct MyBase : rtti_helper<type_id_type<0x1, 0x0>>
{
char payload_;
int value_ = 0;
Expand Down Expand Up @@ -186,7 +188,7 @@ struct MyCopyableOnly final : MyBase

static constexpr type_id _get_type_id_() noexcept
{
return {0x0, 0b01};
return {0x1, 0b01};
}

CETL_NODISCARD void* _cast_(const type_id& id) & noexcept override
Expand Down Expand Up @@ -225,7 +227,7 @@ struct MyMovableOnly final : MyBase

static constexpr type_id _get_type_id_() noexcept
{
return {0x0, 0b10};
return {0x1, 0b10};
}

CETL_NODISCARD void* _cast_(const type_id& id) & noexcept override
Expand Down Expand Up @@ -263,7 +265,7 @@ struct MyCopyableAndMovable final : MyBase

static constexpr type_id _get_type_id_() noexcept
{
return {0x0, 0b11};
return {0x1, 0b11};
}

CETL_NODISCARD void* _cast_(const type_id& id) & noexcept override
Expand Down Expand Up @@ -382,6 +384,9 @@ TEST_F(TestPmrUnboundedVariant, cppref_example)

TEST_F(TestPmrUnboundedVariant, ctor_1_default)
{
EXPECT_THAT(unbounded_variant<1>{}.type_size(), 0UL);
EXPECT_THAT(unbounded_variant<1>{}.type_id(), type_id_value<void>);

EXPECT_FALSE((unbounded_variant<1>{}.has_value()));
EXPECT_FALSE((unbounded_variant<1, false>{}.has_value()));
EXPECT_FALSE((unbounded_variant<1, false, true>{}.has_value()));
Expand All @@ -397,6 +402,9 @@ TEST_F(TestPmrUnboundedVariant, ctor_1_default)

TEST_F(TestPmrUnboundedVariant, ctor_1_default_pmr)
{
EXPECT_THAT((unbounded_variant<0, true, true, 8, pmr>{get_mr()}.type_size()), 0UL);
EXPECT_THAT((unbounded_variant<0, true, true, 8, pmr>{get_mr()}.type_id()), type_id_value<void>);

EXPECT_FALSE((unbounded_variant<0, false, false, 8, pmr>{get_mr()}.has_value()));
EXPECT_FALSE((unbounded_variant<0, false, true, 8, pmr>{get_mr()}.has_value()));
EXPECT_FALSE((unbounded_variant<0, true, false, 8, pmr>{get_mr()}.has_value()));
Expand All @@ -420,13 +428,23 @@ TEST_F(TestPmrUnboundedVariant, ctor_2_copy)
using ub_var = unbounded_variant<sizeof(int)>;

const ub_var src{42};
ub_var dst{src};
EXPECT_THAT(src.type_size(), sizeof(int));
EXPECT_THAT(src.type_id(), type_id_value<int>);

ub_var dst{src};
EXPECT_THAT(src.type_size(), sizeof(int));
EXPECT_THAT(src.type_id(), type_id_value<int>);
EXPECT_THAT(dst.type_size(), sizeof(int));
EXPECT_THAT(dst.type_id(), type_id_value<int>);

EXPECT_THAT(get<int>(src), 42);
EXPECT_THAT(get<int>(dst), 42);

const ub_var empty{};
ub_var dst2{empty};
EXPECT_THAT(empty.type_size(), 0);
EXPECT_THAT(empty.type_id(), type_id_value<void>);

ub_var dst2{empty};
EXPECT_THAT(dst2.has_value(), false);
dst2 = {};
EXPECT_THAT(dst2.has_value(), false);
Expand Down Expand Up @@ -1011,6 +1029,8 @@ TEST_F(TestPmrUnboundedVariant, get_if_polymorphic)
auto side_effects = stats.make_side_effect_fn();

ub_var test_ubv = MyCopyableAndMovable{'Y', side_effects};
EXPECT_THAT(test_ubv.type_size(), sizeof(MyCopyableAndMovable));
EXPECT_THAT(test_ubv.type_id(), type_id_value<MyCopyableAndMovable>);

auto& test_base1 = get<const MyBase&>(test_ubv);
EXPECT_THAT(test_base1.payload_, 'Y');
Expand All @@ -1020,6 +1040,8 @@ TEST_F(TestPmrUnboundedVariant, get_if_polymorphic)
EXPECT_THAT(get_if<MyMovableOnly>(&test_ubv), IsNull());

test_ubv = MyBase{'X', side_effects};
EXPECT_THAT(test_ubv.type_size(), sizeof(MyBase));
EXPECT_THAT(test_ubv.type_id(), type_id_value<MyBase>);

auto& test_base2 = get<const MyBase&>(test_ubv);
EXPECT_THAT(test_base2.payload_, 'X');
Expand Down Expand Up @@ -1166,6 +1188,8 @@ TEST_F(TestPmrUnboundedVariant, emplace_1_ctor_exception)

EXPECT_THAT(t.has_value(), false);
EXPECT_THAT(t.valueless_by_exception(), true);
EXPECT_THAT(t.type_size(), 0);
EXPECT_THAT(t.type_id(), type_id_value<void>);
EXPECT_THAT(stats.constructs, 1);
EXPECT_THAT(stats.destructs, 0);
t.reset();
Expand Down Expand Up @@ -1402,6 +1426,8 @@ TEST_F(TestPmrUnboundedVariant, pmr_with_footprint_move_value_when_out_of_memory
#endif
EXPECT_THAT(dst.has_value(), false);
EXPECT_THAT(dst.valueless_by_exception(), true);
EXPECT_THAT(dst.type_size(), 0);
EXPECT_THAT(dst.type_id(), type_id_value<void>);
EXPECT_THAT(stats.ops, "@");
}
EXPECT_THAT(stats.constructs, stats.destructs);
Expand Down Expand Up @@ -1726,44 +1752,15 @@ namespace cetl
{

template <>
constexpr type_id type_id_value<bool> = {1};

template <>
constexpr type_id type_id_value<int> = {2};

template <>
constexpr type_id type_id_value<float> = {3};

template <>
constexpr type_id type_id_value<double> = {4};

template <>
constexpr type_id type_id_value<char> = {5};

template <>
constexpr type_id type_id_value<std::string> = {6};

template <>
constexpr type_id type_id_value<uint16_t> = {7};

template <>
constexpr type_id type_id_value<std::unique_ptr<MyCopyableAndMovable>> =
{0xB3, 0xB8, 0x4E, 0xC1, 0x1F, 0xE4, 0x49, 0x35, 0x9E, 0xC9, 0x1A, 0x77, 0x7B, 0x82, 0x53, 0x25};

template <>
constexpr type_id type_id_value<std::complex<double>> = {8};

template <>
constexpr type_id type_id_value<std::function<const char*()>> = {9};

template <>
constexpr type_id type_id_value<Empty> = {10};

template <>
constexpr type_id type_id_value<std::uint32_t> = {11};

constexpr type_id type_id_getter<std::unique_ptr<MyCopyableAndMovable>>() noexcept
{
return {0xB3, 0xB8, 0x4E, 0xC1, 0x1F, 0xE4, 0x49, 0x35, 0x9E, 0xC9, 0x1A, 0x77, 0x7B, 0x82, 0x53, 0x25};
}
template <>
constexpr type_id type_id_value<std::vector<char>> = {12};
constexpr type_id type_id_getter<Empty>() noexcept
{
return {0xD5, 0x62, 0x39, 0x66, 0x90, 0x8B, 0x4F, 0x56, 0x8F, 0x2A, 0x2F, 0x4F, 0xDF, 0x3F, 0x31, 0x5B};
}

} // namespace cetl

Expand Down
Loading

0 comments on commit 5aa0a5b

Please sign in to comment.