-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_struct_base.h
More file actions
158 lines (132 loc) · 4.53 KB
/
safe_struct_base.h
File metadata and controls
158 lines (132 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//x--------------------x
//| safe_struct_base.h |
//x--------------------x
#ifndef SAFE_STRUCT_BASE_H_
#define SAFE_STRUCT_BASE_H_
#include "fake_boost.h"
#include <limits>
#include <optional>
#include <string>
#include <sstream>
#include <cstring>
#include <vector>
#define ENUM_LLINT(x) enum class x: long long { invalid = std::numeric_limits<long long>::min() }
#define ENUM_CLASS(x, ...) enum class x{ invalid, __VA_ARGS__ }
namespace core_types
{
using fake_boost::flat_set;
constexpr int k_tag_len{ 20 };
typedef char tag[k_tag_len];
}
namespace session
{
ENUM_LLINT(sequencing_id);
ENUM_LLINT(source_id);
ENUM_LLINT(destination_id);
}
namespace account_management
{
ENUM_LLINT(account_id);
ENUM_CLASS(account_status, active, inactive);
}
namespace party_data
{
ENUM_LLINT(participant_id);
ENUM_CLASS(kyc_level, id_verified, address_verified);
}
namespace trading
{
ENUM_CLASS(self_trading_rule);
ENUM_CLASS(cancellation_policy, valid);
}
struct RetailParticipant
{
party_data::participant_id participant_id{ party_data::participant_id::invalid };
party_data::kyc_level kyc_level{ party_data::kyc_level::invalid };
constexpr bool operator<=>(const RetailParticipant&) const = default;
};
template <typename ParticipantT>
struct Account
{
session::sequencing_id sequencing_id{ session::sequencing_id::invalid };
account_management::account_id account_id{ account_management::account_id::invalid };
ParticipantT participant{};
account_management::account_status account_status{ account_management::account_status::invalid };
core_types::flat_set<core_types::tag> tags{};
std::optional<trading::self_trading_rule> self_trading_rule;
std::optional<trading::cancellation_policy> cancellation_policy;
constexpr bool operator<=>(const Account<ParticipantT>&) const = default;
std::string ToString()
{
std::stringstream ss;
using llong = long long;
for (long long t: std::vector<llong>{(llong)sequencing_id,
(llong)account_id,
(llong)participant.participant_id,
(llong)participant.kyc_level,
(llong)account_status})
{
ss << t << ',';
}
for (int i{}; i<tags.Size(); ++i)
{
if (strlen(tags.m_arr_[i]))
{
ss << "[" << tags.m_arr_[i] << "]";
}
}
if (self_trading_rule)
{
ss << (llong)(*self_trading_rule) << ',';
}
if (cancellation_policy)
{
ss << (llong)(*cancellation_policy) << ',';
}
return ss.str();
}
};
constexpr bool IsInvalid(const RetailParticipant& participant) noexcept
{
constexpr auto k_invalid{ RetailParticipant{} };
return participant.participant_id == k_invalid.participant_id ||
participant.kyc_level == k_invalid.kyc_level;
}
template <typename ParticipantT>
constexpr bool IsInvalid(const Account<ParticipantT>& account) noexcept
{
constexpr auto k_invalid{ Account<ParticipantT>{} };
return account.account_id == k_invalid.account_id ||
account.account_status == k_invalid.account_status ||
account.cancellation_policy == k_invalid.cancellation_policy ||
IsInvalid(account.participant) ||
account.self_trading_rule == k_invalid.self_trading_rule ||
account.sequencing_id == k_invalid.sequencing_id ||
account.tags == k_invalid.tags;
}
template <typename StructT>
auto CreateAccountValid(const StructT& struct_in)
{
return IsInvalid(struct_in)? std::optional<StructT>{}: std::optional<StructT>{ struct_in };
}
Account<RetailParticipant> CreateAccount( session::sequencing_id sid,
account_management::account_id aid,
party_data::participant_id pid,
account_management::account_status status,
party_data::kyc_level kyc,
core_types::flat_set<core_types::tag>&& tags,
trading::self_trading_rule str,
trading::cancellation_policy cp)
{
Account<RetailParticipant> account{};
account.sequencing_id = sid;
account.account_id = aid;
account.participant.participant_id = pid;
account.account_status = status;
account.participant.kyc_level = kyc;
account.tags = std::move(tags);
account.self_trading_rule = str;
account.cancellation_policy = cp;
return account;
}
#endif