From 18e5317baf09f485f1550e8ab4825dc1edff4ef5 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 14 May 2022 18:23:58 +0200 Subject: [PATCH] Move code out of main.cpp --- script/main.wren | 21 ++++++++ src/bitcoinity_ticker_price.cpp | 53 ++++++++++++++++++ src/bitcoinity_ticker_price.hpp | 8 ++- src/main.cpp | 95 ++++++--------------------------- src/ticker_price.cpp | 14 +++++ src/ticker_price.hpp | 6 +++ subprojects/wrenpp | 2 +- 7 files changed, 117 insertions(+), 82 deletions(-) create mode 100644 script/main.wren diff --git a/script/main.wren b/script/main.wren new file mode 100644 index 0000000..081a03e --- /dev/null +++ b/script/main.wren @@ -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") diff --git a/src/bitcoinity_ticker_price.cpp b/src/bitcoinity_ticker_price.cpp index 3a9cbdd..8cee156 100644 --- a/src/bitcoinity_ticker_price.cpp +++ b/src/bitcoinity_ticker_price.cpp @@ -18,7 +18,11 @@ #include "bitcoinity_ticker_price.hpp" #include "bitcoinity_reader.hpp" +#include +#include + 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(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(vm, "ticker_price"); + *tp = bitcoinity_ticker_price(wren::get(vm, 1), wren::get(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(vm, "ticker_price"); + *tp = bitcoinity_ticker_price(g_dummy_exchange, wren::get(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(vm, "ticker_price"); + *tp = bitcoinity_ticker_price(wren::get(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(_)", ¤cy_ticker_price_bitcoinity_wren) + .add_callback(true, "bitcoinity", "Bitcoinity", "ticker_exchange(_)", &exchange_ticker_price_bitcoinity_wren); + + register_ticker_price_to_wren(vm); +} } //namespace duck diff --git a/src/bitcoinity_ticker_price.hpp b/src/bitcoinity_ticker_price.hpp index 80e69cc..738e26d 100644 --- a/src/bitcoinity_ticker_price.hpp +++ b/src/bitcoinity_ticker_price.hpp @@ -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 diff --git a/src/main.cpp b/src/main.cpp index ce694a9..1df4200 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,76 +18,9 @@ #include "bitcoinity_ticker_price.hpp" #include #include -#include -#include -#include -#include +#include 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(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(vm, "ticker_price"); - *tp = bitcoinity_ticker_price(wren::get(vm, 1), wren::get(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(vm, "ticker_price"); - *tp = bitcoinity_ticker_price(g_dummy_exchange, wren::get(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(vm, "ticker_price"); - *tp = bitcoinity_ticker_price(wren::get(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(ifs), + std::istreambuf_iterator() + ); +} } //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(_)", ¤cy_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); - 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(vm, {"main", "the_app"}, "start"); return 0; diff --git a/src/ticker_price.cpp b/src/ticker_price.cpp index c622815..5967629 100644 --- a/src/ticker_price.cpp +++ b/src/ticker_price.cpp @@ -16,6 +16,10 @@ */ #include "ticker_price.hpp" +#include +#include +#include +#include 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); +} } //namespace duck diff --git a/src/ticker_price.hpp b/src/ticker_price.hpp index 78a9391..78265da 100644 --- a/src/ticker_price.hpp +++ b/src/ticker_price.hpp @@ -19,6 +19,10 @@ #include +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 diff --git a/subprojects/wrenpp b/subprojects/wrenpp index 258237c..036dd57 160000 --- a/subprojects/wrenpp +++ b/subprojects/wrenpp @@ -1 +1 @@ -Subproject commit 258237cbf3b070f31defd5d5976bb96277ac9f65 +Subproject commit 036dd57524083f9d196f06a553aaeaaf1a17492d