OnixS C++ CME MDP Premium Market Data Handler  5.8.3
API Documentation
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 
32 /// Implements vector-like container over
33 /// built-in array of fixed size being
34 /// referenced.
35 template <typename ItemType, typename SizeType>
37 {
38 public:
39  /// Aliases container item type.
40  typedef ItemType Item;
41 
42  /// Aliases dimension type.
43  typedef SizeType Size;
44 
45  /// Aliases constant iterator type.
46  typedef const Item* ConstIterator;
47 
48  /// Aliases mutable iterator type.
49  typedef Item* Iterator;
50 
51  /// Initializes instance with no items.
53  : items_(ONIXS_CMEMDH_NULLPTR)
54  , size_(0)
55  , capacity_(0)
56  {
57  }
58 
59  /// Initializes instance over given fixed-size array.
60  VectorOverArray(Item* items, Size capacity)
61  : items_(items)
62  , size_(0)
63  , capacity_(capacity)
64  {
65  }
66 
67  /// Cleans everything up.
69  {
70  resize(0);
71  }
72 
73  /// Indicates whether instance has no items.
74  bool empty() const
75  {
76  return (0 == size_);
77  }
78 
79  /// Number of items.
80  Size size() const
81  {
82  return size_;
83  }
84 
85  /// Total capacity of container.
86  Size capacity() const
87  {
88  return capacity_;
89  }
90 
91  /// Iterator referencing to the first item.
92  Iterator begin()
93  {
94  return items_;
95  }
96 
97  /// Iterator referencing to the first item.
98  ConstIterator begin() const
99  {
100  return items_;
101  }
102 
103  /// Iterator referencing to the item behind the last item.
104  Iterator end()
105  {
106  return (items_ + size_);
107  }
108 
109  /// Iterator referencing to the item behind the last item.
110  ConstIterator end() const
111  {
112  return (items_ + size_);
113  }
114 
115  /// Provides access to the front item if available.
116  Item& front()
117  {
118  assert(0 < size_);
119 
120  return items_[0];
121  }
122 
123  /// Provides access to the front item if available.
124  const Item& front() const
125  {
126  assert(0 < size_);
127 
128  return items_[0];
129  }
130 
131  /// Accesses to item by index.
132  Item& operator[](Size index)
133  {
134  assert(index < size_);
135 
136  return items_[index];
137  }
138 
139  /// Accesses to item by index.
140  const Item& operator[](Size index) const
141  {
142  assert(index < size_);
143 
144  return items_[index];
145  }
146 
147  /// Accesses to item by index
148  /// checking access bounds.
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 
157  /// Accesses to item by index
158  /// checking access bounds.
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 
167  /// Inserts new item to given position.
168  /// Items under higher indices are moved back.
169  /// Last item is extruded thus keeping size of
170  /// or container constant.
171  ///
172  /// @return Reference to an item at
173  /// requested position for further update.
174  Item& insertExtrude(Size index)
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 
193  /// Erases item at given position.
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 
216  /// Resizes container.
217  ///
218  /// If vector is enlarged, new items
219  /// constructed as copies of given instance.
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 
236 private:
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.
248 template <typename TargetSize, typename SourceSize>
249 void 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 
268 /// Copies items from source to target.
269 /// Items are not required to be of same type
270 /// thus compatible types can be copied.
271 template <typename TargetItem, typename TargetSize, typename SourceItem, typename SourceSize>
272 void 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 
bool empty() const
Indicates whether instance has no items.
Implements vector-like container over built-in array of fixed size being referenced.
Item & at(Size index)
Accesses to item by index checking access bounds.
Size capacity() const
Total capacity of container.
Iterator begin()
Iterator referencing to the first item.
VectorOverArray(Item *items, Size capacity)
Initializes instance over given fixed-size array.
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:145
ItemType Item
Aliases container item type.
Item * Iterator
Aliases mutable iterator type.
void erase(Size index)
Erases item at given position.
void throwVectorCopyOverflow(TargetSize targetSize, SourceSize sourceSize)
Iterator end()
Iterator referencing to the item behind the last item.
void toStr(std::string &str, const Message &message)
Serializes FIX message into tag=value format.
Definition: Message.h:318
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
Size size() const
Number of items.
Item & operator[](Size index)
Accesses to item by index.
const Item & at(Size index) const
Accesses to item by index checking access bounds.
Item & front()
Provides access to the front item if available.
ConstIterator begin() const
Iterator referencing to the first item.
Item & insertExtrude(Size index)
Inserts new item to given position.
VectorOverArray()
Initializes instance with no items.
void resize(Size newSize, const Item &item=Item())
Resizes container.
const Item & front() const
Provides access to the front item if available.
const Item * ConstIterator
Aliases constant iterator type.
ConstIterator end() const
Iterator referencing to the item behind the last item.
~VectorOverArray()
Cleans everything up.
void copy(VectorOverArray< TargetItem, TargetSize > &target, const VectorOverArray< SourceItem, TargetSize > &source, SourceSize itemCount=static_cast< SourceSize >(-1))
Copies items from source to target.
const Item & operator[](Size index) const
Accesses to item by index.
SizeType Size
Aliases dimension type.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68