Files
spark/src/lib/domain/Side.h
T

156 lines
3.8 KiB
C++

#pragma once
#include <ostream>
namespace bts::domain {
class Side {
public:
enum Value { Buy = 0, Sell = 1 };
Value value;
Side() = default;
constexpr Side(Side::Value value) noexcept; // NOLINT
Side(const Side &) noexcept = default;
Side(Side &&) noexcept = default;
Side &operator=(const Side &) noexcept = default;
Side &operator=(Side &&) noexcept = default;
~Side() = default;
static constexpr Side from_contract_flow(int flow);
[[nodiscard]] constexpr size_t as_index() const noexcept;
[[nodiscard]] constexpr int as_contract_flow() const noexcept;
[[nodiscard]] constexpr int as_cash_flow() const noexcept;
[[nodiscard]] constexpr const char *name() const noexcept;
[[nodiscard]] constexpr Side other() const noexcept;
template <class NumericT>
constexpr NumericT normalize(NumericT numeric) const noexcept;
template <class T>
[[nodiscard]] T &pick(T &pick_if_buy, T &pick_if_sell) const;
template <class T>
[[nodiscard]] const T &pick(
const T &pick_if_buy, const T &pick_if_sell) const;
template <class C>
static void foreach (C c);
template <class C>
static bool foreither(C c);
template <class C>
static bool forboth(C c);
};
constexpr bool operator==(Side lhs, Side rhs) noexcept;
constexpr bool operator==(Side lhs, Side::Value rhs) noexcept;
constexpr bool operator!=(Side lhs, Side rhs) noexcept;
constexpr bool operator!=(Side lhs, Side::Value rhs) noexcept;
constexpr Side operator*(Side lhs, Side rhs) noexcept;
constexpr Side operator*(Side lhs, Side::Value rhs) noexcept;
std::ostream &operator<<(std::ostream &out, const Side &s);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
constexpr Side Side::from_contract_flow(int flow) {
if (flow > 0) {
return Side::Buy;
}
return Side::Sell;
}
constexpr Side::Side(Side::Value value) noexcept : value(value) {}
constexpr std::size_t Side::as_index() const noexcept { return (int)value; }
constexpr int Side::as_contract_flow() const noexcept {
if (value == Buy) {
return 1;
}
return -1;
}
constexpr int Side::as_cash_flow() const noexcept {
if (value == Buy) {
return -1;
}
return 1;
}
constexpr const char *Side::name() const noexcept {
if (value == Side::Buy) {
return "Buy";
}
return "Sell";
}
constexpr Side Side::other() const noexcept {
if (value == Side::Buy) {
return {Side::Sell};
}
return {Side::Buy};
}
template <class NumericT>
constexpr NumericT Side::normalize(NumericT numeric) const noexcept {
return value == Side::Buy ? numeric : -numeric;
}
template <class T>
T &Side::pick(T &pick_if_buy, T &pick_if_sell) const {
return value == Side::Buy ? pick_if_buy : pick_if_sell;
}
template <class T>
const T &Side::pick(const T &pick_if_buy, const T &pick_if_sell) const {
return value == Side::Buy ? pick_if_buy : pick_if_sell;
}
template <class C>
void Side::foreach (C c) {
c(Side(Side::Buy));
c(Side(Side::Sell));
}
template <class C>
bool Side::foreither(C c) {
return c(Side(Side::Buy)) || c(Side(Side::Sell));
}
template <class C>
bool Side::forboth(C c) {
return c(Side(Side::Buy)) && c(Side(Side::Sell));
}
constexpr bool operator==(Side lhs, Side rhs) noexcept {
return lhs.value == rhs.value;
}
constexpr bool operator==(Side lhs, Side::Value rhs) noexcept {
return lhs.value == rhs;
}
constexpr bool operator!=(Side lhs, Side rhs) noexcept {
return lhs.value != rhs.value;
}
constexpr bool operator!=(Side lhs, Side::Value rhs) noexcept {
return lhs.value != rhs;
}
constexpr Side operator*(Side lhs, Side rhs) noexcept {
return rhs == Side::Buy ? lhs : lhs.other();
}
constexpr Side operator*(Side lhs, Side::Value rhs) noexcept {
return rhs == Side::Buy ? lhs : lhs.other();
}
} // namespace bts::domain