ADD: py_spread.h..
This commit is contained in:
1 parent
0e331229d4
commit
c15968e17e
1 file changed
+98
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include "spark/core/FMT_IMPL.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace bts::md::py_interop {
|
||||
|
||||
/**
|
||||
* Spread contract definition.
|
||||
*/
|
||||
class PySpread {
|
||||
public:
|
||||
const std::string &spread_type() const;
|
||||
void set_spread_type(std::string value);
|
||||
|
||||
const std::vector<std::string> &legs() const;
|
||||
void set_legs(std::vector<std::string> value);
|
||||
|
||||
const std::string &expiration() const;
|
||||
void set_expiration(std::string value);
|
||||
|
||||
std::string to_string() const;
|
||||
|
||||
private:
|
||||
std::string spread_type_;
|
||||
std::vector<std::string> legs_;
|
||||
std::string expiration_;
|
||||
};
|
||||
|
||||
void to_json(nlohmann::json &j, const PySpread &spread);
|
||||
void from_json(const nlohmann::json &j, PySpread &spread);
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const PySpread &spread) noexcept;
|
||||
|
||||
} // namespace bts::md::py_interop
|
||||
|
||||
FMT_IMPL(
|
||||
bts::md::py_interop::PySpread, s,
|
||||
R"({{ "type" : "{}", "legs" : "{}", "exp" : "{}" }})",
|
||||
s.spread_type(), fmt::join(s.legs(), ","), s.expiration());
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
namespace bts::md::py_interop {
|
||||
|
||||
inline const std::string &PySpread::spread_type() const { return spread_type_; }
|
||||
inline void PySpread::set_spread_type(std::string value) {
|
||||
spread_type_ = std::move(value);
|
||||
}
|
||||
|
||||
inline const std::vector<std::string> &PySpread::legs() const { return legs_; }
|
||||
inline void PySpread::set_legs(std::vector<std::string> value) {
|
||||
legs_ = std::move(value);
|
||||
}
|
||||
|
||||
inline const std::string &PySpread::expiration() const { return expiration_; }
|
||||
inline void PySpread::set_expiration(std::string value) {
|
||||
expiration_ = std::move(value);
|
||||
}
|
||||
|
||||
inline std::string PySpread::to_string() const {
|
||||
return fmt::format("{}", *this);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(
|
||||
std::ostream &out, const PySpread &spread) noexcept {
|
||||
out << fmt::format("{}", spread);
|
||||
return out;
|
||||
}
|
||||
|
||||
// JSON serialization implementation
|
||||
inline void to_json(nlohmann::json &j, const PySpread &spread) {
|
||||
j = nlohmann::json{
|
||||
{"spread_type", spread.spread_type()},
|
||||
{"legs", spread.legs()},
|
||||
{"expiration", spread.expiration()}
|
||||
};
|
||||
}
|
||||
|
||||
inline void from_json(const nlohmann::json &j, PySpread &spread) {
|
||||
if (j.contains("spread_type")) {
|
||||
spread.set_spread_type(j["spread_type"].get<std::string>());
|
||||
}
|
||||
if (j.contains("legs")) {
|
||||
spread.set_legs(j["legs"].get<std::vector<std::string>>());
|
||||
}
|
||||
if (j.contains("expiration")) {
|
||||
spread.set_expiration(j["expiration"].get<std::string>());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::md::py_interop
|
||||
Reference in new issue
Block a user