Template Class SoFi

Class Documentation

template<class ValueType, uint64_t CapacityValue>
class SoFi

Thread safe producer and consumer queue with a safe overflowing behavior. SoFi is designed in a FIFO Manner but prevents data loss when pushing into a full SoFi. When SoFi is full and a Sender tries to push, the data at the current read position will be returned. SoFi is a Thread safe without using locks. When the buffer is filled, new data is written starting at the beginning of the buffer and overwriting the old.The SoFi is especially designed to provide fixed capacity storage. When its capacity is exhausted, newly inserted elements will cause elements either at the beginning to be overwritten.The SoFi only allocates memory when created , capacity can be is adjusted explicitly.

Param ValueType:

[in] DataType to be stored, must be trivially copyable

Param CapacityValue:

[in] Capacity of the SoFi

Public Functions

SoFi() noexcept

default constructor which constructs an empty sofi

bool push(const ValueType &valueOut, ValueType &f_paramOut_r) noexcept

pushs an element into sofi. if sofi is full the oldest data will be returned and the pushed element is stored in its place instead.

Parameters:
  • valueOut[in] value which should be stored

  • f_paramOut_r[out] if sofi is overflowing the value of the overridden value is stored here @concurrent restricted thread safe: single pop, single push no push calls from multiple contexts

Returns:

return true if push was sucessfull else false.

 (initial situation, SOFI is FULL)
     Start|-----A-------|
                        |-----B-------|
                                      |-----C-------|
                                                    |-----D-------|


 (calling push with data ’E’)
     Start|-----E-------|
                        |-----A-------|
                                      |-----B-------|
                                                    |-----C-------|
                                     (’D’ is returned as value_out)

###################################################################

 (if SOFI is not FULL , calling push() add new data)
     Start|-------------|
                        |-------------|  ( Initial SOFI )
  (push() Called two times)

                                      |-------------|
                                      (New Data)
                                                     |-------------|
                                                      (New Data)

bool pop(ValueType &valueOut) noexcept

pop the oldest element

Parameters:

valueOut[out] storage of the pop’ed value @concurrent restricted thread safe: single pop, single push no pop or popIf calls from multiple contexts

Returns:

false if sofi is empty, otherwise true

template<typename Verificator_T>
bool popIf(ValueType &valueOut, const Verificator_T &verificator) noexcept

conditional pop call to provide an alternative for a peek and pop approach. If the verificator returns true the peeked element is returned.

Parameters:
  • valueOut[out] storage of the pop’ed value

  • verificator[in] callable of type bool(const ValueType& peekValue) which takes the value which would be pop’ed as argument and returns true if it should be pop’ed, otherwise false

    int limit = 7128;
    mysofi.popIf(value, [=](const ValueType & peek)
        {
            return peek < limit; // pop only when peek is smaller than limit
        }
    ); // pop's a value only if it is smaller than 9012
    
    @concurrent restricted thread safe: single pop, single push no pop or popIf calls from multiple contexts

Returns:

false if sofi is empty or when verificator returns false, otherwise true

bool empty() const noexcept

returns true if sofi is empty, otherwise false

Note

the use of this function is limited in the concurrency case. if you call this and in another thread pop is called the result can be out of date as soon as you require it @concurrent unrestricted thread safe

bool setCapacity(const uint64_t newSize) noexcept

resizes sofi

Parameters:

newSize[in] valid values are 0 < newSize < CapacityValue

Pre:

it is important that no pop or push calls occur during this call @concurrent not thread safe

uint64_t capacity() const noexcept

returns the capacity of sofi @concurrent unrestricted thread safe

uint64_t size() const noexcept

returns the current size of sofi @concurrent unrestricted thread safe