325 lines
6.1 KiB
C++
325 lines
6.1 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 <OnixS/CME/MDH/BookAttributes.h>
|
|
#include <OnixS/CME/MDH/MboBookTraits.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
|
|
/// Private base for MBO book.
|
|
///
|
|
/// Manages internals related with
|
|
/// efficient order entry allocation.
|
|
ONIXS_CMEMDH_LTWT_CLASS
|
|
MboBookBase
|
|
{
|
|
MemoryPool& pool_;
|
|
const bool releasePool_;
|
|
|
|
protected:
|
|
/// Instantiates allocator using MemoryPool for orders.
|
|
typedef
|
|
MemoryPoolAllocator<Order>
|
|
OrderAllocator;
|
|
|
|
/// Initializes with privately maintained memory pool.
|
|
MboBookBase()
|
|
: pool_(
|
|
*makeMemoryPool
|
|
(
|
|
64,
|
|
1,
|
|
20,
|
|
1
|
|
)
|
|
)
|
|
, releasePool_(true)
|
|
{
|
|
}
|
|
|
|
/// Binds to given memory pool.
|
|
MboBookBase(
|
|
MemoryPool& sharedPool)
|
|
: pool_(sharedPool)
|
|
, releasePool_(false)
|
|
{
|
|
}
|
|
|
|
/// Releases memory pool according to policy.
|
|
~MboBookBase()
|
|
{
|
|
if (releasePool_)
|
|
pool_.release();
|
|
}
|
|
|
|
/// Allocator over memory pool.
|
|
OrderAllocator allocator() const
|
|
{
|
|
return OrderAllocator(pool_);
|
|
}
|
|
};
|
|
|
|
/// Market By Order book.
|
|
ONIXS_CMEMDH_LTWT_CLASS
|
|
MboBook : MboBookBase
|
|
{
|
|
BookState::Enum state_;
|
|
|
|
SortedOrders bids_;
|
|
SortedOrders offers_;
|
|
|
|
public:
|
|
/// Order as book entry.
|
|
typedef Order Entry;
|
|
|
|
/// Book depth type.
|
|
typedef
|
|
SortedOrders::size_type
|
|
Depth;
|
|
|
|
/// Sorted orders are book entries.
|
|
typedef SortedOrders Entries;
|
|
|
|
/// Iterator over bids and offers.
|
|
typedef
|
|
Entries::const_iterator
|
|
EntryIterator;
|
|
|
|
/// Initializes blank order book.
|
|
MboBook()
|
|
: state_(
|
|
BookState::Constructed)
|
|
, bids_(
|
|
allocator())
|
|
, offers_(
|
|
allocator())
|
|
{
|
|
}
|
|
|
|
/// Initializes book using given memory pool.
|
|
MboBook(
|
|
MemoryPool& pool)
|
|
: MboBookBase(pool)
|
|
, state_(
|
|
BookState::Constructed)
|
|
, bids_(
|
|
allocator())
|
|
, offers_(
|
|
allocator())
|
|
{
|
|
}
|
|
|
|
/// Initializes as copy of other book.
|
|
MboBook(
|
|
const MboBook& other)
|
|
: MboBookBase()
|
|
, state_(other.state())
|
|
, bids_(allocator())
|
|
, offers_(allocator())
|
|
{
|
|
bids_ = other.bids();
|
|
offers_ = other.offers();
|
|
}
|
|
|
|
/// Disposes allocated resources.
|
|
~MboBook()
|
|
{
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
Entries& bids()
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
Entries&
|
|
operator [](
|
|
const Bids&)
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
const
|
|
Entries&
|
|
bids() const
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
const
|
|
Entries&
|
|
operator [](
|
|
const Bids&) const
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
Entries& offers()
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
Entries&
|
|
operator [](
|
|
const Offers&)
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
const
|
|
Entries&
|
|
offers() const
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
const
|
|
Entries&
|
|
operator [](
|
|
const Offers&) const
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// Indicates current state of book.
|
|
BookState::Enum state() const
|
|
{
|
|
return state_;
|
|
}
|
|
|
|
/// Updates state of book.
|
|
void
|
|
state(
|
|
BookState::Enum state)
|
|
{
|
|
state_ = state;
|
|
}
|
|
|
|
/// Wipes out all bids and offers and other
|
|
/// data associated with book (like instance
|
|
/// of last applied FIX message).
|
|
void
|
|
reset(
|
|
BookState::Enum bookState)
|
|
{
|
|
state(bookState);
|
|
|
|
bids_.clear();
|
|
offers_.clear();
|
|
}
|
|
|
|
/// Re-initializes as copy of other instance.
|
|
MboBook&
|
|
operator =(
|
|
const MboBook& other)
|
|
{
|
|
if (this != &other)
|
|
{
|
|
state_ = other.state();
|
|
|
|
bids_ = other.bids();
|
|
offers_ = other.offers();
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/// Copies content of MBO book to the other one.
|
|
inline
|
|
void
|
|
copy(
|
|
MboBook& target,
|
|
const MboBook& source)
|
|
{
|
|
target = source;
|
|
}
|
|
|
|
/// Book brief info.
|
|
ONIXS_CMEMDH_EXPORTED
|
|
void
|
|
brief(
|
|
std::string&,
|
|
const MboBook&);
|
|
|
|
/// Book brief info.
|
|
inline
|
|
std::string
|
|
brief(
|
|
const MboBook& book)
|
|
{
|
|
std::string str;
|
|
|
|
brief(str, book);
|
|
|
|
return str;
|
|
}
|
|
|
|
/// Serializes book.
|
|
ONIXS_CMEMDH_EXPORTED
|
|
void
|
|
toStr(
|
|
std::string&,
|
|
const MboBook&);
|
|
|
|
/// Serializes book.
|
|
inline
|
|
std::string
|
|
toStr(
|
|
const MboBook& book)
|
|
{
|
|
std::string str;
|
|
|
|
toStr(str, book);
|
|
|
|
return str;
|
|
}
|
|
|
|
/// Serializes book in formatted form.
|
|
ONIXS_CMEMDH_EXPORTED
|
|
void
|
|
toFormattedStr(
|
|
std::string&,
|
|
const MboBook&);
|
|
|
|
/// Serializes book in formatted form.
|
|
inline
|
|
std::string
|
|
toFormattedStr(
|
|
const MboBook& book)
|
|
{
|
|
std::string str;
|
|
|
|
toFormattedStr(str, book);
|
|
|
|
return str;
|
|
}
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_END
|