Files
2019-02-11 17:36:12 +00:00

133 lines
3.4 KiB
C++

/*
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
*
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
* and international copyright treaties.
*
* Access to and use of the software is governed by the terms of the applicable OnixS Software
* Services Agreement (the Agreement) and Customer end user license agreements granting
* a non-assignable, non-transferable and non-exclusive license to use the software
* for it's own data processing purposes under the terms defined in the Agreement.
*
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
* of this source code or associated reference material to any other location for further reproduction
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
*
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
* the terms of the Agreement is a violation of copyright law.
*/
#pragma once
#include <stdexcept>
namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
/// Represents reference to segment
/// of built-in array of items.
template <typename ItemType, typename SizeType>
class ArrayRef
{
public:
typedef ItemType Item;
typedef ItemType* Iterator;
typedef SizeType Index;
typedef SizeType Size;
/// Initializes instance referencing to nothing.
ArrayRef()
: items_(0)
, size_(0)
{
}
/// Initializes instance referencing to given data.
ArrayRef(Item* const items, const Size itemsCount)
: items_(items)
, size_(itemsCount)
{
}
/// Initializes instance as copy of other one.
template <typename OtherItem, typename OtherSize>
ArrayRef(const ArrayRef<OtherItem, OtherSize>& other)
: items_(other.items())
, size_(other.size())
{
}
/// Re-initializes instance as copy of the other one.
template <typename OtherItem, typename OtherSize>
ArrayRef& operator=(const ArrayRef<OtherItem, OtherSize>& other)
{
items_ = other.items();
size_ = other.size();
return *this;
}
/// Destructs the instance.
~ArrayRef()
{
}
/// Indicates whether array has no items.
bool empty() const
{
return (0 == size_);
}
/// Number of items being referenced.
Size size() const
{
return size_;
}
/// Gets raw pointer to items.
Item* items() const
{
return items_;
}
/// Iterator referencing to the first item.
Iterator begin() const
{
return items_;
}
/// Iterator referencing to the item behind the last item.
Iterator end() const
{
return (items_ + size_);
}
/// Accesses to item by index.
Item& operator[](Index index) const
{
return items_[index];
}
/// Accesses to item by index checking access bounds.
Item& at(const Index index) const
{
if (index < size_)
return items_[index];
throw std::out_of_range("Index outside of range.");
}
/// Resets array with new items and size.
void reset(Item* const items = 0, const Size size = 0)
{
items_ = items;
size_ = size;
}
protected:
Item* items_;
Size size_;
};
}}}} // namespace MarketData, iMpact, ICE, OnixS