Skip to content

Also first revision of any methods, functions and tests - without RTTI yet. - #102

Merged
serges147 merged 16 commits into
issue/83_anyfrom
sshirokov/83_any
Mar 20, 2024
Merged

serges147 merged 16 commits into
issue/83_anyfrom
sshirokov/83_any

Conversation

@serges147

@serges147 serges147 commented Mar 18, 2024 •

Copy link
Copy Markdown
Contributor
  • Implemented most of constructors (close to std::any).
  • Added implementation of 3 operator=-s.
  • Added first 2 overloads of any_cast.
  • Added make_any overloads.
  • Implemented has_value, emplace, swap & reset.
  • cetl::bad_any_cast now alias to cetl::pf17::bad_any_cast .

Also:

  • Appended is_in_place_type_v and is_in_place_index_v constexpr-s.
  • Applied clang format to utility.hpp header file.

Related to #83

@serges147 serges147 self-assigned this Mar 18, 2024
Comment thread include/cetl/pf17/any.hpp

/// A polyfill for `std::bad_any_cast`.
/// This is only available if exceptions are enabled (`__cpp_exceptions` is defined).
class bad_any_cast : public std::bad_cast

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense that this class is here, as it polyfills std::bad_any_cast, but I wonder if it's going to be a problem later on when we need to add a specialization of our cetl::any<> into this file, because that would require this header to depend on cetl/any.hpp, which creates a recursive dependency.

If this is a valid concern, we might be better off moving everything that is needed for cetl/any.hpp into its own header.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I though about it, but decided to tackle this when I start working on the specialization. Is it fine to postpone?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Comment thread include/cetl/any.hpp
{
case detail::action::Get:

return get(const_cast<any&>(*self));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const_cast violates AUTOSAR A5-2-3

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will eliminate all these const_cast-s when reworking the handle thing (and implementing Copyable/Movable constraints). is it ok to postpone to near future pr?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Comment thread include/cetl/any.hpp
Copy,
Move,
Destroy
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is fine but really cool kids from the block use algebraic types for this these days, because it allows one to express more concepts through the type system:

struct action_get{};
struct action_copy{ any* other; };  // notice the `other`!
struct action_move{ any* other; };  // it is introduced only when needed
struct action_destroy{};
using action = variant<action_get, action_copy, action_move, action_destroy>;

Then you use visit to handle action in a type-safe way without the need to introduce unused parameters.

Just saying.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think about it. Also, maybe when I start working on Copyable/Movable constraints then it's possible that enum and switch won't be applicable anymore...

Comment thread include/cetl/any.hpp
{
static_assert(sizeof(Tp) <= Footprint, "Enlarge the footprint");

static void* handle(detail::action action, const any* self, any* other)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I read it, non-const function parameters that are not mutated violate AUTOSAR A7-1-1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my answer for line 218 plz

Comment thread include/cetl/any.hpp

case detail::action::Copy:

copy(*other, *self);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this yesterday in the evening but just for historical purposes I want to mention it here again that this code will break if Tp is non-copyable. Same goes for moving. This is why we need these telescoping bases here that select the copy/move behaviors depending on the template parameters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll work on that in the next PR (if it's ok with you)

Comment thread include/cetl/any.hpp Outdated
Comment thread cetlvast/suites/unittest/test_any.cpp Outdated
{
using uut = any<sizeof(int)>;

const uut srcAny{42};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applies in other places as well.

Suggested change
const uut srcAny{42};
const uut src_any{42};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavel-kirienko is it fine if I fix this in separate pr? it will be just "cosmetic" pr without functional changes.

@pavel-kirienko pavel-kirienko Mar 19, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we fix this now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we can - I just though it will be easier review separately "cosmetics" ONLY changes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes a lot of difference here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll do it now, thnx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done - see last commit 2ffc005

@sonarqubecloud

Copy link
Copy Markdown

Comment thread include/cetl/any.hpp
template <std::size_t Footprint, bool Copyable>
struct base_copy;

template <std::size_t Footprint, std::size_t Align = sizeof(std::max_align_t)>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alignment should be also configurable via the list of template parameters to any:

template <std::size_t Footprint,
          bool Copyable = true,
          bool Movable = Copyable,
          std::size_t Align = alignof(std::max_align_t)>

I neglected to mentioned this explicitly earlier. Also note that alignof should be used instead of sizeof.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align configurable will be ready (and tested) after next pr (already ready in my separate private branch).

@serges147
serges147 merged commit 13c533d into issue/83_any Mar 20, 2024
@serges147
serges147 deleted the sshirokov/83_any branch March 20, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants