Split out wren code, add a ticker_quick() method

This commit is contained in:
King_DuckZ 2022-05-14 07:42:34 +02:00
parent 4121bb3c74
commit 6bd0dbb07e
5 changed files with 119 additions and 38 deletions

View file

@ -19,7 +19,16 @@
#include "bitcoinity_reader.hpp"
namespace duck {
TickerPrice bitcoinity_ticker_price (std::string_view currency, std::string_view exchange) {
const std::string_view g_bitcoinity_wren_module {
R"wren(import "ticker_price" for TickerPrice
class Bitcoinity {
foreign static ticker(currency, exchange)
foreign static ticker_quick()
}
)wren"};
TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view currency, bool exact_match) {
static const TickerPrice error_price {0.0, "", ""};
duck::BitcoinityReader bitcoinity{
@ -53,6 +62,9 @@ TickerPrice bitcoinity_ticker_price (std::string_view currency, std::string_view
std::string_view currency_out = msg.payload.get_string_or_empty("data", "currency");
std::string_view exchange_out = msg.payload.get_string_or_empty("data", "exchange_name");
if (exact_match and (currency_out != currency or exchange_out != exchange))
continue; //wait 'til we get a better match
return {price_out, std::string(currency_out), std::string(exchange_out)};
} while (true);
}

View file

@ -20,5 +20,11 @@
#include "ticker_price.hpp"
namespace duck {
TickerPrice bitcoinity_ticker_price (std::string_view currency, std::string_view exchange);
TickerPrice bitcoinity_ticker_price (
std::string_view exchange,
std::string_view currency,
bool exact_match
);
extern const std::string_view g_bitcoinity_wren_module;
} //namespace duck

View file

@ -20,43 +20,47 @@
#include <wrenpp/def_configuration.hpp>
#include <wrenpp/callback_manager.hpp>
#include <utility>
#include <algorithm>
namespace {
constexpr char g_script[] =
R"script(
foreign class TickerPrice {
construct new() { }
foreign price
foreign currency
foreign exchange
foreign is_valid
}
class Bitcoinity {
foreign static ticker(currency, exchange)
}
import "bitcoinity" for Bitcoinity
class App {
construct new(currency, exchange) {
construct new(exchange, currency) {
_currency = currency
_exchange = exchange
}
start() {
var price = Bitcoinity.ticker(_currency, _exchange)
System.print("Ticker: %(price.price) %(price.currency) on %(price.exchange)")
var price_quick = Bitcoinity.ticker_quick()
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)")
var price = Bitcoinity.ticker(_exchange, _currency)
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)")
}
}
var the_app = App.new("USD", "kraken")
var the_app = App.new("kraken", "USD")
)script";
void quick_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = false;
*tp = bitcoinity_ticker_price("kraken", "EUR", exact_match);
}
void ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "main");
*tp = duck::bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2));
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = true;
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2), exact_match);
}
class MyWrenConfiguration : public wren::DefConfiguration {
@ -66,11 +70,27 @@ public:
std::string_view module,
std::string_view class_name
) {
if (module == "main" and class_name == "TickerPrice")
if (module == "ticker_price" and class_name == "TickerPrice")
return wren::make_foreign_class<duck::TickerPrice>();
else
return {nullptr, nullptr};
}
char* load_module_fn(wren::VM* vm, std::string_view module_name) {
std::string_view wren_code;
if (module_name == "bitcoinity")
wren_code = duck::g_bitcoinity_wren_module;
else if (module_name == "ticker_price")
wren_code = duck::g_ticker_price_wren_module;
else
return nullptr;
const std::size_t buff_sz = wren_code.size();
char* const buff = static_cast<char*>(MyWrenConfiguration::reallocate_fn(nullptr, buff_sz));
std::copy(wren_code.cbegin(), wren_code.cend(), buff);
return buff;
}
};
} //unnamed namespace
@ -80,11 +100,12 @@ int main() {
MyWrenConfiguration config;
wren::VM vm(&config, nullptr);
vm.callback_manager()
.add_callback(true, "main", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(false, "main", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
.add_callback(false, "main", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>())
.add_callback(false, "main", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>())
.add_callback(false, "main", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>());
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_quick()", &quick_ticker_price_bitcoinity_wren)
.add_callback(false, "ticker_price", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
.add_callback(false, "ticker_price", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>())
.add_callback(false, "ticker_price", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>())
.add_callback(false, "ticker_price", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>());
vm.interpret("main", g_script);
wren::call<void>(vm, {"main", "the_app"}, "start");

View file

@ -18,4 +18,48 @@
#include "ticker_price.hpp"
namespace duck {
const std::string_view g_ticker_price_wren_module {
R"wren(foreign class TickerPrice {
construct new() { }
foreign price
foreign currency
foreign exchange
foreign is_valid
}
)wren"};
TickerPrice::TickerPrice (double price, std::string&& currency, std::string&& exchange) :
m_price(price),
m_currency(std::move(currency)),
m_exchange(std::move(exchange))
{}
void TickerPrice::set_price (double price) {
m_price = price;
}
void TickerPrice::set_currency (std::string&& currency) {
m_currency = std::move(currency);
}
void TickerPrice::set_exchange (std::string&& exchange) {
m_exchange = std::move(exchange);
}
double TickerPrice::price() const noexcept {
return m_price;
}
const char* TickerPrice::currency() const noexcept {
return m_currency.c_str();
}
const char* TickerPrice::exchange() const noexcept {
return m_exchange.c_str();
}
bool TickerPrice::is_valid() const noexcept {
return not m_exchange.empty();
}
} //namespace duck

View file

@ -23,23 +23,21 @@ namespace duck {
class TickerPrice {
public:
TickerPrice() = default;
TickerPrice (double price, std::string&& currency, std::string&& exchange) :
m_price(price),
m_currency(std::move(currency)),
m_exchange(std::move(exchange))
{}
TickerPrice (double price, std::string&& currency, std::string&& exchange);
void set_price (double price) { m_price = price; }
void set_currency (std::string&& currency) { m_currency = std::move(currency); }
void set_exchange (std::string&& exchange) { m_exchange = std::move(exchange); }
double price() const noexcept { return m_price; }
const char* currency() const noexcept { return m_currency.c_str(); }
const char* exchange() const noexcept { return m_exchange.c_str(); }
bool is_valid() const noexcept { return not m_exchange.empty(); }
void set_price (double price);
void set_currency (std::string&& currency);
void set_exchange (std::string&& exchange);
double price() const noexcept;
const char* currency() const noexcept;
const char* exchange() const noexcept;
bool is_valid() const noexcept;
private:
double m_price{};
std::string m_currency;
std::string m_exchange;
};
extern const std::string_view g_ticker_price_wren_module;
} //namespace duck