Skip to content

Commit

Permalink
iox-#180 Fix Bazel and GCC5 build of 'iox::span'
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Hoinkis <simon.hoinkis@apex.ai>
  • Loading branch information
mossmaurice committed Apr 17, 2023
1 parent 0ebe3b3 commit 7627919
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
5 changes: 4 additions & 1 deletion iceoryx_dust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ cc_library(
"source/**/*.hpp",
]),
hdrs = glob(["include/**"] + glob(["vocabulary/**"])),
includes = ["vocabulary/include/", "include"],
includes = [
"include",
"vocabulary/include/",
],
visibility = ["//visibility:public"],
deps = ["//iceoryx_hoofs"],
)
14 changes: 8 additions & 6 deletions iceoryx_dust/test/moduletests/test_vocabulary_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ TEST(span_test, CheckConstexprIterOfSpan)
static constexpr int arr[] = {1, 6, 1, 8, 0};
constexpr span<const int> span(arr);

static_assert(1 == span.begin()[0], "First element needs to be '1'");
static_assert(1 == *(span.begin() += 0), "First element needs to be '1'");
static_assert(6 == *(span.begin() += 1), "Second element needs to be '6'");

static_assert(1 == *((span.begin() + 1) -= 1), "First element needs to be '1'");
static_assert(6 == *((span.begin() + 1) -= 0), "Second element needs to be '6'");
// Explicitly not use EXPECT_TRUE here to be able to execute the test case during compile-time
// 'static_assert' is not possible as not being supported with GCC5
assert(1 == span.begin()[0]); // First element needs to be '1'
assert(1 == *(span.begin() += 0)); // First element needs to be '1'
assert(6 == *(span.begin() += 1)); // Second element needs to be '6'

assert(1 == *((span.begin() + 1) -= 1)); // First element needs to be '1'
assert(6 == *((span.begin() + 1) -= 0)); // Second element needs to be '6'
}

TEST(span_test, GetSpanDataAsWritableBytes)
Expand Down
9 changes: 4 additions & 5 deletions iceoryx_dust/vocabulary/include/iox/detail/span.inl
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ inline constexpr span<T, Extent>::span(It first, uint64_t count) noexcept
: span_storage_t(count)
, m_data(internal::to_address(first))
{
iox::ConstexprCheckTrue(Extent == DYNAMIC_EXTENT || Extent == count);
assert(Extent == DYNAMIC_EXTENT || Extent == count);
}

template <typename T, uint64_t Extent>
template <typename It, typename End, typename>
inline constexpr span<T, Extent>::span(It begin, End end) noexcept
: span(begin, static_cast<uint64_t>(end - begin))
{
// check for non negative result
static_assert(begin <= end, "");
static_assert(begin <= end, "begin shall not be smaller than end");
}

template <typename T, uint64_t Extent>
Expand Down Expand Up @@ -163,7 +162,7 @@ template <typename T, uint64_t Extent>
inline constexpr T& span<T, Extent>::front() const noexcept
{
static_assert(Extent == DYNAMIC_EXTENT || Extent > 0, "Extent must not be 0");
iox::ConstexprCheckTrue(Extent != DYNAMIC_EXTENT || !empty());
assert(Extent != DYNAMIC_EXTENT || !empty());
return *data();
}

Expand All @@ -178,7 +177,7 @@ inline constexpr T& span<T, Extent>::back() const noexcept
template <typename T, uint64_t Extent>
inline constexpr T& span<T, Extent>::operator[](uint64_t index) const noexcept
{
iox::ConstexprCheckTrue(index < size());
assert(index < size());
return *(data() + index);
}

Expand Down
44 changes: 20 additions & 24 deletions iceoryx_dust/vocabulary/include/iox/detail/span_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
#ifndef IOX_DUST_VOCABULARY_SPAN_ITERATOR_HPP
#define IOX_DUST_VOCABULARY_SPAN_ITERATOR_HPP

#include <iterator> // for reverse_iterator, distance, random_access_...

#include "iceoryx_hoofs/cxx/requires.hpp"

#include <iterator> // for reverse_iterator, distance, random_access_...
#include <cassert>

namespace iox
{
/// @todo Replace this with 'if constexpr(..)' when moving to C++17
inline constexpr bool ConstexprCheckTrue(bool condition)
{
return condition || false;
}

template <typename T>
class span_iterator
Expand Down Expand Up @@ -64,21 +60,21 @@ class span_iterator

constexpr reference operator*() const noexcept
{
ConstexprCheckTrue(m_begin && m_end);
ConstexprCheckTrue(m_begin <= m_current && m_current < m_end);
assert(m_begin && m_end);
assert(m_begin <= m_current && m_current < m_end);
return *m_current;
}

constexpr pointer operator->() const noexcept
{
ConstexprCheckTrue(m_begin && m_end);
ConstexprCheckTrue(m_begin <= m_current && m_current < m_end);
assert(m_begin && m_end);
assert(m_begin <= m_current && m_current < m_end);
return m_current;
}
constexpr span_iterator& operator++() noexcept
{
ConstexprCheckTrue(m_begin && m_current && m_end);
ConstexprCheckTrue(m_current < m_end);
assert(m_begin && m_current && m_end);
assert(m_current < m_end);

++m_current;
return *this;
Expand All @@ -93,8 +89,8 @@ class span_iterator

constexpr span_iterator& operator--() noexcept
{
ConstexprCheckTrue(m_begin && m_end);
ConstexprCheckTrue(m_begin < m_current);
assert(m_begin && m_end);
assert(m_begin < m_current);
--m_current;
return *this;
}
Expand All @@ -110,15 +106,15 @@ class span_iterator
{
if (n != 0)
{
ConstexprCheckTrue(m_begin && m_current && m_end);
assert(m_begin && m_current && m_end);
}
if (n > 0)
{
ConstexprCheckTrue(m_end - m_current >= n);
assert(m_end - m_current >= n);
}
if (n < 0)
{
ConstexprCheckTrue(m_current - m_begin >= -n);
assert(m_current - m_begin >= -n);
}

m_current += n;
Expand All @@ -141,15 +137,15 @@ class span_iterator
{
if (n != 0)
{
ConstexprCheckTrue(m_begin && m_current && m_end);
assert(m_begin && m_current && m_end);
}
if (n > 0)
{
ConstexprCheckTrue(m_current - m_begin >= n);
assert(m_current - m_begin >= n);
}
if (n < 0)
{
ConstexprCheckTrue(m_end - m_current >= -n);
assert(m_end - m_current >= -n);
}
m_current -= n;
return *this;
Expand All @@ -165,7 +161,7 @@ class span_iterator
template <class Type2, std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
{
ConstexprCheckTrue(m_begin == rhs.m_begin && m_end == rhs.m_end);
assert(m_begin == rhs.m_begin && m_end == rhs.m_end);
return m_current - rhs.m_current;
}

Expand All @@ -177,7 +173,7 @@ class span_iterator
template <class Type2, std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
{
ConstexprCheckTrue(m_begin == rhs.m_begin && m_end == rhs.m_end);
assert(m_begin == rhs.m_begin && m_end == rhs.m_end);
return m_current == rhs.m_current;
}

Expand All @@ -190,7 +186,7 @@ class span_iterator
template <class Type2, std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
{
ConstexprCheckTrue(m_begin == rhs.m_begin && m_end == rhs.m_end);
assert(m_begin == rhs.m_begin && m_end == rhs.m_end);
return m_current < rhs.m_current;
}

Expand Down
3 changes: 2 additions & 1 deletion iceoryx_dust/vocabulary/include/iox/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
#define IOX_DUST_VOCABULARY_SPAN_HPP

#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iox/detail/span_iterator.hpp"
#include "iox/type_traits.hpp"
#include "iox/uninitialized_array.hpp"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <limits>
Expand Down

0 comments on commit 7627919

Please sign in to comment.