OnixS C++ CME Market Data Handler  5.4.0
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
36 <
37  typename ItemType,
38  typename SizeType
39 >
41 {
42 public:
43  /// Aliases container item type.
44  typedef ItemType Item;
45 
46  /// Aliases dimension type.
47  typedef SizeType Size;
48 
49  /// Aliases constant iterator type.
50  typedef const Item* ConstIterator;
51 
52  /// Aliases mutable iterator type.
53  typedef Item* Iterator;
54 
55  /// Initializes instance with no items.
57  : items_(NULL)
58  , size_(0)
59  , capacity_(0)
60  {
61  }
62 
63  /// Initializes instance over given fixed-size array.
65  Item* items,
66  Size capacity)
67  : items_(items)
68  , size_(0)
69  , capacity_(capacity)
70  {
71  }
72 
73  /// Cleans everything up.
75  {
76  resize(0);
77  }
78 
79  /// Indicates whether instance has no items.
80  bool
81  empty() const
82  {
83  return (0 == size_);
84  }
85 
86  /// Number of items.
87  Size
88  size() const
89  {
90  return size_;
91  }
92 
93  /// Total capacity of container.
94  Size
95  capacity() const
96  {
97  return capacity_;
98  }
99 
100  /// Iterator referencing to the first item.
101  Iterator
103  {
104  return items_;
105  }
106 
107  /// Iterator referencing to the first item.
108  ConstIterator
109  begin() const
110  {
111  return items_;
112  }
113 
114  /// Iterator referencing to the item behind the last item.
115  Iterator
116  end()
117  {
118  return (items_ + size_);
119  }
120 
121  /// Iterator referencing to the item behind the last item.
122  ConstIterator
123  end() const
124  {
125  return (items_ + size_);
126  }
127 
128  /// Provides access to the front item if available.
129  Item&
131  {
132  assert(0 < size_);
133 
134  return items_[0];
135  }
136 
137  /// Provides access to the front item if available.
138  const Item&
139  front() const
140  {
141  assert(0 < size_);
142 
143  return items_[0];
144  }
145 
146  /// Accesses to item by index.
147  Item&
149  Size index)
150  {
151  assert(index < size_);
152 
153  return items_[index];
154  }
155 
156  /// Accesses to item by index.
157  const Item&
159  Size index) const
160  {
161  assert(index < size_);
162 
163  return items_[index];
164  }
165 
166  /// Accesses to item by index
167  /// checking access bounds.
168  Item&
169  at(
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 
181  /// Accesses to item by index
182  /// checking access bounds.
183  const Item&
184  at(
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 
196  /// Inserts new item to given position.
197  /// Items under higher indices are moved back.
198  /// Last item is extruded thus keeping size of
199  /// or container constant.
200  ///
201  /// @return Reference to an item at
202  /// requested position for further update.
203  Item&
204  insertExtrude(Size index)
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 
223  /// Erases item at given position.
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 
248  /// Resizes container.
249  ///
250  /// If vector is enlarged, new items
251  /// constructed as copies of given instance.
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 
271 private:
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.
286 template
287 <
288  typename TargetSize,
289  typename SourceSize
290 >
291 void
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 
314 /// Copies items from source to target.
315 /// Items are not required to be of same type
316 /// thus compatible types can be copied.
317 template
318 <
319  typename TargetItem,
320  typename TargetSize,
321  typename SourceItem,
322  typename SourceSize
323 >
324 void
327  <TargetItem, TargetSize>&
328  target,
329  const
331  <SourceItem, TargetSize>&
332  source,
333  SourceSize itemCount =
334  static_cast<SourceSize>(-1))
335 {
336  itemCount =
337  (itemCount >
338  source.size())
339  ? source.size()
340  : itemCount;
341 
342  if (itemCount >
343  target.capacity())
344  {
346  (
347  target.capacity(),
348  itemCount
349  );
350  }
351 
352  target.resize(itemCount);
353 
354  while (itemCount)
355  {
356  --itemCount;
357 
358  target[itemCount] =
359  source[itemCount];
360  }
361 }
362 
bool empty() const
Indicates whether instance has no items.
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.
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:379
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
Size size() const
Number of items.
Item & operator[](Size index)
Accesses to item by index.
const Item & at(Size index) const
Item & front()
Provides access to the front item if available.
ConstIterator begin() const
Iterator referencing to the first item.
VectorOverArray()
Initializes instance with no items.
void resize(Size newSize, const Item &item=Item())
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))
const Item & operator[](Size index) const
Accesses to item by index.
SizeType Size
Aliases dimension type.
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156