| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Queue items interfaces and queue implementation utilities | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_QUEUES_HPP_INCLUDED | ||
| 35 | #define GATE_QUEUES_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/queues.h" | ||
| 39 | #include "gate/gatetypes.hpp" | ||
| 40 | #include "gate/memalloc.hpp" | ||
| 41 | #include "gate/wrappers.hpp" | ||
| 42 | |||
| 43 | namespace gate | ||
| 44 | { | ||
| 45 | |||
| 46 | class GATE_CORE_CPP_API QueueItemBufferBase : private NonCopyable | ||
| 47 | { | ||
| 48 | public: | ||
| 49 | size_t length() const noexcept; | ||
| 50 | gate_queueitembuffer_t c_impl() const noexcept; | ||
| 51 | |||
| 52 | protected: | ||
| 53 | QueueItemBufferBase(size_t maxlength, size_t workitemSize, gate_mem_copyctor_t cctor, gate_mem_dtor_t dtor); | ||
| 54 | ~QueueItemBufferBase() noexcept; | ||
| 55 | |||
| 56 | |||
| 57 | bool_t pushItem(void const* ptrItem); | ||
| 58 | bool_t popItem(void* ptrStorage, size_t storageSize, uint32_t timeoutMs = 0); | ||
| 59 | |||
| 60 | private: | ||
| 61 | gate_queueitembuffer_t impl; | ||
| 62 | }; | ||
| 63 | |||
| 64 | |||
| 65 | /// @brief | ||
| 66 | /// @tparam T | ||
| 67 | template<class T> | ||
| 68 | class GATE_CORE_CPP_API QueueItemBuffer : public QueueItemBufferBase | ||
| 69 | { | ||
| 70 | private: | ||
| 71 | |||
| 72 | struct ItemStorage | ||
| 73 | { | ||
| 74 | union | ||
| 75 | { | ||
| 76 | gate_c_maxalign_t align; | ||
| 77 | char dummy[sizeof(T)]; | ||
| 78 | }; | ||
| 79 | bool initialized; | ||
| 80 | |||
| 81 | 4 | ItemStorage() : initialized(false) { } | |
| 82 | 4 | ~ItemStorage() | |
| 83 | { | ||
| 84 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (this->initialized) |
| 85 | { | ||
| 86 | 2 | TypeFunctions<T>::destruct(&this->align); | |
| 87 | } | ||
| 88 | 4 | } | |
| 89 | }; | ||
| 90 | |||
| 91 | public: | ||
| 92 | 1 | QueueItemBuffer(size_t maxlength = 0) | |
| 93 | 1 | : QueueItemBufferBase(maxlength, sizeof(T), &TypeFunctions<T>::copyConstruct, &TypeFunctions<T>::destruct) | |
| 94 | { | ||
| 95 | 1 | } | |
| 96 | 1 | ~QueueItemBuffer() noexcept { } | |
| 97 | |||
| 98 | 3 | bool push(T const& item) | |
| 99 | { | ||
| 100 | 3 | return this->pushItem(&item); | |
| 101 | } | ||
| 102 | |||
| 103 | 4 | bool pop(T& item, uint32_t timeoutMs = 0) | |
| 104 | { | ||
| 105 | 8 | ItemStorage store; | |
| 106 |
3/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | if(this->popItem(&store.align, sizeof(store), timeoutMs)) |
| 107 | { | ||
| 108 | 2 | store.initialized = true; | |
| 109 | 2 | item = *reinterpret_cast<T*>(&store.align); | |
| 110 | 2 | return true; | |
| 111 | } | ||
| 112 | 2 | return false; | |
| 113 | } | ||
| 114 | }; | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | #endif | ||
| 119 |