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
This commit is contained in:
King_DuckZ 2020-08-10 19:19:06 +01:00
parent 4b3d88b2f0
commit 5bf036f056
10 changed files with 51 additions and 207 deletions

View file

@ -16,9 +16,7 @@
*/ */
#include "evloop.hpp" #include "evloop.hpp"
#include "timer_items.hpp" #include "timer_generic.hpp"
#include "timer_icons.hpp"
#include "timer_shops.hpp"
#include "eventia/eventia.hpp" #include "eventia/eventia.hpp"
#include "roar11/ThreadPool.hpp" #include "roar11/ThreadPool.hpp"
#include "oro/api.hpp" #include "oro/api.hpp"
@ -46,6 +44,10 @@ void join(roar11::ThreadPool& pool) {
} //unnamed namespace } //unnamed namespace
void test(oro::Api* api, oro::OriginsDB* db) { 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); 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"; std::cout << "Running with " << thread_count << " worker threads\n";
roar11::ThreadPool pool(thread_count); roar11::ThreadPool pool(thread_count);

View file

@ -51,10 +51,7 @@ executable(meson.project_name(),
'evloop.cpp', 'evloop.cpp',
'eventia/eventia.cpp', 'eventia/eventia.cpp',
'eventia/timer.cpp', 'eventia/timer.cpp',
'timer_items.cpp',
'timer_icons.cpp',
'timer_base.cpp', 'timer_base.cpp',
'timer_shops.cpp',
'oro/originsdb.cpp', 'oro/originsdb.cpp',
'gnulib/lib/base64.c', 'gnulib/lib/base64.c',
'base64.cpp', 'base64.cpp',

View file

@ -18,6 +18,7 @@
#include "timer_base.hpp" #include "timer_base.hpp"
#include "roar11/ThreadPool.hpp" #include "roar11/ThreadPool.hpp"
#include "oro/api.hpp" #include "oro/api.hpp"
#include "oro/originsdb.hpp"
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
@ -64,4 +65,16 @@ oro::OriginsDB& TimerBase::db() {
return *m_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 } //namespace duck

View file

@ -28,6 +28,9 @@ namespace oro {
class Api; class Api;
class OriginsDB; class OriginsDB;
struct Header; struct Header;
struct Shops;
struct Icons;
struct Items;
} //namespace oro } //namespace oro
namespace duck { namespace duck {
@ -47,6 +50,9 @@ public:
protected: protected:
void set_next_timer (const oro::Header& header); 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(); roar11::ThreadPool& pool();
oro::Api& oro_api(); oro::Api& oro_api();
oro::OriginsDB& db(); oro::OriginsDB& db();

View file

@ -19,11 +19,16 @@
#include "timer_base.hpp" #include "timer_base.hpp"
namespace oro {
class Api;
} //namespace oro
namespace duck { namespace duck {
class TimerShops : TimerBase { template<auto Method>
class TimerGeneric : TimerBase {
public: public:
TimerShops ( TimerGeneric (
const eve::Context& ctx, const eve::Context& ctx,
double timeout, double timeout,
roar11::ThreadPool* pool, roar11::ThreadPool* pool,
@ -35,4 +40,24 @@ private:
virtual void fetch_data() override; virtual void fetch_data() override;
}; };
template<auto Method>
inline TimerGeneric<Method>::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<auto Method>
inline void TimerGeneric<Method>::fetch_data() {
auto results = (oro_api().*Method)();
set_next_timer(results.first);
this->update_db(results.second);
}
} //namespace duck } //namespace duck

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "timer_icons.hpp"
#include "oro/originsdb.hpp"
#include "oro/api.hpp"
#include <iostream>
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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "timer_items.hpp"
#include "oro/originsdb.hpp"
#include "oro/api.hpp"
#include <iostream>
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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "timer_shops.hpp"
#include "oro/originsdb.hpp"
#include "oro/api.hpp"
#include <iostream>
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