OnixS BME SENAF Handler C++ library  2.2.1
API documentation
Containers.h
Go to the documentation of this file.
1 /*
2  * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3  *
4  * This software owned by Onix Solutions Limited [OnixS] and is protected by
5  * copyright law and international copyright treaties.
6  *
7  * Access to and use of the software is governed by the terms of the applicable
8  * ONIXS Software Services Agreement (the Agreement) and Customer end user
9  * license agreements granting a non-assignable, non-transferable and
10  * non-exclusive license to use the software for it's own data processing
11  * purposes under the terms defined in the Agreement.
12  *
13  * Except as otherwise granted within the terms of the Agreement, copying or
14  * reproduction of any part of this source code or associated reference material
15  * to any other location for further reproduction or redistribution, and any
16  * amendments to this copyright notice, are expressly prohibited.
17  *
18  * Any reproduction or redistribution for sale or hiring of the Software not in
19  * accordance with the terms of the Agreement is a violation of copyright law.
20  */
21 
22 #pragma once
23 
24 #include <stdexcept>
25 
26 namespace OnixS { namespace Senaf { namespace MarketData {
27 
28 /// Represents reference to segment
29 /// of built-in array of items.
30 template <typename ItemType, typename SizeType>
31 class ArrayRef
32 {
33 public:
34  typedef ItemType Item;
35  typedef ItemType* Iterator;
36 
37  typedef SizeType Index;
38  typedef SizeType Size;
39 
40  /// Initializes instance referencing to nothing.
42  : items_(0)
43  , size_(0)
44  {
45  }
46 
47  /// Initializes instance referencing to given data.
48  ArrayRef(Item* const items, const Size itemsCount)
49  : items_(items)
50  , size_(itemsCount)
51  {
52  }
53 
54  /// Initializes instance as copy of other one.
55  template <typename OtherItem, typename OtherSize>
57  : items_(other.items())
58  , size_(other.size())
59  {
60  }
61 
62  /// Re-initializes instance as copy of the other one.
63  template <typename OtherItem, typename OtherSize>
65  {
66  items_ = other.items();
67  size_ = other.size();
68 
69  return *this;
70  }
71 
72  /// Destructs the instance.
73  ~ArrayRef() {}
74 
75  /// Indicates whether array has no items.
76  bool empty() const
77  {
78  return (0 == size_);
79  }
80 
81  /// Number of items being referenced.
82  Size size() const
83  {
84  return size_;
85  }
86 
87  /// Gets raw pointer to items.
88  Item* items() const
89  {
90  return items_;
91  }
92 
93  /// Iterator referencing to the first item.
94  Iterator begin() const
95  {
96  return items_;
97  }
98 
99  /// Iterator referencing to the item behind the last item.
100  Iterator end() const
101  {
102  return (items_ + size_);
103  }
104 
105  /// Accesses to item by index.
106  Item& operator[](Index index) const
107  {
108  return items_[index];
109  }
110 
111  /// Accesses to item by index checking access bounds.
112  Item& at(const Index index) const
113  {
114  if (index < size_)
115  {
116  return items_[index];
117  }
118 
119  throw std::out_of_range("Index outside of range.");
120  }
121 
122  /// Resets array with new items and size.
123  void reset(Item* const items = 0, const Size size = 0)
124  {
125  items_ = items;
126  size_ = size;
127  }
128 
129 protected:
130  Item* items_;
131  Size size_;
132 };
133 
134 }}} // namespace OnixS::Senaf::MarketData
Item & operator[](Index index) const
Accesses to item by index.
Definition: Containers.h:106
Item * items() const
Gets raw pointer to items.
Definition: Containers.h:88
bool empty() const
Indicates whether array has no items.
Definition: Containers.h:76
Iterator begin() const
Iterator referencing to the first item.
Definition: Containers.h:94
ArrayRef(const ArrayRef< OtherItem, OtherSize > &other)
Initializes instance as copy of other one.
Definition: Containers.h:56
~ArrayRef()
Destructs the instance.
Definition: Containers.h:73
ArrayRef(Item *const items, const Size itemsCount)
Initializes instance referencing to given data.
Definition: Containers.h:48
void reset(Item *const items=0, const Size size=0)
Resets array with new items and size.
Definition: Containers.h:123
Iterator end() const
Iterator referencing to the item behind the last item.
Definition: Containers.h:100
ArrayRef()
Initializes instance referencing to nothing.
Definition: Containers.h:41
ArrayRef & operator=(const ArrayRef< OtherItem, OtherSize > &other)
Re-initializes instance as copy of the other one.
Definition: Containers.h:64
Item & at(const Index index) const
Accesses to item by index checking access bounds.
Definition: Containers.h:112
Size size() const
Number of items being referenced.
Definition: Containers.h:82