pabigot
0.1.1
C++ support classes
|
Container used to link objects into a sequence. More...
#include </mnt/devel/pabigot-cxx/include/pabigot/container.hpp>
Data Structures | |
class | chain_iterator_type |
Iterator type designed to support range-for. More... | |
class | end_iterator_type |
Sentinal type used as end-of-chain iterator value. More... | |
Public Types | |
using | chain_type = forward_chain< T, REF_NEXT > |
The type of this chain. | |
using | value_type = T |
The type of object linked by this chain. | |
using | pointer_type = value_type * |
The type for a pointer to value_type. | |
using | predicate_type = std::function< bool(const value_type &)> |
The type for a function that tests a value_type for a condition. | |
Public Member Functions | |
bool | is_unlinked (pointer_type ptr) const noexcept |
Test whether a pointer is unlinked. | |
bool | is_unlinked (value_type &value) const noexcept |
Test whether a value is unlinked. | |
forward_chain (const forward_chain &)=delete | |
forward_chain & | operator= (const forward_chain &)=delete |
forward_chain (forward_chain &&from) noexcept | |
forward_chain & | operator= (forward_chain &&from) noexcept |
chain_iterator_type | begin () const noexcept |
Get an iterator that starts at the beginning of the chain. | |
end_iterator_type | end () const noexcept |
Get a value end for which iter != end will return false only when iter is a chain_iterator_type that is past the end of its chain. | |
bool | empty () const noexcept |
Indicate whether the sequence is empty. | |
pointer_type | front () const noexcept |
Return a pointer to the first value in the sequence. | |
pointer_type | next (value_type &elt) const noexcept |
Return a pointer to the next value in the sequence. More... | |
pointer_type | back () const noexcept |
Return a pointer to the last value in the sequence. | |
void | link_front (value_type &value) noexcept |
Add the value to the front of the sequence. More... | |
pointer_type | unlink_front () noexcept |
Remove and return a pointer to the first value of the sequence. | |
void | link_after (value_type &pos, value_type &value) noexcept |
Add the value immediately after an value already in the sequence. More... | |
void | link_before (value_type &value, predicate_type pred) noexcept |
Add the value immediately before the first value in the sequence for which pred is true . More... | |
chain_type | split_through (predicate_type pred) noexcept |
Strip off a chain of all leading elements that satisfy a predicate. More... | |
void | link_back (value_type &value) noexcept |
Add the value to the end of the sequence. More... | |
pointer_type | unlink (value_type &value) noexcept |
Remove an value from the sequence at any position. More... | |
void | clear () noexcept |
Remove all values from the sequence. | |
Static Public Member Functions | |
static pointer_type | unlinked_ptr () noexcept |
Test whether a pointer is unlinked. | |
Static Public Attributes | |
static constexpr uintptr_t | UNLINKED_PTR = -1 |
Integral value of an invalid pointer used to denote unlinked objects. | |
Container used to link objects into a sequence.
The container is intended for use in embedded systems where objects may need to be present in one or more sequences and dynamic memory allocation for std::forward_list
and similar containers is not desired. Links are instead stored as fields within the linked structures, accessed through a function object provided to the container on construction.
Supports:
Container instances may be moved, but cannot be copied.
const
-qualified: iterating over a const forward_chain
instance allows non-const operations to be performed on the objects themselves.std::forward_list
. It is specifically safe to unlink the current value from the sequence within a range-for statement body: iteration will continue with the next value in the original sequence. Removing other values, or adding values, may produce anomalous behavior if an existing iterator is used after the sequence is modified. Iterators are also invalidated if the sequence is moved.T | the type of the instance. |
REF_NEXT | a function object type where operator() converts a T& to a T*& which is the lvalue for the pointer to the next T in the chain. |
Example:
struct object { // Function object type used to reference field next struct ref_next { using pointer_type = object *; pointer_type& operator() (object& m) noexcept { return m.next; } }; using chain_type = forward_chain<object>; // Declare link used when in a chain, initalized as unlinked chain_type::pointer_type next{chain_type::unlinked_ptr()}; // Declare the chain that links objects static chain_type chain; };
|
inlinenoexcept |
Add the value immediately after an value already in the sequence.
pos | the value already in the sequence. |
value | the value to add after pos . |
|
inlinenoexcept |
Add the value to the end of the sequence.
value
must not already be in the sequence.
|
inlinenoexcept |
Add the value immediately before the first value in the sequence for which pred
is true
.
If no value in the sequence satisfies the predicate the new value is linked at the end.
value | the value to insert into the chain. |
pred | the predicate that identifies the linked member before which value will be inserted. |
|
inlinenoexcept |
Add the value to the front of the sequence.
value
must not already be in the sequence.
|
inlinenoexcept |
Return a pointer to the next value in the sequence.
ptr | an object known to be in the chain. |
nullptr
if elt
is last.
|
inlinenoexcept |
Strip off a chain of all leading elements that satisfy a predicate.
This may be used on a chain linking objects by some ordinal function via insert_before(), to extract the prefix list of items that are ready to process.
pred | the predicate that identifies elements that are to be removed from the list. |
pred
. The chain in which this method is invoked holds any remaining elements.
|
inlinenoexcept |
Remove an value from the sequence at any position.
value | the value to be removed |
value
if value
was in the sequence, otherwise a null pointer.