Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file lib.AbstractBuffer.hpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2014-2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #ifndef LIB_ABSTRACTBUFFER_HPP_ | ||
7 | #define LIB_ABSTRACTBUFFER_HPP_ | ||
8 | |||
9 | #include "lib.NonCopyable.hpp" | ||
10 | #include "api.SequenceContainer.hpp" | ||
11 | #include "api.IllegalValue.hpp" | ||
12 | |||
13 | namespace eoos | ||
14 | { | ||
15 | namespace lib | ||
16 | { | ||
17 | |||
18 | /** | ||
19 | * @class AbstractBuffer<T,A> | ||
20 | * @brief Abstract buffer class. | ||
21 | * | ||
22 | * @tparam T Data type of buffer element. | ||
23 | * @tparam A Heap memory allocator class. | ||
24 | * | ||
25 | * @todo Inherit Object instead of NonCopyable. | ||
26 | */ | ||
27 | template <typename T, class A = Allocator> | ||
28 | class AbstractBuffer : public NonCopyable<A>, public api::SequenceContainer<T>, public api::IllegalValue<T> | ||
29 | { | ||
30 | typedef NonCopyable<A> Parent; | ||
31 | |||
32 | public: | ||
33 | |||
34 | using api::SequenceContainer<T>::getData; | ||
35 | |||
36 | /** | ||
37 | * @brief Destructor. | ||
38 | */ | ||
39 | 130 | virtual ~AbstractBuffer() | |
40 | { | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * @copydoc eoos::api::Object::isConstructed() | ||
45 | */ | ||
46 | 754 | virtual bool_t isConstructed() const ///< SCA MISRA-C++:2008 Defected Rule 9-3-3 | |
47 | { | ||
48 | 754 | return Parent::isConstructed(); | |
49 | } | ||
50 | |||
51 | /** | ||
52 | * @copydoc eoos::api::Collection::getLength() | ||
53 | */ | ||
54 | 16 | virtual size_t getLength() const | |
55 | { | ||
56 | 16 | size_t length( 0U ); | |
57 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 2 times.
|
16 | if( isConstructed() ) |
58 | { | ||
59 | 14 | length = length_; | |
60 | } | ||
61 | 16 | return length; | |
62 | } | ||
63 | |||
64 | /** | ||
65 | * @copydoc eoos::api::Collection::isEmpty() | ||
66 | */ | ||
67 | 4 | virtual bool_t isEmpty() const | |
68 | { | ||
69 | 4 | bool_t res( true ); | |
70 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | if( isConstructed() ) |
71 | { | ||
72 | 2 | res = length_ == 0U; | |
73 | } | ||
74 | 4 | return res; | |
75 | } | ||
76 | |||
77 | /** | ||
78 | * @copydoc eoos::api::IllegalValue::getIllegal() | ||
79 | */ | ||
80 | 6 | virtual T const& getIllegal() const | |
81 | { | ||
82 | 6 | return illegal_; | |
83 | } | ||
84 | |||
85 | /** | ||
86 | * @copydoc eoos::api::IllegalValue::setIllegal(T const&) | ||
87 | */ | ||
88 | 2 | virtual void setIllegal(T const& value) | |
89 | { | ||
90 | 2 | illegal_ = value; | |
91 | 2 | } | |
92 | |||
93 | /** | ||
94 | * @copydoc eoos::api::IllegalValue::isIllegal() | ||
95 | */ | ||
96 | 6 | virtual bool_t isIllegal(T const& value) const | |
97 | { | ||
98 | 6 | return illegal_ == value; | |
99 | } | ||
100 | |||
101 | /** | ||
102 | * @brief Fills this buffer by given value. | ||
103 | * | ||
104 | * @param value A filling value. | ||
105 | */ | ||
106 | 14 | void fill(T const& value) | |
107 | { | ||
108 | 14 | fill(value, length_); | |
109 | 14 | } | |
110 | |||
111 | /** | ||
112 | * @brief Fills this buffer by given value. | ||
113 | * | ||
114 | * @param value Filling value. | ||
115 | * @param length Count of filling elements. | ||
116 | */ | ||
117 | 17 | void fill(T const& value, size_t const length) | |
118 | { | ||
119 | 17 | fill(value, 0, length); | |
120 | 17 | } | |
121 | |||
122 | /** | ||
123 | * @brief Fills this buffer by given value. | ||
124 | * | ||
125 | * @param value Filling value. | ||
126 | * @param index Begin index. | ||
127 | * @param count Count of filling elements. | ||
128 | */ | ||
129 | 32 | void fill(T const& value, size_t const begin, size_t const count) | |
130 | { | ||
131 | 32 | bool_t const hasIndex( begin < length_ ); | |
132 |
5/6✓ Branch 1 taken 27 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 5 times.
|
32 | if( isConstructed() && hasIndex ) |
133 | { | ||
134 | 27 | T* const buf( getData() ); | |
135 | 27 | size_t const length( begin + count ); | |
136 | // MSVC warning C4003 of Most Vexing Parse case avoided with no nameing the variable 'max' | ||
137 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 24 times.
|
27 | size_t const maximum( ( length <= length_ ) ? length : length_ ); |
138 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 27 times.
|
129 | for(size_t i(begin); i<maximum; i++) |
139 | { | ||
140 | 102 | buf[i] = value; | |
141 | } | ||
142 | } | ||
143 | 32 | } | |
144 | |||
145 | /** | ||
146 | * @brief Returns an element of this buffer. | ||
147 | * | ||
148 | * @param index An element index. | ||
149 | * @return An element. | ||
150 | */ | ||
151 | 294 | T& operator[](size_t const index) | |
152 | { | ||
153 | T* value; | ||
154 | 294 | T* const buf( getData() ); | |
155 |
7/8✓ Branch 1 taken 246 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 217 times.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 217 times.
✓ Branch 7 taken 77 times.
✓ Branch 8 taken 217 times.
|
294 | if( (!isConstructed()) || (index >= length_) || (buf == NULLPTR) ) |
156 | { | ||
157 | 77 | value = &illegal_; | |
158 | } | ||
159 | else | ||
160 | { | ||
161 | 217 | value = &buf[index]; | |
162 | } | ||
163 | 294 | return *value; | |
164 | } | ||
165 | |||
166 | protected: | ||
167 | |||
168 | /** | ||
169 | * @brief Constructor. | ||
170 | * | ||
171 | * @param length Count of buffer elements. | ||
172 | */ | ||
173 | 12 | explicit AbstractBuffer(size_t length) | |
174 | : NonCopyable<A>() | ||
175 | , api::SequenceContainer<T>() | ||
176 | , api::IllegalValue<T>() | ||
177 | 12 | , length_(length) | |
178 | 12 | , illegal_(){ | |
179 | 12 | } | |
180 | |||
181 | /** | ||
182 | * @brief Constructor. | ||
183 | * | ||
184 | * @note A passed illegal element will be copied to an internal data of the class | ||
185 | * | ||
186 | * @param length Count of buffer elements. | ||
187 | * @param illegal Illegal value. | ||
188 | */ | ||
189 | 53 | AbstractBuffer(size_t length, T const& illegal) | |
190 | : NonCopyable<A>() | ||
191 | , api::SequenceContainer<T>() | ||
192 | , api::IllegalValue<T>() | ||
193 | 53 | , length_(length) | |
194 | 53 | , illegal_(illegal){ | |
195 | 53 | } | |
196 | |||
197 | /** | ||
198 | * @brief Copies buffer to buffer. | ||
199 | * | ||
200 | * If the source buffer greater than this buffer, | ||
201 | * then only cropped data of that will be and copied. | ||
202 | * | ||
203 | * @param buf Reference to source buffer. | ||
204 | */ | ||
205 | 6 | void copy(api::SequenceContainer<T> const& buf) | |
206 | { | ||
207 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | if( isConstructed() ) |
208 | { | ||
209 | 6 | size_t const size1( getLength() ); | |
210 | 6 | size_t const size2( buf.getLength() ); | |
211 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | size_t const size( ( size1 < size2 ) ? size1 : size2 ); |
212 | 6 | T* const buf1( getData() ); | |
213 | 6 | T* const buf2( buf.getData() ); | |
214 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
|
22 | for(size_t i(0U); i<size; i++) |
215 | { | ||
216 | 16 | buf1[i] = buf2[i]; | |
217 | } | ||
218 | } | ||
219 | 6 | } | |
220 | |||
221 | private: | ||
222 | |||
223 | /** | ||
224 | * @brief Number of elements of this buffer. | ||
225 | */ | ||
226 | size_t length_; | ||
227 | |||
228 | /** | ||
229 | * @brief Illegal element of this buffer. | ||
230 | */ | ||
231 | T illegal_; | ||
232 | |||
233 | }; | ||
234 | |||
235 | } // namespace lib | ||
236 | } // namespace eoos | ||
237 | #endif // LIB_ABSTRACTBUFFER_HPP_ | ||
238 |