Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert dynamic casts to static casts #831

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 16 additions & 35 deletions rai/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

#include <boost/endian/conversion.hpp>

/** Compare blocks, first by type, then content. This is an optimization over dynamic_cast, which is very slow on some platforms. */
namespace
{
template <typename T>
bool blocks_equal (T const & first, rai::block const & second)
{
static_assert (std::is_base_of<rai::block, T>::value, "Input parameter is not a block type");
return (first.type () == second.type ()) && (static_cast<T const &> (second)) == first;
}
}

std::string rai::to_string_hex (uint64_t value_a)
{
std::stringstream stream;
Expand Down Expand Up @@ -263,13 +274,7 @@ hashables (error_a, tree_a)

bool rai::send_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast<rai::send_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
result = *this == *other_l;
}
return result;
return blocks_equal (*this, other_a);
}

bool rai::send_block::valid_predecessor (rai::block const & block_a) const
Expand Down Expand Up @@ -547,13 +552,7 @@ rai::block_type rai::open_block::type () const

bool rai::open_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast<rai::open_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
result = *this == *other_l;
}
return result;
return blocks_equal (*this, other_a);
}

bool rai::open_block::operator== (rai::open_block const & other_a) const
Expand Down Expand Up @@ -776,13 +775,7 @@ rai::block_type rai::change_block::type () const

bool rai::change_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast<rai::change_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
result = *this == *other_l;
}
return result;
return blocks_equal (*this, other_a);
}

bool rai::change_block::operator== (rai::change_block const & other_a) const
Expand Down Expand Up @@ -1094,13 +1087,7 @@ rai::block_type rai::state_block::type () const

bool rai::state_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast<rai::state_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
result = *this == *other_l;
}
return result;
return blocks_equal (*this, other_a);
}

bool rai::state_block::operator== (rai::state_block const & other_a) const
Expand Down Expand Up @@ -1417,13 +1404,7 @@ void rai::receive_block::block_work_set (uint64_t work_a)

bool rai::receive_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast<rai::receive_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
result = *this == *other_l;
}
return result;
return blocks_equal (*this, other_a);
}

bool rai::receive_block::valid_predecessor (rai::block const & block_a) const
Expand Down