OnixS C++ CME MDP Streamlined Market Data Handler 1.2.0
API Documentation
Loading...
Searching...
No Matches
VectorOverArray.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <cassert>
24
25#include <string>
26#include <stdexcept>
27
29
31
35template
36<
37 typename ItemType,
38 typename SizeType
39>
41{
42public:
44 typedef ItemType Item;
45
47 typedef SizeType Size;
48
50 typedef const Item* ConstIterator;
51
53 typedef Item* Iterator;
54
58 , size_(0)
59 , capacity_(0)
60 {
61 }
62
65 Item* items,
67 : items_(items)
68 , size_(0)
69 , capacity_(capacity)
70 {
71 }
72
75 {
76 resize(0);
77 }
78
80 bool
81 empty() const
82 {
83 return (0 == size_);
84 }
85
87 Size
88 size() const
89 {
90 return size_;
91 }
92
94 Size
95 capacity() const
96 {
97 return capacity_;
98 }
99
101 Iterator
103 {
104 return items_;
105 }
106
108 ConstIterator
109 begin() const
110 {
111 return items_;
112 }
113
115 Iterator
117 {
118 return (items_ + size_);
119 }
120
122 ConstIterator
123 end() const
124 {
125 return (items_ + size_);
126 }
127
129 Item&
131 {
132 assert(0 < size_);
133
134 return items_[0];
135 }
136
138 const Item&
139 front() const
140 {
141 assert(0 < size_);
142
143 return items_[0];
144 }
145
147 Item&
149 Size index)
150 {
151 assert(index < size_);
152
153 return items_[index];
154 }
155
157 const Item&
159 Size index) const
160 {
161 assert(index < size_);
162
163 return items_[index];
164 }
165
168 Item&
170 Size index)
171 {
172 if (index < size_)
173 return items_[index];
174
175 throw std::out_of_range
176 (
177 "Index outside of range."
178 );
179 }
180
183 const Item&
185 Size index) const
186 {
187 if (index < size_)
188 return items_[index];
189
190 throw std::out_of_range
191 (
192 "Index outside of range."
193 );
194 }
195
203 Item&
205 {
206 assert(index < size_);
207
208 Item* const toBeUpdated = items_ + index;
209
210 Item* target = items_ + size_ - 1;
211 Item* source = target - 1;
212
213 while (target != toBeUpdated)
214 {
215 target->~Item();
216
217 new (target--) Item(*source--);
218 }
219
220 return *toBeUpdated;
221 }
222
224 void
226 Size index)
227 {
228 assert(index < size_);
229
230 Item* const theEnd = end();
231 Item* const toBeErased = items_ + index;
232
233 Item* target = toBeErased;
234 Item* source = toBeErased + 1;
235
236 while (source != theEnd)
237 {
238 target->~Item();
239
240 new (target++) Item(*source++);
241 }
242
243 target->~Item();
244
245 --size_;
246 }
247
252 void
254 Size newSize,
255 const Item& item = Item())
256 {
257 assert(newSize <= capacity_);
258
259 if (newSize > size_)
260 {
261 while (size_ != newSize)
262 new (items_ + size_++) Item(item);
263 }
264 else
265 {
266 while (size_ != newSize)
267 (items_ + --size_)->~Item();
268 }
269 }
270
271private:
272 Item* items_;
273 Size size_;
274
275 const Size capacity_;
276
278 const VectorOverArray&);
279
281 operator =(
282 const VectorOverArray&);
283};
284
285// Throws exception on copy failure.
286template
287<
288 typename TargetSize,
289 typename SourceSize
290>
291void
293 TargetSize targetSize,
294 SourceSize sourceSize)
295{
296 std::string error;
297
298 error +=
299 "Cannot copy items because number "
300 "of source items exceeds target capacity "
301 "[sourceItemCount=";
302
303 toStr(error, sourceSize);
304
305 error += ",targetCapacity=";
306
307 toStr(error, targetSize);
308
309 error += "]. ";
310
311 throw std::runtime_error(error);
312}
313
317template
318<
319 typename TargetItem,
320 typename TargetSize,
321 typename SourceItem,
322 typename SourceSize
323>
324void
327 <TargetItem, TargetSize>&
328 target,
329 const
331 <SourceItem, TargetSize>&
332 source,
333 SourceSize itemCount =
334 static_cast<SourceSize>(-1))
335{
336 if (itemCount > source.size())
337 itemCount = source.size();
338
339 if (itemCount > target.capacity())
340 {
342 (
343 target.capacity(),
344 itemCount
345 );
346 }
347
348 target.resize(itemCount);
349
350 while (itemCount--)
351 {
352 target[itemCount] =
353 source[itemCount];
354 }
355}
356
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:169
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_NULLPTR
Definition Compiler.h:167
Implements vector-like container over built-in array of fixed size being referenced.
Size capacity() const
Total capacity of container.
void erase(Size index)
Erases item at given position.
Iterator begin()
Iterator referencing to the first item.
const Item & operator[](Size index) const
Accesses to item by index.
void resize(Size newSize, const Item &item=Item())
Resizes container.
Item & at(Size index)
Accesses to item by index checking access bounds.
Item & operator[](Size index)
Accesses to item by index.
const Item * ConstIterator
Aliases constant iterator type.
bool empty() const
Indicates whether instance has no items.
ConstIterator end() const
Iterator referencing to the item behind the last item.
ItemType Item
Aliases container item type.
VectorOverArray(Item *items, Size capacity)
Initializes instance over given fixed-size array.
Item & insertExtrude(Size index)
Inserts new item to given position.
const Item & front() const
Provides access to the front item if available.
Iterator end()
Iterator referencing to the item behind the last item.
ConstIterator begin() const
Iterator referencing to the first item.
Item & front()
Provides access to the front item if available.
const Item & at(Size index) const
Accesses to item by index checking access bounds.
VectorOverArray()
Initializes instance with no items.
Item * Iterator
Aliases mutable iterator type.
void throwVectorCopyOverflow(TargetSize targetSize, SourceSize sourceSize)
void copy(VectorOverArray< TargetItem, TargetSize > &target, const VectorOverArray< SourceItem, TargetSize > &source, SourceSize itemCount=static_cast< SourceSize >(-1))
Copies items from source to target.
void toStr(std::string &str, const Decimal &number)
Definition Decimal.h:502