-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathalert_context.cpp
More file actions
76 lines (69 loc) · 3.94 KB
/
Copy pathalert_context.cpp
File metadata and controls
76 lines (69 loc) · 3.94 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
#include "alert_context.hpp"
#include "longbridge.h"
#include "convert.hpp"
extern "C" {
const lb_alert_context_t* lb_alert_context_new(const lb_config_t*);
void lb_alert_context_retain(const lb_alert_context_t*);
void lb_alert_context_release(const lb_alert_context_t*);
void lb_alert_context_list(const lb_alert_context_t*, lb_async_callback_t, void*);
void lb_alert_context_add(const lb_alert_context_t*, const char*, lb_alert_condition_t, const char*, lb_alert_frequency_t, lb_async_callback_t, void*);
void lb_alert_context_update(const lb_alert_context_t*, const lb_alert_item_t*, lb_async_callback_t, void*);
}
namespace longbridge {
namespace alert {
AlertContext::AlertContext() : ctx_(nullptr) {}
AlertContext::AlertContext(const lb_alert_context_t* ctx) { ctx_ = ctx; if(ctx_) lb_alert_context_retain(ctx_); }
AlertContext::AlertContext(const AlertContext& ctx) { ctx_ = ctx.ctx_; if(ctx_) lb_alert_context_retain(ctx_); }
AlertContext::AlertContext(AlertContext&& ctx) { ctx_ = ctx.ctx_; ctx.ctx_ = nullptr; }
AlertContext::~AlertContext() { if(ctx_) lb_alert_context_release(ctx_); }
AlertContext& AlertContext::operator=(const AlertContext& ctx) { ctx_ = ctx.ctx_; if(ctx_) lb_alert_context_retain(ctx_); return *this; }
AlertContext AlertContext::create(const Config& config) { auto* ptr = lb_alert_context_new(config); AlertContext ctx(ptr); if(ptr) lb_alert_context_release(ptr); return ctx; }
void AlertContext::list(AsyncCallback<AlertContext, AlertList> callback) const {
lb_alert_context_list(ctx_,
[](auto res) {
auto cb = callback::get_async_callback<AlertContext, AlertList>(res->userdata);
AlertContext fctx((const lb_alert_context_t*)res->ctx);
Status status(res->error);
if (status) {
auto r = convert::convert((const lb_alert_list_t*)res->data);
(*cb)(AsyncResult<AlertContext, AlertList>(fctx, std::move(status), &r));
} else {
(*cb)(AsyncResult<AlertContext, AlertList>(fctx, std::move(status), nullptr));
}
}, new AsyncCallback<AlertContext, AlertList>(callback));
}
void AlertContext::add(const std::string& symbol, AlertCondition condition,
const std::string& trigger_value, AlertFrequency frequency,
AsyncCallback<AlertContext, void> callback) const {
lb_alert_context_add(ctx_, symbol.c_str(), (lb_alert_condition_t)condition, trigger_value.c_str(), (lb_alert_frequency_t)frequency,
[](auto res) {
auto cb = callback::get_async_callback<AlertContext, void>(res->userdata);
AlertContext fctx((const lb_alert_context_t*)res->ctx);
Status status(res->error);
(*cb)(AsyncResult<AlertContext, void>(fctx, std::move(status), nullptr));
}, new AsyncCallback<AlertContext, void>(callback));
}
void AlertContext::update(const AlertItem& item,
AsyncCallback<AlertContext, void> callback) const {
// Build a lb_alert_item_t from the C++ AlertItem to pass to the C layer.
std::vector<int32_t> state_copy = item.state;
lb_alert_item_t c_item{};
c_item.id = item.id.c_str();
c_item.indicator_id = item.indicator_id.c_str();
c_item.enabled = item.enabled;
c_item.frequency = item.frequency;
c_item.scope = item.scope;
c_item.text = item.text.c_str();
c_item.state = state_copy.data();
c_item.num_state = state_copy.size();
c_item.value_map = item.value_map.c_str();
lb_alert_context_update(ctx_, &c_item,
[](auto res) {
auto cb = callback::get_async_callback<AlertContext, void>(res->userdata);
AlertContext fctx((const lb_alert_context_t*)res->ctx);
Status status(res->error);
(*cb)(AsyncResult<AlertContext, void>(fctx, std::move(status), nullptr));
}, new AsyncCallback<AlertContext, void>(callback));
}
} // namespace alert
} // namespace longbridge