OnixS C++ CME MDP Premium Market Data Handler 5.9.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 <typename ItemType, typename SizeType>
37{
38public:
40 typedef ItemType Item;
41
43 typedef SizeType Size;
44
46 typedef const Item* ConstIterator;
47
49 typedef Item* Iterator;
50
53 : items_(ONIXS_CMEMDH_NULLPTR)
54 , size_(0)
55 , capacity_(0)
56 {
57 }
58
61 : items_(items)
62 , size_(0)
63 , capacity_(capacity)
64 {
65 }
66
69 {
70 resize(0);
71 }
72
74 bool empty() const
75 {
76 return (0 == size_);
77 }
78
80 Size size() const
81 {
82 return size_;
83 }
84
86 Size capacity() const
87 {
88 return capacity_;
89 }
90
93 {
94 return items_;
95 }
96
99 {
100 return items_;
101 }
102
105 {
106 return (items_ + size_);
107 }
108
111 {
112 return (items_ + size_);
113 }
114
117 {
118 assert(0 < size_);
119
120 return items_[0];
121 }
122
124 const Item& front() const
125 {
126 assert(0 < size_);
127
128 return items_[0];
129 }
130
133 {
134 assert(index < size_);
135
136 return items_[index];
137 }
138
140 const Item& operator[](Size index) const
141 {
142 assert(index < size_);
143
144 return items_[index];
145 }
146
149 Item& at(Size index)
150 {
151 if (index < size_)
152 return items_[index];
153
154 throw std::out_of_range("Index outside of range.");
155 }
156
159 const Item& at(Size index) const
160 {
161 if (index < size_)
162 return items_[index];
163
164 throw std::out_of_range("Index outside of range.");
165 }
166
175 {
176 assert(index < size_);
177
178 Item* const toBeUpdated = items_ + index;
179
180 Item* target = items_ + size_ - 1;
181 Item* source = target - 1;
182
183 while (target != toBeUpdated)
184 {
185 target->~Item();
186
187 new (target--) Item(*source--);
188 }
189
190 return *toBeUpdated;
191 }
192
194 void erase(Size index)
195 {
196 assert(index < size_);
197
198 Item* const theEnd = end();
199 Item* const toBeErased = items_ + index;
200
201 Item* target = toBeErased;
202 Item* source = toBeErased + 1;
203
204 while (source != theEnd)
205 {
206 target->~Item();
207
208 new (target++) Item(*source++);
209 }
210
211 target->~Item();
212
213 --size_;
214 }
215
220 void resize(Size newSize, const Item& item = Item())
221 {
222 assert(newSize <= capacity_);
223
224 if (newSize > size_)
225 {
226 while (size_ != newSize)
227 new (items_ + size_++) Item(item);
228 }
229 else
230 {
231 while (size_ != newSize)
232 (items_ + --size_)->~Item();
233 }
234 }
235
236private:
237 Item* items_;
238 Size size_;
239
240 const Size capacity_;
241
243
244 VectorOverArray& operator=(const VectorOverArray&);
245};
246
247// Throws exception on copy failure.
248template <typename TargetSize, typename SourceSize>
249void throwVectorCopyOverflow(TargetSize targetSize, SourceSize sourceSize)
250{
251 std::string error;
252
253 error += "Cannot copy items because number "
254 "of source items exceeds target capacity "
255 "[sourceItemCount=";
256
257 toStr(error, sourceSize);
258
259 error += ",targetCapacity=";
260
261 toStr(error, targetSize);
262
263 error += "]. ";
264
265 throw std::runtime_error(error);
266}
267
271template <typename TargetItem, typename TargetSize, typename SourceItem, typename SourceSize>
272void copy(
275 SourceSize itemCount = static_cast<SourceSize>(-1)
276)
277{
278 itemCount = (itemCount > source.size()) ? source.size() : itemCount;
279
280 if (itemCount > target.capacity())
281 {
282 throwVectorCopyOverflow(target.capacity(), itemCount);
283 }
284
285 target.resize(itemCount);
286
287 while (itemCount)
288 {
289 --itemCount;
290
291 target[itemCount] = source[itemCount];
292 }
293}
294
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:68
#define ONIXS_CMEMDH_NULLPTR
Definition Compiler.h:178
Implements vector-like container over built-in array of fixed size being referenced.
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 & at(Size index)
Accesses to item by index checking access bounds.
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.
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.
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
Accesses to item by index checking access bounds.
VectorOverArray()
Initializes instance with no items.
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
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.