Move code out of main.cpp

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

21
script/main.wren Normal file
View file

@ -0,0 +1,21 @@
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")

View file

@ -18,7 +18,11 @@
#include "bitcoinity_ticker_price.hpp"
#include "bitcoinity_reader.hpp"
#include <wrenpp/vm_fun.hpp>
#include <wrenpp/callback_manager.hpp>
namespace duck {
const std::string_view g_bitcoinity_wren_module {
R"wren(import "ticker_price" for TickerPrice
class Bitcoinity {
@ -29,6 +33,46 @@ class Bitcoinity {
}
)wren"};
namespace {
constexpr std::string_view g_dummy_currency {"EUR"};
constexpr std::string_view g_dummy_exchange {"kraken"};
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);
}
} //unnamed namespace
TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view currency, BitcoinityMatchType match_type) {
static const TickerPrice error_price {0.0, "", ""};
@ -85,4 +129,13 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view
} while (true);
}
void register_bitcoinity_to_wren (wren::VM& vm) {
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);
register_ticker_price_to_wren(vm);
}
} //namespace duck

View file

@ -19,6 +19,10 @@
#include "ticker_price.hpp"
namespace wren {
class VM;
} //namespace wren
namespace duck {
enum class BitcoinityMatchType {
Any,
@ -27,11 +31,13 @@ enum class BitcoinityMatchType {
ExchangeOnly
};
extern const std::string_view g_bitcoinity_wren_module;
TickerPrice bitcoinity_ticker_price (
std::string_view exchange,
std::string_view currency,
BitcoinityMatchType match_type
);
extern const std::string_view g_bitcoinity_wren_module;
void register_bitcoinity_to_wren (wren::VM& vm);
} //namespace duck

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;

View file

@ -16,6 +16,10 @@
*/
#include "ticker_price.hpp"
#include <wrenpp/vm_fun.hpp>
#include <wrenpp/callback_manager.hpp>
#include <wrenpp/class_manager.hpp>
#include <utility>
namespace duck {
const std::string_view g_ticker_price_wren_module {
@ -62,4 +66,14 @@ const char* TickerPrice::exchange() const noexcept {
bool TickerPrice::is_valid() const noexcept {
return not m_exchange.empty();
}
void register_ticker_price_to_wren(wren::VM& vm) {
vm.callback_manager()
.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>);
}
} //namespace duck

View file

@ -19,6 +19,10 @@
#include <string>
namespace wren {
class VM;
} //namespace wren
namespace duck {
class TickerPrice {
public:
@ -40,4 +44,6 @@ private:
};
extern const std::string_view g_ticker_price_wren_module;
void register_ticker_price_to_wren(wren::VM& vm);
} //namespace duck

@ -1 +1 @@
Subproject commit 258237cbf3b070f31defd5d5976bb96277ac9f65
Subproject commit 036dd57524083f9d196f06a553aaeaaf1a17492d