Skip to content

Commit

Permalink
Merge branch 'issue/83_any' into sshirokov/83_any_no_copy_move #verif…
Browse files Browse the repository at this point in the history
…ication
  • Loading branch information
serges147 committed May 1, 2024
2 parents 804a31f + 75c4e76 commit b5ffdcc
Show file tree
Hide file tree
Showing 12 changed files with 1,299 additions and 1,339 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/// @file
/// Compile test that ensures it's impossible get "bigger" value than `Footprint` of const `any`.
/// Compile test that ensures it's impossible get "bigger" value than `Footprint` of const `unbounded_variant`.
///
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT
///

#include "cetl/any.hpp"
#include "cetl/unbounded_variant.hpp"

#include <cstdint>

Expand All @@ -23,9 +23,9 @@ constexpr type_id type_id_value<uint16_t>{};

int main()
{
using any = cetl::any<sizeof(uint8_t)>;
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;

const any test{static_cast<uint8_t>(0)};
const ub_var test{static_cast<uint8_t>(0)};

#ifndef CETLVAST_COMPILETEST_PRECHECK

Expand All @@ -34,11 +34,11 @@ int main()
// static_assert(sizeof(ValueType) <= Footprint,
// "Cannot contain the requested type since the footprint is too small");
// ```
return cetl::any_cast<uint16_t>(test);
return cetl::get<uint16_t>(test);

#else

return cetl::any_cast<uint8_t>(test);
return cetl::get<uint8_t>(test);

#endif
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/// @file
/// Compile test that ensures it's impossible set "bigger" value than `Footprint` of `any`.
/// Compile test that ensures it's impossible set "bigger" value than `Footprint` of `unbounded_variant`.
///
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT
///

#include "cetl/any.hpp"
#include "cetl/unbounded_variant.hpp"

#include <cstdint>

Expand All @@ -23,9 +23,9 @@ constexpr type_id type_id_value<uint16_t>{};

int main()
{
using any = cetl::any<sizeof(uint8_t)>;
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;

any test{static_cast<uint8_t>(0)};
ub_var test{static_cast<uint8_t>(0)};

#ifndef CETLVAST_COMPILETEST_PRECHECK

Expand All @@ -34,11 +34,11 @@ int main()
// static_assert(sizeof(ValueType) <= Footprint,
// "Cannot contain the requested type since the footprint is too small");
// ```
return cetl::any_cast<uint16_t&>(test);
return cetl::get<uint16_t&>(test);

#else

return cetl::any_cast<uint8_t&>(test);
return cetl::get<uint8_t&>(test);

#endif
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/// @file
/// Compile test that ensures it's impossible set "bigger" value than `Footprint` of `any`.
/// Compile test that ensures it's impossible set "bigger" value than `Footprint` of `unbounded_variant`.
///
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT
///

#include "cetl/any.hpp"
#include "cetl/unbounded_variant.hpp"

#include <cstdint>

Expand All @@ -23,9 +23,9 @@ constexpr type_id type_id_value<uint16_t>{};

int main()
{
using any = cetl::any<sizeof(uint8_t)>;
using ub_var = cetl::unbounded_variant<sizeof(uint8_t)>;

any test{};
ub_var test{};

#ifndef CETLVAST_COMPILETEST_PRECHECK

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/// @file
/// Example of using cetl::any.
/// Example of using cetl::unbounded_variant.
///
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT
///
#include "cetl/any.hpp"
#include "cetl/unbounded_variant.hpp"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

//! [example_10_any_type_id]
//! [example_10_unbounded_variant_type_id]
namespace cetl
{
template <>
Expand All @@ -23,23 +23,23 @@ template <>
constexpr type_id type_id_value<double> = {4};

} // namespace cetl
//! [example_10_any_type_id]
//! [example_10_unbounded_variant_type_id]

TEST(example_10_any, basic_usage)
TEST(example_10_unbounded_variant, basic_usage)
{
//! [example_10_any_basic_usage]
//! [example_10_unbounded_variant_basic_usage]
/// This example is inspired by the [cppreference.com](https://en.cppreference.com/w/cpp/utility/any)
/// documentation.
using any = cetl::any<std::max(sizeof(int), sizeof(double))>;
/// documentation, but instead of `any` it's called `unbounded_variant`.
using ub_var = cetl::unbounded_variant<std::max(sizeof(int), sizeof(double))>;

any a = 1;
EXPECT_EQ(1, cetl::any_cast<int>(a));
ub_var a = 1;
EXPECT_THAT(cetl::get<int>(a), 1);

a = 3.14;
EXPECT_EQ(3.14, cetl::any_cast<double>(a));
EXPECT_THAT(cetl::get<double>(a), 3.14);

a = true;
EXPECT_TRUE(cetl::any_cast<bool>(a));
EXPECT_TRUE(cetl::get<bool>(a));

// bad any cast
//
Expand All @@ -49,9 +49,9 @@ TEST(example_10_any, basic_usage)
// Should be used in the tests where exceptions are expected (see `EXPECT_THROW`).
const auto sink = [](auto&&) {};

EXPECT_THROW(sink(cetl::any_cast<float>(a)), cetl::bad_any_cast);
EXPECT_THROW(sink(cetl::get<float>(a)), cetl::bad_unbounded_variant_access);
#else
EXPECT_EQ(nullptr, cetl::any_cast<float>(&a));
EXPECT_THAT(cetl::get_if<float>(&a), testing::IsNull());
#endif

a = 2;
Expand All @@ -65,6 +65,6 @@ TEST(example_10_any, basic_usage)
// pointer to contained data
//
a = 3;
EXPECT_EQ(3, *cetl::any_cast<int>(&a));
//! [example_10_any_basic_usage]
EXPECT_THAT(*cetl::get_if<int>(&a), 3);
//! [example_10_unbounded_variant_basic_usage]
}
Loading

0 comments on commit b5ffdcc

Please sign in to comment.