Files
externals/linux/include/OnixS/CME/MDP3/BookBase.h
T
2017-08-09 14:33:06 +00:00

603 lines
18 KiB
C++
Executable File

// 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 <assert.h>
#include <sstream>
#include "OnixS/CME/MDP3/Types.h"
namespace OnixS
{
namespace CME
{
namespace MDP3
{
/// 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_(NULL),
size_(0)
{
}
/// Initializes instance referencing to given data.
ArrayRef(
Item* items,
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())
{
}
// 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_;
}
/// 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(Index index) const
{
if (index < size_)
return items_[index];
throw std::domain_error(
"Index outside of range.");
}
/// Re-initializes instance as copy of the other one.
template
<
typename OtherItem,
typename OtherSize
>
ArrayRef<Item, Size>&
operator =(
const ArrayRef<OtherItem, OtherSize>& other)
{
items_ = other.items();
size_ = other.size();
return *this;
}
protected:
Item* items_;
Size size_;
};
/// Implements vector-like container over
/// built-in array of fixed size being
/// referenced.
template
<
typename ItemType,
typename SizeType
>
class VectorOverArray
{
public:
typedef ItemType Item;
typedef ItemType* Iterator;
typedef SizeType Index;
typedef SizeType Size;
/// Initializes instance with no items.
VectorOverArray()
: items_(NULL),
size_(0),
capacity_(0)
{
}
/// Initializes instance over given fixed-size array.
VectorOverArray(
Item* items,
Size capacity)
: items_(items),
size_(0),
capacity_(capacity)
{
}
/// Does nothing.
~VectorOverArray()
{
resize(0);
}
/// Indicates whether instance has no items.
bool
empty() const
{
return (0 == size_);
}
/// Number of items.
Size
size() const
{
return size_;
}
/// Total capacity of container.
Size
capacity() const
{
return capacity_;
}
/// 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
{
assert(index < size_);
return items_[index];
}
/// Accesses to item by index
/// checking access bounds.
Item&
at(Index index) const
{
if (index < size_)
return items_[index];
throw std::domain_error(
"Index outside of range.");
}
/// Inserts new item to given position.
/// Items under higher indices are moved back.
/// Last item is extruded thus keeping size of
/// or container constant.
///
/// @return Reference to an item at
/// requested position for further update.
Item&
extrudingInsert(Index index)
{
assert(index < size_);
Item* const toBeUpdated = items_ + index;
Item* target = items_ + size_ - 1;
Item* source = target - 1;
while (target != toBeUpdated)
{
target->~Item();
new (target--) Item(*source--);
}
return *toBeUpdated;
}
/// Erases item at given position.
void
eraseAt(
Index index)
{
assert(index < size_);
Item* const theEnd = end();
Item* const toBeErased = items_ + index;
Item* target = toBeErased;
Item* source = toBeErased + 1;
while (source != theEnd)
{
target->~Item();
new (target++) Item(*source++);
}
target->~Item();
--size_;
}
/// Resizes container.
///
/// If vector is enlarged, new items
/// constructed as copies of given instance.
void
resize(
Size newSize,
const Item& item = Item())
{
assert(newSize <= capacity_);
if (newSize > size_)
{
while (size_ != newSize)
new (items_ + size_++) Item(item);
}
else
{
while (size_ != newSize)
(items_ + --size_)->~Item();
}
}
/// Emplaces back a new item constructed using passed argument.
template <typename T>
void emplaceBack(const T& arg)
{
assert(size_ < capacity_);
new (items_ + size_++) Item(arg);
}
/// Emplaces back a new item constructed using passed arguments.
template <typename T1, typename T2>
void emplaceBack(const T1& arg1, const T2& arg2)
{
assert(size_ < capacity_);
new (items_ + size_++) Item(arg1, arg2);
}
/// Changes size of container without deleting or
/// constructing items (suitable for POD items).
void
size(Size newSize)
{
assert(newSize <= capacity_);
size_ = newSize;
}
protected:
Item* items_;
Size size_;
Size capacity_;
VectorOverArray(
const VectorOverArray&)
{
}
VectorOverArray&
operator =(
const VectorOverArray&)
{
return *this;
}
};
/// State of order book.
struct
ONIXS_CME_MDP3_API
BookStates
{
enum Enum
{
/// Book is naturally refreshed.
NaturallyRefreshed,
/// Book is up-to-date.
Latest
};
/// Deserializes value from text presentation.
static Enum deserialize(const char*);
/// Returns text presentation for given value.
static const char* toString(Enum);
};
typedef
BookStates::Enum
BookState;
/// Base attributes of order book.
///
/// A set of bids and asks data for the given security
/// (bids are descending and asks are ascending).
template
<
class PriceLevelType
>
class
BookBase
{
public:
/// Indicates price level data type.
typedef
PriceLevelType
PriceLevel;
/// Indicates reference to array of immutable price levels.
typedef
ArrayRef
<
const PriceLevel,
MarketDepth
>
PriceLevelsRef;
/// Indicates reference to array of mutable price levels.
typedef
ArrayRef
<
PriceLevel,
MarketDepth
>
EditablePriceLevelsRef;
/// Collection of price levels allowing modifications.
typedef
VectorOverArray
<
PriceLevel,
MarketDepth
>
EditablePriceLevels;
/// Unique instrument Id (tag 48) as qualified
/// by the exchange per tag 22-SecurityIDSource.
SecurityId
securityId() const
{
return id_;
}
/// Updates unique instrument Id.
void
securityId(
SecurityId id)
{
id_ = id;
}
/// Returns the maximal number of price levels.
MarketDepth
depth() const
{
return depth_;
}
/// Updates the maximal number of price levels.
void
depth(
MarketDepth depth)
{
assert(depth <= bids_.capacity());
assert(depth <= asks_.capacity());
depth_ = depth;
}
/// List of descending bid prices for the security.
const
PriceLevelsRef
bids() const
{
return PriceLevelsRef(
bids_.begin(), bids_.size());
}
/// List of ascending ask prices for the security.
const
PriceLevelsRef
asks() const
{
return PriceLevelsRef(
asks_.begin(), asks_.size());
}
/// List of ascending ask prices for the security.
EditablePriceLevels&
asks()
{
return asks_;
}
/// List of descending bid prices for the security.
EditablePriceLevels&
bids()
{
return bids_;
}
/// Indicates current state of book.
BookState
state() const
{
return state_;
}
/// Updates state of book.
void
state(
BookState state)
{
state_ = state;
}
/// Instrument-level sequence number of market
/// data entry most recently applied to the book.
SequenceNumber
lastAppliedSecurityLevelSeqNumber() const
{
return securityLevelSeqNumber_;
}
/// Updates instrument-level sequence number of market
/// data entry most recently applied to the book.
void
lastAppliedSecurityLevelSeqNumber(
SequenceNumber securityLevelSeqNumber)
{
securityLevelSeqNumber_ = securityLevelSeqNumber;
}
protected:
/// Initializes instance with given attributes.
///
/// Bids and asks are stored in given segments of plain arrays.
/// Book constructs vectors over input arrays of fixed size.
/// That provides more flexibility on memory layout for
/// internally used data.
BookBase(
SecurityId id,
MarketDepth depth,
const EditablePriceLevelsRef& bids,
const EditablePriceLevelsRef& asks)
: id_(id),
depth_(depth),
bids_(
bids.begin(),
bids.size()),
asks_(
asks.begin(),
asks.size()),
securityLevelSeqNumber_(0)
{
assert(depth <= bids_.capacity());
assert(depth <= asks_.capacity());
}
/// Cleans everything up.
~BookBase()
{
}
/// Wipes out all bids and asks and other
/// data associated with book (like instance
/// of last applied FIX message).
void
reset(
BookState bookState)
{
state(bookState);
bids_.resize(0);
asks_.resize(0);
}
private:
// Security id.
SecurityId id_;
// Book depth.
MarketDepth depth_;
// Book state.
BookState state_;
// Bids.
EditablePriceLevels bids_;
// Asks.
EditablePriceLevels asks_;
// last applied rpt seq
SequenceNumber securityLevelSeqNumber_;
// Copying/Cloning is done in other way.
BookBase(const BookBase&);
BookBase& operator=(const BookBase&);
};
}
}
}