Skip to content

Commit

Permalink
Merge branch 'feature/iox-#186_add_cxx_list' into feature/iox-#221_ap…
Browse files Browse the repository at this point in the history
…ply_cxx_list

# Conflicts:
#	iceoryx_utils/README.md
  • Loading branch information
bishibashiB committed Sep 15, 2020
2 parents 2257c9f + cfade35 commit bf03943
Show file tree
Hide file tree
Showing 7 changed files with 726 additions and 674 deletions.
2 changes: 1 addition & 1 deletion iceoryx_utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class should be used.
|`function_ref` | | | C++11 implementation of the next-gen C++ feature `std::function_ref` see [function_ref proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0792r2.html). It behaves like `std::function` but does not own the callable. |
|`GenericRAII` | | | This is an abstraction of the C++ RAII idiom. Sometimes you have constructs where you would like to perform a certain task on creation and then again when they are getting out of scope, this is where `GenericRAII` comes in. It is like a `std::lock_guard` or a `std::shared_ptr` but more generic. |
|`helplets` | | | Implementations of [C++ Core Guideline](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) concepts like `not_null` `Ensures` `Expects` are contained here. Additionally, we are providing some types to verify preconditions at compile time. Think of an int which has to be always greater 5, here we provide types like `greater_or_equal<int, 6>`.|
|`list` | | | Heap and exception free implementation of `std::list` |
|`list` | | | Heap and exception free, relocatable implementation of `std::list` |
|`NewType<T, Policies>`| | | C++11 implementation of [Haskells NewType-pattern](https://wiki.haskell.org/Newtype). |
|`optional` | | | C++11 implementation of the C++17 feature `std::optional` |
|`pair` | i | X | Simplistic reimplementation of an `std::pair` It maybe becomes obsolete. |
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_utils/include/iceoryx_utils/cxx/forward_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class forward_list
/// @param[in] iter is the iterator which will deliver list and index info for the const_iterator
IteratorBase(const IteratorBase<false>& iter) noexcept;

/// @brief assings a const_iterator from an iterator; needs to be implemented because the copy c'tor is also
/// @brief assigns a const_iterator from an iterator; needs to be implemented because the copy c'tor is also
/// explicitly implemented
/// @param[in] rhs is the iterator which will deliver list and index info for the const_iterator
/// @return reference to this iterator object
Expand Down
67 changes: 36 additions & 31 deletions iceoryx_utils/include/iceoryx_utils/cxx/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace cxx
///
/// overview of cxx::forward_list deviations to std::forward_list(C++11)
/// - list declaration with mandatory max list size argument
/// - memeber functions don't throw exception but will trigger different failure handling
/// - member functions don't throw exception but will trigger different failure handling
/// - push_front/~_back returns a bool (instead of void) informing on successful insertion (true)
/// - pop_front/~_back returns a bool (instead of void) informing on successful removal (true), otherwise empty
/// (false)
Expand All @@ -56,11 +56,11 @@ class list
// forward declarations, private
struct ListLink;
template <bool>
class iterator_base;
class IteratorBase;

public:
using iterator = iterator_base<false>;
using const_iterator = iterator_base<true>;
using iterator = IteratorBase<false>;
using const_iterator = IteratorBase<true>;
using value_type = T;
using size_type = decltype(Capacity);

Expand Down Expand Up @@ -168,7 +168,7 @@ class list
/// @return successful insertion (true), otherwise no element could be added to list (e.g. full -> false)
bool push_front(const T& data) noexcept;

/// @brief create element inplace at the begining of the list
/// @brief add element to the beginning of the list via move
/// @param[in] data universal reference perfectly forwarded to client class
/// @return successful insertion (true), otherwise no element could be added to list (e.g. full -> false)
bool push_front(T&& data) noexcept;
Expand All @@ -178,7 +178,7 @@ class list
/// @return successful insertion (true), otherwise no element could be added to list (e.g. full -> false)
bool push_back(const T& data) noexcept;

/// @brief create element inplace at the end of the list
/// @brief add element to the end of the list via move
/// @param[in] data universal reference perfectly forwarded to client class
/// @return successful insertion (true), otherwise no element could be added to list (e.g. full -> false)
bool push_back(T&& data) noexcept;
Expand Down Expand Up @@ -224,7 +224,7 @@ class list
template <typename... ConstructorArgs>
T& emplace_front(ConstructorArgs&&... args) noexcept;

/// @brief construct element inplace at end of list
/// @brief construct element inplace after the pointed-to element
/// @param[in] args T-typed construction parameters (initializer list)
/// @return referene to generated element, return is C++17-conform
template <typename... ConstructorArgs>
Expand All @@ -243,7 +243,7 @@ class list
/// @return iterator to the newly added element
iterator insert(const_iterator citer, const T& data) noexcept;

/// @brief construct element inplace at begining of list
/// @brief add element after the pointed-to element via move
/// @param[in] citer iterator with the position to insert after
/// @param[in] data universal reference perfectly forwarded to client class
/// @return iterator to the newly added element
Expand All @@ -252,33 +252,37 @@ class list
private:
/// @brief nested iterator class for list element operations including element access
/// comparison of iterator from different list is rejected by terminate()
template <bool is_const_iterator = true>
class iterator_base
template <bool IsConstIterator = true>
class IteratorBase
{
public:
// provide the following public types for a std::iterator compatible iterator_category interface
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename std::conditional<is_const_iterator, const T, T>::type;
using value_type = typename std::conditional<IsConstIterator, const T, T>::type;
using difference_type = void;
using pointer = typename std::conditional<is_const_iterator, const T*, T*>::type;
using reference = typename std::conditional<is_const_iterator, const T&, T&>::type;
using pointer = typename std::conditional<IsConstIterator, const T*, T*>::type;
using reference = typename std::conditional<IsConstIterator, const T&, T&>::type;


/// @brief construct a const_iterator from an iterator
/// @param[in] iter is the iterator which will deliver list and index info for the const_iterator
iterator_base(const iterator_base<false>& iter);
IteratorBase(const IteratorBase<false>& iter) noexcept;

/// @brief assigns a const_iterator from an iterator; needs to be implemented because the copy c'tor is also
/// explicitly implemented
/// @param[in] rhs is the iterator which will deliver list and index info for the const_iterator
/// @return reference to this iterator object
IteratorBase& operator=(const IteratorBase<false>& rhs) noexcept;

/// @brief prefix increment iterator, so it points to the next list element
/// when trying to increment beyond the end of the list, iterator stays pointing at the end, a
/// message is forwarded to the error_message handler / cerr stream
/// when trying to increment beyond the end of the list, iterator stays pointing at the end
/// @return reference to this iterator object
iterator_base& operator++() noexcept;
IteratorBase& operator++() noexcept;

/// @brief prefix decrement iterator, so it points to the previous list element
/// when trying to increment beyond the end of the list, iterator stays pointing at the end, a
/// message is forwarded to the error_message handler / cerr stream
/// decrementing an iterator pointing already towards begin() has no effect (iterator stays at begin())
/// @return reference to this iterator object
iterator_base& operator--() noexcept;
IteratorBase& operator--() noexcept;


/// @brief comparing list iterators for equality
Expand All @@ -287,17 +291,17 @@ class list
/// only iterators of the same parent list can be compared; in case of misuse, terminate() is invoked
/// @param[in] rhs is the 2nd iterator to compare to
/// @return list position for two iterators is the same (true) or different (false)
template <bool is_const_iterator_other>
bool operator==(const iterator_base<is_const_iterator_other>& rhs) const noexcept;
template <bool IsConstIteratorOther>
bool operator==(const IteratorBase<IsConstIteratorOther>& rhs) const noexcept;

/// @brief comparing list iterators for non-equality
/// the referenced list position is compared, not the content of the list element (T-typed)
/// -> there is no content for fictional elements at BEGIN_END_LINK_INDEX
/// only iterators of the same parent list can be compared; in case of misuse, terminate() is invoked
/// @param[in] rhs is the 2nd iterator to compare to
/// @return list position for two iterators is the same (true) or different (false)
template <bool is_const_iterator_other>
bool operator!=(const iterator_base<is_const_iterator_other>& rhs) const noexcept;
template <bool IsConstIteratorOther>
bool operator!=(const IteratorBase<IsConstIteratorOther>& rhs) const noexcept;

/// @brief dereferencing element content via iterator-position element
/// @return reference to list element data
Expand All @@ -310,18 +314,18 @@ class list

private:
using parentListPointer =
typename std::conditional<is_const_iterator, const list<T, Capacity>*, list<T, Capacity>*>::type;
typename std::conditional<IsConstIterator, const list<T, Capacity>*, list<T, Capacity>*>::type;

/// @brief private construct for an iterator, the iterator is bundled to
/// an existing parent (object) of type list,
/// an iterator is only constructed through calls to begin() or end()
/// @param[in] parent is the const list the this iterator operates on
/// @param[in] idx is the index of the list element (within allocated memory of parent list)
explicit iterator_base(parentListPointer parent, size_type idx) noexcept;
explicit IteratorBase(parentListPointer parent, size_type idx) noexcept;

// Make iterator_base<true> a friend class of iterator_base<false> so the copy constructor can access the
// Make IteratorBase<true> a friend class of IteratorBase<false> so the copy constructor can access the
// private member variables.
friend class iterator_base<true>;
friend class IteratorBase<true>;
friend class list<T, Capacity>;
parentListPointer m_list;
size_type m_iterListNodeIdx;
Expand All @@ -340,7 +344,7 @@ class list
bool isValidElementIdx(const size_type idx) const noexcept;
bool handleInvalidElement(const size_type idx) const noexcept;
bool handleInvalidIterator(const const_iterator& iter) const noexcept;
bool invalidIterOrDifferentLists(const const_iterator& iter) const noexcept;
bool isInvalidIterOrDifferentLists(const const_iterator& iter) const noexcept;
size_type& getPrevIdx(const size_type idx) noexcept;
size_type& getNextIdx(const size_type idx) noexcept;
size_type& getPrevIdx(const const_iterator& iter) noexcept;
Expand All @@ -362,8 +366,9 @@ class list
static constexpr size_type NODE_LINK_COUNT{size_type(Capacity) + 1U};
static constexpr size_type INVALID_INDEX{NODE_LINK_COUNT};

// two member variables point to head of freeList and usedList
// available elements are moved between freeList and usedList when inserted or removed
// unused/free elements are stored in an internal list (freeList), this freeList is accessed via the
// member variable m_freeListHeadIdx; user insert- and erase- operations move elements between the freeList and
// active list
size_type m_freeListHeadIdx{0U};

// m_links array is one element bigger than request element count. In this additional element links are stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ template <bool IsConstIterator>
inline typename forward_list<T, Capacity>::template IteratorBase<IsConstIterator>&
forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator=(const IteratorBase<false>& rhs) noexcept
{
if (this != &rhs)
if (reinterpret_cast<const void*>(this) != reinterpret_cast<const void*>(&rhs))
{
m_list = rhs.m_list;
m_iterListNodeIdx = rhs.m_iterListNodeIdx;
Expand Down Expand Up @@ -461,7 +461,7 @@ inline bool forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator!=
template <typename T, uint64_t Capacity>
template <bool IsConstIterator>
inline typename forward_list<T, Capacity>::template IteratorBase<IsConstIterator>::reference
forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator*() const noexcept
forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator*() const noexcept
{
return *operator->();
}
Expand All @@ -470,7 +470,7 @@ forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator*() const noex
template <typename T, uint64_t Capacity>
template <bool IsConstIterator>
inline typename forward_list<T, Capacity>::template IteratorBase<IsConstIterator>::pointer
forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator->() const noexcept
forward_list<T, Capacity>::IteratorBase<IsConstIterator>::operator->() const noexcept

{
return m_list->getDataPtrFromIdx(m_iterListNodeIdx);
Expand Down
Loading

0 comments on commit bf03943

Please sign in to comment.