| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, 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 Local buffer utilities | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_LIBRARIES_HPP_INCLUDED | ||
| 35 | #define GATE_LIBRARIES_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/memalloc.hpp" | ||
| 39 | #include "gate/exceptions.hpp" | ||
| 40 | #include "gate/localbuffer.h" | ||
| 41 | |||
| 42 | namespace gate | ||
| 43 | { | ||
| 44 | |||
| 45 | /// @brief LocalBuffer template to allocate a specifi amount of items | ||
| 46 | /// @tparam T | ||
| 47 | template <class T> | ||
| 48 | class GATE_CORE_CPP_TEMPLATE_API LocalBuffer | ||
| 49 | { | ||
| 50 | private: | ||
| 51 | gate_localbuffer_t impl; | ||
| 52 | const size_t count; | ||
| 53 | public: | ||
| 54 | typedef T item_t; | ||
| 55 | |||
| 56 | 1 | explicit LocalBuffer(size_t itemsCount) | |
| 57 | 1 | : count(itemsCount) | |
| 58 | { | ||
| 59 | 1 | const size_t byte_size = itemsCount * sizeof(item_t); | |
| 60 | 1 | item_t* ptr_data = static_cast<item_t*>(gate_localbuffer_create(&this->impl, byte_size)); | |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (NULL == ptr_data) |
| 62 | { | ||
| 63 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 64 | } | ||
| 65 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (item_t *ptr = ptr_data, *ptr_end = ptr_data + itemsCount; ptr != ptr_end; ++ptr) |
| 66 | { | ||
| 67 | 4 | GATEXX_TRY_CATCHALL( | |
| 68 | { | ||
| 69 | constructType(ptr); | ||
| 70 | }, | ||
| 71 | { | ||
| 72 | while(ptr != ptr_data) | ||
| 73 | { | ||
| 74 | --ptr; | ||
| 75 | destructType(ptr); | ||
| 76 | } | ||
| 77 | GATEXX_RETHROW; | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | } | ||
| 81 | 1 | } | |
| 82 | |||
| 83 | 1 | ~LocalBuffer() noexcept | |
| 84 | { | ||
| 85 | 1 | item_t* const begin = this->get(); | |
| 86 | 1 | item_t* ptr = begin + this->count; | |
| 87 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | while (ptr != begin) |
| 88 | { | ||
| 89 | 4 | --ptr; | |
| 90 | 4 | destructType(ptr); | |
| 91 | } | ||
| 92 | 1 | } | |
| 93 | |||
| 94 | item_t const* get() const noexcept | ||
| 95 | { | ||
| 96 | item_t const* ptr = static_cast<item_t const*>(gate_localbuffer_get(&this->impl)); | ||
| 97 | return ptr; | ||
| 98 | } | ||
| 99 | |||
| 100 | 2 | item_t* get() noexcept | |
| 101 | { | ||
| 102 | 2 | item_t* ptr = static_cast<item_t*>(gate_localbuffer_get(&this->impl)); | |
| 103 | 2 | return ptr; | |
| 104 | } | ||
| 105 | |||
| 106 | 1 | size_t length() const noexcept | |
| 107 | { | ||
| 108 | 1 | return this->count; | |
| 109 | } | ||
| 110 | }; | ||
| 111 | |||
| 112 | } // end of namespace gate | ||
| 113 | |||
| 114 | #endif | ||
| 115 |