diff --git a/src/bitcoinity_ticker_price.cpp b/src/bitcoinity_ticker_price.cpp index d961257..eaa4212 100644 --- a/src/bitcoinity_ticker_price.cpp +++ b/src/bitcoinity_ticker_price.cpp @@ -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); } diff --git a/src/bitcoinity_ticker_price.hpp b/src/bitcoinity_ticker_price.hpp index 721dd18..33edef4 100644 --- a/src/bitcoinity_ticker_price.hpp +++ b/src/bitcoinity_ticker_price.hpp @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 66ceb52..f2905e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,43 +20,47 @@ #include #include #include +#include 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(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(vm, "main"); - *tp = duck::bitcoinity_ticker_price(wren::get(vm, 1), wren::get(vm, 2)); + TickerPrice* const tp = wren::make_foreign_object(vm, "ticker_price"); + constexpr bool exact_match = true; + *tp = bitcoinity_ticker_price(wren::get(vm, 1), wren::get(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(); 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(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(vm, {"main", "the_app"}, "start"); diff --git a/src/ticker_price.cpp b/src/ticker_price.cpp index 76981ef..c622815 100644 --- a/src/ticker_price.cpp +++ b/src/ticker_price.cpp @@ -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 diff --git a/src/ticker_price.hpp b/src/ticker_price.hpp index 6f4f22c..78a9391 100644 --- a/src/ticker_price.hpp +++ b/src/ticker_price.hpp @@ -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