NetworkListener: use Pimpl idiom (#1405)

This enables different platforms to use very different implementations.
This commit is contained in:
Peter Johnson
2018-11-02 13:14:06 -07:00
committed by GitHub
parent fb557f49ea
commit b6830638df
4 changed files with 41 additions and 60 deletions

View File

@@ -8,8 +8,9 @@
#ifndef CSCORE_NETWORKLISTENER_H_
#define CSCORE_NETWORKLISTENER_H_
#include <memory>
#include <wpi/Logger.h>
#include <wpi/SafeThread.h>
namespace cs {
@@ -17,19 +18,15 @@ class Notifier;
class NetworkListener {
public:
NetworkListener(wpi::Logger& logger, Notifier& notifier)
: m_logger(logger), m_notifier(notifier) {}
NetworkListener(wpi::Logger& logger, Notifier& notifier);
~NetworkListener();
void Start();
void Stop();
private:
wpi::Logger& m_logger;
Notifier& m_notifier;
class Thread;
wpi::SafeThreadOwner<Thread> m_owner;
class Impl;
std::unique_ptr<Impl> m_impl;
};
} // namespace cs

View File

@@ -18,36 +18,54 @@
#include <cerrno>
#include <wpi/SafeThread.h>
#include "Log.h"
#include "Notifier.h"
using namespace cs;
class NetworkListener::Thread : public wpi::SafeThread {
class NetworkListener::Impl {
public:
Thread(wpi::Logger& logger, Notifier& notifier)
Impl(wpi::Logger& logger, Notifier& notifier)
: m_logger(logger), m_notifier(notifier) {}
void Main();
wpi::Logger& m_logger;
Notifier& m_notifier;
int m_command_fd = -1;
class Thread : public wpi::SafeThread {
public:
Thread(wpi::Logger& logger, Notifier& notifier)
: m_logger(logger), m_notifier(notifier) {}
void Main();
wpi::Logger& m_logger;
Notifier& m_notifier;
int m_command_fd = -1;
};
wpi::SafeThreadOwner<Thread> m_owner;
};
NetworkListener::NetworkListener(wpi::Logger& logger, Notifier& notifier)
: m_impl(std::make_unique<Impl>(logger, notifier)) {}
NetworkListener::~NetworkListener() { Stop(); }
void NetworkListener::Start() { m_owner.Start(m_logger, m_notifier); }
void NetworkListener::Start() {
m_impl->m_owner.Start(m_impl->m_logger, m_impl->m_notifier);
}
void NetworkListener::Stop() {
// Wake up thread
if (auto thr = m_owner.GetThread()) {
if (auto thr = m_impl->m_owner.GetThread()) {
thr->m_active = false;
if (thr->m_command_fd >= 0) eventfd_write(thr->m_command_fd, 1);
}
m_owner.Stop();
m_impl->m_owner.Stop();
}
void NetworkListener::Thread::Main() {
void NetworkListener::Impl::Thread::Main() {
// Create event socket so we can be shut down
m_command_fd = ::eventfd(0, 0);
if (m_command_fd < 0) {

View File

@@ -7,31 +7,14 @@
#include "NetworkListener.h"
#include "Log.h"
#include "Notifier.h"
using namespace cs;
class NetworkListener::Thread : public wpi::SafeThread {
public:
void Main();
};
class NetworkListener::Impl {};
NetworkListener::~NetworkListener() { Stop(); }
NetworkListener::NetworkListener(wpi::Logger& logger, Notifier& notifier) {}
void NetworkListener::Start() {
auto thr = m_owner.GetThread();
if (!thr) m_owner.Start();
}
NetworkListener::~NetworkListener() {}
void NetworkListener::Stop() {
// Wake up thread
if (auto thr = m_owner.GetThread()) {
thr->m_active = false;
}
m_owner.Stop();
}
void NetworkListener::Start() {}
void NetworkListener::Thread::Main() {
// TODO
}
void NetworkListener::Stop() {}

View File

@@ -7,31 +7,14 @@
#include "NetworkListener.h"
#include "Log.h"
#include "Notifier.h"
using namespace cs;
class NetworkListener::Thread : public wpi::SafeThread {
public:
void Main();
};
class NetworkListener::Impl {};
NetworkListener::~NetworkListener() { Stop(); }
NetworkListener::NetworkListener(wpi::Logger& logger, Notifier& notifier) {}
void NetworkListener::Start() {
auto thr = m_owner.GetThread();
if (!thr) m_owner.Start();
}
NetworkListener::~NetworkListener() {}
void NetworkListener::Stop() {
// Wake up thread
if (auto thr = m_owner.GetThread()) {
thr->m_active = false;
}
m_owner.Stop();
}
void NetworkListener::Start() {}
void NetworkListener::Thread::Main() {
// TODO
}
void NetworkListener::Stop() {}