Skip to content

Commit

Permalink
#807 Change in legacy etl::bitset with nullptr construction + error h…
Browse files Browse the repository at this point in the history
…andling

Harmonised operation of legacy and new etl::bitset
Added ETL_ASSERT checks for nullptr buffer pointers for etl::bitset_ext
  • Loading branch information
John Wellbelove committed Dec 16, 2023
1 parent 8f88aa0 commit 59cd9e6
Show file tree
Hide file tree
Showing 11 changed files with 882 additions and 66 deletions.
42 changes: 33 additions & 9 deletions include/etl/private/bitset_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,14 @@ namespace etl
//*************************************************************************
ibitset& set(const char* text)
{
from_string(text);
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}

return *this;
}
Expand All @@ -439,7 +446,14 @@ namespace etl
//*************************************************************************
ibitset& set(const wchar_t* text)
{
from_string(text);
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}

return *this;
}
Expand All @@ -449,7 +463,14 @@ namespace etl
//*************************************************************************
ibitset& set(const char16_t* text)
{
from_string(text);
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}

return *this;
}
Expand All @@ -459,7 +480,14 @@ namespace etl
//*************************************************************************
ibitset& set(const char32_t* text)
{
from_string(text);
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}

return *this;
}
Expand Down Expand Up @@ -512,7 +540,7 @@ namespace etl
//*************************************************************************
ibitset& reset()
{
::memset(pdata, 0x00, Number_Of_Elements);
::memset(pdata, 0, Number_Of_Elements * sizeof(element_type));

return *this;
}
Expand Down Expand Up @@ -1194,7 +1222,6 @@ namespace etl
//*************************************************************************
bitset<MaxN>& set(const char* text)
{
ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
etl::ibitset::set(text);

return *this;
Expand All @@ -1205,7 +1232,6 @@ namespace etl
//*************************************************************************
bitset<MaxN>& set(const wchar_t* text)
{
ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
etl::ibitset::set(text);

return *this;
Expand All @@ -1216,7 +1242,6 @@ namespace etl
//*************************************************************************
bitset<MaxN>& set(const char16_t* text)
{
ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
etl::ibitset::set(text);

return *this;
Expand All @@ -1227,7 +1252,6 @@ namespace etl
//*************************************************************************
bitset<MaxN>& set(const char32_t* text)
{
ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
etl::ibitset::set(text);

return *this;
Expand Down
63 changes: 46 additions & 17 deletions include/etl/private/bitset_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ namespace etl
}
};

//***************************************************************************
/// Bitset nullptr buffer exception.
///\ingroup bitset
//***************************************************************************
class bitset_invalid_buffer : public bitset_exception
{
public:

bitset_invalid_buffer(string_type file_name_, numeric_type line_number_)
: bitset_exception(ETL_ERROR_TEXT("bitset:invalid buffer", ETL_BITSET_FILE_ID"D"), file_name_, line_number_)
{
}
};

//*************************************************************************
/// The implementation class for multi-element etl::bitset
///\ingroup bitset
Expand Down Expand Up @@ -2598,16 +2612,17 @@ namespace etl
//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 explicit bitset_ext(element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = All_Clear_Element;
}

//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(buffer_type& buffer) ETL_NOEXCEPT
ETL_CONSTEXPR14 explicit bitset_ext(buffer_type& buffer) ETL_NOEXCEPT
: pbuffer(&buffer)
{
*pbuffer = All_Clear_Element;
Expand All @@ -2616,9 +2631,10 @@ namespace etl
//*************************************************************************
/// Construct copy.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, true>& other, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, true>& other, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = *other.pbuffer;
}

Expand All @@ -2639,9 +2655,10 @@ namespace etl
//*************************************************************************
/// Construct from a value.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = element_type(value);
}

Expand All @@ -2657,9 +2674,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = All_Clear_Element;
set(text);
}
Expand All @@ -2677,9 +2695,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = All_Clear_Element;
set(text);
}
Expand All @@ -2697,9 +2716,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = All_Clear_Element;
set(text);
}
Expand All @@ -2717,9 +2737,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
*pbuffer = All_Clear_Element;
set(text);
}
Expand Down Expand Up @@ -2963,7 +2984,7 @@ namespace etl
//*************************************************************************
template <typename T>
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value, T>::type
typename etl::enable_if<etl::is_integral<T>::value, T>::type
value() const ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
Expand Down Expand Up @@ -3476,27 +3497,30 @@ namespace etl
//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 explicit bitset_ext(element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
reset();
}

//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(buffer_type& buffer) ETL_NOEXCEPT
ETL_CONSTEXPR14 explicit bitset_ext(buffer_type& buffer)
: pbuffer(buffer.data())
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
reset();
}

//*************************************************************************
/// Construct copy.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, false>& other, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, false>& other, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
etl::copy_n(other.pbuffer, Number_Of_Elements, pbuffer);
}

Expand All @@ -3517,9 +3541,10 @@ namespace etl
//*************************************************************************
/// Construct from a value.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
ibitset.initialise(pbuffer, Number_Of_Elements, value);
clear_unused_bits_in_msb();
}
Expand All @@ -3537,9 +3562,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
clear_unused_bits_in_msb();
}
Expand All @@ -3557,9 +3583,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
clear_unused_bits_in_msb();
}
Expand All @@ -3577,9 +3604,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
clear_unused_bits_in_msb();
}
Expand All @@ -3597,9 +3625,10 @@ namespace etl
//*************************************************************************
/// Construct from a string.
//*************************************************************************
ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_) ETL_NOEXCEPT
ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_)
: pbuffer(pbuffer_)
{
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
clear_unused_bits_in_msb();
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_bit_stream_reader_big_endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace etl

namespace
{
SUITE(test_bit_stream_reader)
SUITE(test_bit_stream_reader_big_endian)
{
//*************************************************************************
TEST(test_read_bool)
Expand Down
2 changes: 1 addition & 1 deletion test/test_bit_stream_reader_little_endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace etl

namespace
{
SUITE(test_bit_stream_reader)
SUITE(test_bit_stream_reader_little_endian)
{
//*************************************************************************
TEST(test_read_bool)
Expand Down
2 changes: 1 addition & 1 deletion test/test_bit_stream_writer_big_endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace
std::vector<char> data;
};

SUITE(test_bit_stream_big_endian)
SUITE(test_bit_stream_writer_big_endian)
{
//*************************************************************************
TEST(test_bit_stream_writer_construction)
Expand Down
2 changes: 1 addition & 1 deletion test/test_bit_stream_writer_little_endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace
std::vector<char> data;
};

SUITE(test_bit_stream_little_endian)
SUITE(test_bit_stream_writer_little_endian)
{
//*************************************************************************
TEST(test_bit_stream_writer_construction)
Expand Down
Loading

0 comments on commit 59cd9e6

Please sign in to comment.