From 5bf036f056050d45ccdfda388c301a1a63952748 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 10 Aug 2020 19:19:06 +0100 Subject: [PATCH] Replace timers with a templated one They all do the same thing, the only difference is the method they call on oro::Api, so template on that --- src/evloop.cpp | 8 +++-- src/meson.build | 3 -- src/timer_base.cpp | 13 +++++++ src/timer_base.hpp | 6 ++++ src/{timer_shops.hpp => timer_generic.hpp} | 29 +++++++++++++-- src/timer_icons.cpp | 41 ---------------------- src/timer_icons.hpp | 38 -------------------- src/timer_items.cpp | 41 ---------------------- src/timer_items.hpp | 38 -------------------- src/timer_shops.cpp | 41 ---------------------- 10 files changed, 51 insertions(+), 207 deletions(-) rename src/{timer_shops.hpp => timer_generic.hpp} (64%) delete mode 100644 src/timer_icons.cpp delete mode 100644 src/timer_icons.hpp delete mode 100644 src/timer_items.cpp delete mode 100644 src/timer_items.hpp delete mode 100644 src/timer_shops.cpp diff --git a/src/evloop.cpp b/src/evloop.cpp index 4b70fab..c9578e1 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -16,9 +16,7 @@ */ #include "evloop.hpp" -#include "timer_items.hpp" -#include "timer_icons.hpp" -#include "timer_shops.hpp" +#include "timer_generic.hpp" #include "eventia/eventia.hpp" #include "roar11/ThreadPool.hpp" #include "oro/api.hpp" @@ -46,6 +44,10 @@ void join(roar11::ThreadPool& pool) { } //unnamed namespace void test(oro::Api* api, oro::OriginsDB* db) { + typedef TimerGeneric<&oro::Api::market_list> TimerShops; + typedef TimerGeneric<&oro::Api::items_list> TimerItems; + typedef TimerGeneric<&oro::Api::items_icons> TimerIcons; + const std::size_t thread_count = 2U; //std::min(std::max(3U, std::thread::hardware_concurrency()) - 1, 4U); std::cout << "Running with " << thread_count << " worker threads\n"; roar11::ThreadPool pool(thread_count); diff --git a/src/meson.build b/src/meson.build index ab5b388..daa8360 100644 --- a/src/meson.build +++ b/src/meson.build @@ -51,10 +51,7 @@ executable(meson.project_name(), 'evloop.cpp', 'eventia/eventia.cpp', 'eventia/timer.cpp', - 'timer_items.cpp', - 'timer_icons.cpp', 'timer_base.cpp', - 'timer_shops.cpp', 'oro/originsdb.cpp', 'gnulib/lib/base64.c', 'base64.cpp', diff --git a/src/timer_base.cpp b/src/timer_base.cpp index e89c17b..0209794 100644 --- a/src/timer_base.cpp +++ b/src/timer_base.cpp @@ -18,6 +18,7 @@ #include "timer_base.hpp" #include "roar11/ThreadPool.hpp" #include "oro/api.hpp" +#include "oro/originsdb.hpp" #include #include @@ -64,4 +65,16 @@ oro::OriginsDB& TimerBase::db() { return *m_db; } +void TimerBase::update_db (const oro::Shops& shops) { + db().update(shops); +} + +void TimerBase::update_db (const oro::Items& items) { + db().update(items); +} + +void TimerBase::update_db (const oro::Icons& icons) { + db().update(icons); +} + } //namespace duck diff --git a/src/timer_base.hpp b/src/timer_base.hpp index 4659744..eb4ade8 100644 --- a/src/timer_base.hpp +++ b/src/timer_base.hpp @@ -28,6 +28,9 @@ namespace oro { class Api; class OriginsDB; struct Header; + struct Shops; + struct Icons; + struct Items; } //namespace oro namespace duck { @@ -47,6 +50,9 @@ public: protected: void set_next_timer (const oro::Header& header); + void update_db (const oro::Shops& shops); + void update_db (const oro::Items& items); + void update_db (const oro::Icons& icons); roar11::ThreadPool& pool(); oro::Api& oro_api(); oro::OriginsDB& db(); diff --git a/src/timer_shops.hpp b/src/timer_generic.hpp similarity index 64% rename from src/timer_shops.hpp rename to src/timer_generic.hpp index a2a7f57..ab63a4b 100644 --- a/src/timer_shops.hpp +++ b/src/timer_generic.hpp @@ -19,11 +19,16 @@ #include "timer_base.hpp" +namespace oro { + class Api; +} //namespace oro + namespace duck { -class TimerShops : TimerBase { +template +class TimerGeneric : TimerBase { public: - TimerShops ( + TimerGeneric ( const eve::Context& ctx, double timeout, roar11::ThreadPool* pool, @@ -35,4 +40,24 @@ private: virtual void fetch_data() override; }; +template +inline TimerGeneric::TimerGeneric ( + const eve::Context& ctx, + double timeout, + roar11::ThreadPool* pool, + oro::Api* oro_api, + oro::OriginsDB* db +) : + TimerBase(ctx, timeout, pool, oro_api, db) +{ +} + +template +inline void TimerGeneric::fetch_data() { + auto results = (oro_api().*Method)(); + set_next_timer(results.first); + this->update_db(results.second); +} + } //namespace duck + diff --git a/src/timer_icons.cpp b/src/timer_icons.cpp deleted file mode 100644 index 0e5d684..0000000 --- a/src/timer_icons.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of orotool. - * - * Orotool is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Orotool is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Orotool. If not, see . - */ - -#include "timer_icons.hpp" -#include "oro/originsdb.hpp" -#include "oro/api.hpp" -#include - -namespace duck { - TimerIcons::TimerIcons ( - const eve::Context& ctx, - double timeout, - roar11::ThreadPool* pool, - oro::Api* oro_api, - oro::OriginsDB* db - ) : - TimerBase(ctx, timeout, pool, oro_api, db) - { - } - - void TimerIcons::fetch_data() { - auto icons = oro_api().items_icons(); - set_next_timer(icons.first); - db().update(icons.second); - } - -} //namespace duck diff --git a/src/timer_icons.hpp b/src/timer_icons.hpp deleted file mode 100644 index 043feb5..0000000 --- a/src/timer_icons.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of orotool. - * - * Orotool is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Orotool is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Orotool. If not, see . - */ - -#pragma once - -#include "timer_base.hpp" - -namespace duck { - -class TimerIcons : TimerBase { -public: - TimerIcons ( - const eve::Context& ctx, - double timeout, - roar11::ThreadPool* pool, - oro::Api* oro_api, - oro::OriginsDB* db - ); - -private: - virtual void fetch_data() override; -}; - -} //namespace duck diff --git a/src/timer_items.cpp b/src/timer_items.cpp deleted file mode 100644 index a4d6ca1..0000000 --- a/src/timer_items.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of orotool. - * - * Orotool is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Orotool is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Orotool. If not, see . - */ - -#include "timer_items.hpp" -#include "oro/originsdb.hpp" -#include "oro/api.hpp" -#include - -namespace duck { - TimerItems::TimerItems ( - const eve::Context& ctx, - double timeout, - roar11::ThreadPool* pool, - oro::Api* oro_api, - oro::OriginsDB* db - ) : - TimerBase(ctx, timeout, pool, oro_api, db) - { - } - - void TimerItems::fetch_data() { - auto items = oro_api().items_list(); - set_next_timer(items.first); - db().update(items.second); - } - -} //namespace duck diff --git a/src/timer_items.hpp b/src/timer_items.hpp deleted file mode 100644 index 2cf7157..0000000 --- a/src/timer_items.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of orotool. - * - * Orotool is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Orotool is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Orotool. If not, see . - */ - -#pragma once - -#include "timer_base.hpp" - -namespace duck { - -class TimerItems : TimerBase { -public: - TimerItems ( - const eve::Context& ctx, - double timeout, - roar11::ThreadPool* pool, - oro::Api* oro_api, - oro::OriginsDB* db - ); - -private: - virtual void fetch_data() override; -}; - -} //namespace duck diff --git a/src/timer_shops.cpp b/src/timer_shops.cpp deleted file mode 100644 index 242cea5..0000000 --- a/src/timer_shops.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of orotool. - * - * Orotool is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Orotool is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Orotool. If not, see . - */ - -#include "timer_shops.hpp" -#include "oro/originsdb.hpp" -#include "oro/api.hpp" -#include - -namespace duck { - TimerShops::TimerShops ( - const eve::Context& ctx, - double timeout, - roar11::ThreadPool* pool, - oro::Api* oro_api, - oro::OriginsDB* db - ) : - TimerBase(ctx, timeout, pool, oro_api, db) - { - } - - void TimerShops::fetch_data() { - auto shops = oro_api().market_list(); - set_next_timer(shops.first); - db().update(shops.second); - } - -} //namespace duck