GCC Code Coverage Report


Directory: codebase/
File: codebase/library/include/public/lib.Buffer.hpp
Date: 2023-03-16 04:37:09
Exec Total Coverage
Lines: 60 60 100.0%
Functions: 22 22 100.0%
Branches: 27 32 84.4%

Line Branch Exec Source
1 /**
2 * @file lib.Buffer.hpp
3 * @author Sergey Baigudin, sergey@baigudin.software
4 * @copyright 2014-2022, Sergey Baigudin, Baigudin Software
5 */
6 #ifndef LIB_BUFFER_HPP_
7 #define LIB_BUFFER_HPP_
8
9 #include "lib.AbstractBuffer.hpp"
10
11 namespace eoos
12 {
13 namespace lib
14 {
15
16 /**
17 * @class Buffer<T,L,A>
18 * @brief Buffer class static.
19 *
20 * This class is a primary template defines a realization that contains
21 * a whole buffer, which is defined by a template argument.
22 *
23 * @tparam T Data type of buffer element.
24 * @tparam L Maximum number of buffer elements, or 0 for dynamic allocation.
25 * @tparam A Heap memory allocator class.
26 */
27 template <typename T, int32_t L, class A = Allocator>
28 class Buffer : public AbstractBuffer<T,A>
29 {
30 typedef AbstractBuffer<T,A> Parent;
31
32 public:
33
34 using Parent::isConstructed;
35
36 /**
37 * @brief Constructor.
38 */
39 5 Buffer()
40 : AbstractBuffer<T,A>(L)
41 5 , buf_(arr_){
42 5 }
43
44 /**
45 * @brief Constructor.
46 *
47 * @note A passed illegal element will be copied to an internal data of the class
48 *
49 * @param illegal An illegal value.
50 */
51 42 Buffer(T const& illegal)
52 : AbstractBuffer<T,A>(L, illegal)
53 42 , buf_ (arr_){
54 42 }
55
56 /**
57 * @brief Destructor.
58 */
59 52 virtual ~Buffer()
60 {
61 }
62
63 /**
64 * @copydoc eoos::api::SequenceContainer::getData()
65 */
66 276 virtual T* getData() const
67 {
68 276 T* buf( NULLPTR );
69
2/2
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 19 times.
276 if( isConstructed() )
70 {
71 238 buf = buf_;
72 }
73 276 return buf;
74 }
75
76 /**
77 * @brief Assignment operator.
78 *
79 * If the source buffer is greater than this buffer,
80 * only cropped data of that will be copied.
81 *
82 * @param buf Reference to source buffer.
83 * @return Reference to this object.
84 */
85 5 Buffer& operator=(api::SequenceContainer<T> const& buf)
86 {
87
6/6
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
5 if( isConstructed() && buf.isConstructed() )
88 {
89 3 copy(buf);
90 }
91 5 return *this;
92 }
93
94 protected:
95
96 using Parent::setConstructed;
97 using Parent::copy;
98
99 private:
100
101 /**
102 * @brief Current array of T elements.
103 */
104 T arr_[L];
105
106 /**
107 * @brief Pointer to current array.
108 *
109 * @note The variable has been defined only for giving the getBuffer member function to be constant.
110 */
111 T* buf_;
112
113 };
114
115 #ifdef EOOS_ENABLE_DYNAMIC_HEAP_MEMORY
116
117 /**
118 * @class Buffer<T,0,A>
119 * @brief Buffer class dynamic.
120 *
121 * This is a partial specialization of the template allocates necessary
122 * memory size for containing the buffer in a heap memory.
123 *
124 * @tparam T Data type of buffer element.
125 * @tparam A Heap memory allocator class.
126 */
127 template <typename T, class A>
128 class Buffer<T,0,A> : public AbstractBuffer<T,A>
129 {
130 typedef AbstractBuffer<T,A> Parent;
131
132 public:
133
134 using Parent::isConstructed;
135
136 /**
137 * @brief Constructor.
138 *
139 * @param length Count of buffer elements.
140 */
141 6 explicit Buffer(size_t const length)
142 : AbstractBuffer<T,A>(length)
143 6 , buf_(NULLPTR)
144 6 , isDeleted_(true) {
145
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 bool_t const isConstructed( construct(length) );
146 6 setConstructed( isConstructed );
147 6 }
148
149 /**
150 * @brief Constructor.
151 *
152 * @note A passed illegal element will be copied to an internal data of the class
153 *
154 * @param length Count of buffer elements.
155 * @param illegal Illegal value.
156 */
157 21 Buffer(size_t const length, T const& illegal)
158 : AbstractBuffer<T,A>(length, illegal)
159 21 , buf_(NULLPTR)
160 21 , isDeleted_(true) {
161
1/2
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
21 bool_t const isConstructed( construct(length) );
162 21 setConstructed( isConstructed );
163 21 }
164
165 /**
166 * @brief Constructor.
167 *
168 * @note Given external buffer has to exist until this object is alive.
169 *
170 * @param length Number of elements.
171 * @param buf Pointer to external buffer.
172 */
173 1 Buffer(size_t const length, T* const buf)
174 : AbstractBuffer<T,A>(length)
175 1 , buf_(buf)
176 1 , isDeleted_(false) {
177
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 bool_t const isConstructed( construct(length) );
178 1 setConstructed( isConstructed );
179 1 }
180
181 /**
182 * @brief Constructor.
183 *
184 * @note Given external buffer has to exist until this object is alive.
185 * @note A passed illegal element will be copied to an internal data of the class.
186 *
187 * @param length Number of elements.
188 * @param buf Pointer to external buffer.
189 * @param illegal Illegal value.
190 */
191 11 Buffer(size_t const length, T* const buf, T const& illegal)
192 : AbstractBuffer<T,A>(length, illegal)
193 11 , buf_(buf)
194 11 , isDeleted_(false) {
195
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 bool_t const isConstructed( construct(length) );
196 11 setConstructed( isConstructed );
197 11 }
198
199 /**
200 * @brief Destructor.
201 */
202 78 virtual ~Buffer()
203 {
204 78 if( isDeleted_ == true )
205 {
206 54 A::free(buf_);
207 }
208 }
209
210 /**
211 * @copydoc eoos::api::SequenceContainer::getData()
212 */
213 200 virtual T* getData() const
214 {
215 200 T* buf( NULLPTR );
216
2/2
✓ Branch 1 taken 169 times.
✓ Branch 2 taken 31 times.
200 if( isConstructed() )
217 {
218 169 buf = buf_;
219 }
220 200 return buf;
221 }
222
223 /**
224 * @brief Assignment operator.
225 *
226 * If the source buffer is greater than this buffer,
227 * only cropped data of that will be copied.
228 *
229 * @param buf Reference to source buffer.
230 * @return Reference to this object.
231 */
232 5 Buffer& operator=(api::SequenceContainer<T> const& buf)
233 {
234
6/6
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
5 if( isConstructed() && buf.isConstructed() )
235 {
236 3 copy(buf);
237 }
238 5 return *this;
239 }
240
241 protected:
242
243 using Parent::setConstructed;
244 using Parent::copy;
245
246 private:
247
248 /**
249 * @brief Constructor.
250 *
251 * @param length Count of buffer elements.
252 * @return Boolean result.
253 */
254 39 bool_t construct(size_t const length)
255 {
256 39 bool_t res( false );
257
5/6
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 38 times.
✓ Branch 6 taken 1 times.
39 if( isConstructed() && (length > 0U) )
258 {
259
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 12 times.
38 if(buf_ == NULLPTR)
260 {
261 26 void* const addr( A::allocate(length * (sizeof(T))) );
262 26 buf_ = reinterpret_cast<T*>( addr ); ///< SCA MISRA-C++:2008 Justified Rule 5-2-8
263 }
264 38 res = buf_ != NULLPTR;
265 }
266 39 return res;
267 }
268
269 /**
270 * @brief Pointer to external given or self created array.
271 */
272 T* buf_;
273
274 /**
275 * @brief Deletet flag.
276 *
277 * Is set to true for self created array or
278 * set to false for external given array.
279 */
280 bool_t isDeleted_;
281
282 };
283
284 #endif // EOOS_ENABLE_DYNAMIC_HEAP_MEMORY
285
286 } // namespace lib
287 } // namespace eoos
288 #endif // LIB_BUFFER_HPP_
289