OnixS C++ CME MDP Premium Market Data Handler 5.10.3
Users' manual and 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
30
32
36template <typename ItemType, typename SizeType>
38{
39public:
41 typedef ItemType Item;
42
44 typedef SizeType Size;
45
47 typedef const Item* ConstIterator;
48
50 typedef Item* Iterator;
51
54 : items_(nullptr)
55 , size_(0)
56 , capacity_(0)
57 {
58 }
59
62 : items_(items)
63 , size_(0)
64 , capacity_(capacity)
65 {
66 }
67
70 {
71 resize(0);
72 }
73
75 bool empty() const
76 {
77 return (0 == size_);
78 }
79
81 Size size() const
82 {
83 return size_;
84 }
85
87 Size capacity() const
88 {
89 return capacity_;
90 }
91
94 {
95 return items_;
96 }
97
100 {
101 return items_;
102 }
103
106 {
107 return (items_ + size_);
108 }
109
112 {
113 return (items_ + size_);
114 }
115
118 {
119 assert(0 < size_);
120
121 return items_[0];
122 }
123
125 const Item& front() const
126 {
127 assert(0 < size_);
128
129 return items_[0];
130 }
131
134 {
135 assert(index < size_);
136
137 return items_[index];
138 }
139
141 const Item& operator[](Size index) const
142 {
143 assert(index < size_);
144
145 return items_[index];
146 }
147
150 Item& at(Size index)
151 {
152 if (index < size_)
153 return items_[index];
154
155 throw std::out_of_range("Index outside of range.");
156 }
157
160 const Item& at(Size index) const
161 {
162 if (index < size_)
163 return items_[index];
164
165 throw std::out_of_range("Index outside of range.");
166 }
167
176 {
177 assert(index < size_);
178
179 Item* const toBeUpdated = items_ + index;
180
181 Item* target = items_ + size_ - 1;
182 Item* source = target - 1;
183
184 while (target != toBeUpdated)
185 {
186 target->~Item();
187
188 new (target--) Item(*source--);
189 }
190
191 return *toBeUpdated;
192 }
193
195 void erase(Size index)
196 {
197 assert(index < size_);
198
199 Item* const theEnd = end();
200 Item* const toBeErased = items_ + index;
201
202 Item* target = toBeErased;
203 Item* source = toBeErased + 1;
204
205 while (source != theEnd)
206 {
207 target->~Item();
208
209 new (target++) Item(*source++);
210 }
211
212 target->~Item();
213
214 --size_;
215 }
216
221 void resize(Size newSize, const Item& item = Item())
222 {
223 assert(newSize <= capacity_);
224
225 if (newSize > size_)
226 {
227 while (size_ != newSize)
228 new (items_ + size_++) Item(item);
229 }
230 else
231 {
232 while (size_ != newSize)
233 (items_ + --size_)->~Item();
234 }
235 }
236
237private:
238 Item* items_;
239 Size size_;
240
241 const Size capacity_;
242
244
245 VectorOverArray& operator=(const VectorOverArray&);
246};
247
248// Throws exception on copy failure.
249template <typename TargetSize, typename SourceSize>
250void throwVectorCopyOverflow(TargetSize targetSize, SourceSize sourceSize)
251{
252 std::string error;
253
254 error += "Cannot copy items because number "
255 "of source items exceeds target capacity "
256 "[sourceItemCount=";
257
258 Messaging::toStr(error, sourceSize);
259
260 error += ",targetCapacity=";
261
262 Messaging::toStr(error, targetSize);
263
264 error += "]. ";
265
266 throw std::runtime_error(error);
267}
268
272template <typename TargetItem, typename TargetSize, typename SourceItem, typename SourceSize>
273void copy(
276 SourceSize itemCount = static_cast<SourceSize>(-1)
277)
278{
279 itemCount = (itemCount > source.size()) ? source.size() : itemCount;
280
281 if (itemCount > target.capacity())
282 {
283 throwVectorCopyOverflow(target.capacity(), itemCount);
284 }
285
286 target.resize(itemCount);
287
288 while (itemCount)
289 {
290 --itemCount;
291
292 target[itemCount] = source[itemCount];
293 }
294}
295
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:54
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:55
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())
Item & operator[](Size index)
Accesses to item by index.
bool empty() const
Indicates whether instance has no items.
ConstIterator end() const
Iterator referencing to the item behind the last item.
VectorOverArray(Item *items, Size capacity)
Initializes instance over given fixed-size array.
const Item & front() const
Provides access to the front item if available.
Iterator end()
Iterator referencing to the item behind the last item.
Size size() const
Number of items.
ConstIterator begin() const
Iterator referencing to the first item.
~VectorOverArray()
Cleans everything up.
Item & front()
Provides access to the front item if available.
const Item & at(Size index) const
VectorOverArray()
Initializes instance with no items.
void toStr(std::string &, Int8)
Serializes given integer into a string.
void throwVectorCopyOverflow(TargetSize targetSize, SourceSize sourceSize)
MboBook & copy(MboBook &target, const MboBook &source)
Copies content of MBO book to the other one.
Definition MboBook.h:210