Move code out of main.cpp

This commit is contained in:
King_DuckZ 2022-05-14 18:23:58 +02:00
commit 18e5317baf
7 changed files with 117 additions and 82 deletions

View file

@ -18,76 +18,9 @@
#include "bitcoinity_ticker_price.hpp"
#include <wrenpp/vm_fun.hpp>
#include <wrenpp/def_configuration.hpp>
#include <wrenpp/callback_manager.hpp>
#include <wrenpp/class_manager.hpp>
#include <utility>
#include <algorithm>
#include <fstream>
namespace {
constexpr std::string_view g_dummy_currency {"EUR"};
constexpr std::string_view g_dummy_exchange {"kraken"};
constexpr char g_script[] =
R"script(
import "bitcoinity" for Bitcoinity
class App {
construct new(exchange, currency) {
_currency = currency
_exchange = exchange
}
start() {
var price_quick = Bitcoinity.ticker_quick()
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)")
var price_currency = Bitcoinity.ticker_currency(_currency)
System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange)")
var price = Bitcoinity.ticker(_exchange, _currency)
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)")
}
}
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;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(g_dummy_exchange, g_dummy_currency, BitcoinityMatchType::Any);
}
void ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2), BitcoinityMatchType::Exact);
}
void currency_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(g_dummy_exchange, wren::get<const char*>(vm, 1), BitcoinityMatchType::CurrencyOnly);
}
void exchange_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), g_dummy_currency, BitcoinityMatchType::ExchangeOnly);
}
class MyWrenConfiguration : public wren::DefConfiguration {
public:
char* load_module_fn(wren::VM* vm, std::string_view module_name) {
@ -106,6 +39,16 @@ public:
return buff;
}
};
std::string load_string_from_file (const std::string& path) {
std::ifstream ifs(path);
ifs >> std::noskipws;
return std::string(
std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>()
);
}
} //unnamed namespace
int main() {
@ -113,19 +56,11 @@ int main() {
MyWrenConfiguration config;
wren::VM vm(&config, nullptr);
vm.callback_manager()
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_quick()", &quick_ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_currency(_)", &currency_ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_exchange(_)", &exchange_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.class_manager()
.add_class_maker("ticker_price", "TickerPrice", &wren::make_foreign_class<duck::TickerPrice>);
vm.interpret("main", g_script);
duck::register_bitcoinity_to_wren(vm);
std::string main_script = load_string_from_file("script/main.wren");
vm.interpret("main", main_script.c_str());
wren::call<void>(vm, {"main", "the_app"}, "start");
return 0;