refactoring to interfaces complete

This commit is contained in:
Jonas Zeunert
2018-06-14 21:37:54 +02:00
parent 13b5cc0b33
commit 692a2bd68f
11 changed files with 41 additions and 37 deletions

View File

@@ -15,7 +15,7 @@
namespace Input
{
Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, EventNotifier* event_notifier) :
Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, IEventNotifier* event_notifier) :
input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier)
{
detect_thread = std::thread(&Detector::detect, this);

View File

@@ -19,11 +19,11 @@
namespace Input
{
class Detector : IDetector
class Detector : public IDetector
{
public:
Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, EventNotifier* event_notifier);
Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, IEventNotifier* event_notifier);
~Detector();
private:
@@ -35,7 +35,7 @@ private:
std::map<char, Event> events;
EventNotifier* event_notifier;
IEventNotifier* event_notifier;
bool is_running;
std::thread detect_thread;

View File

@@ -11,26 +11,21 @@
#ifndef INPUTEVENTHANDLER_H_
#define INPUTEVENTHANDLER_H_
#include "../utilities/config.h"
#include "IEventHandler.h"
#include "Event.hpp"
#include "IInputDriver.h"
#include "../utilities/config.h"
namespace Input
{
class EventHandler;
class InputDriver
class EventHandler : public IEventHandler
{
public:
void register_event_handler(EventHandler* handler);
void unregister_event_handler(EventHandler* handler);
};
class EventHandler
{
public:
EventHandler(std::shared_ptr<InputDriver> input_driver) :
EventHandler(std::shared_ptr<IInputDriver> input_driver) :
input_driver(input_driver)
{
this->input_driver->register_event_handler(this);
@@ -51,7 +46,7 @@ public:
}
private:
std::shared_ptr<InputDriver> input_driver;
std::shared_ptr<IInputDriver> input_driver;
};
}

View File

@@ -41,7 +41,7 @@ void EventNotifier::unregister_event_handler(IEventHandler* handler)
event_handler.erase(handler);
}
void EventNotifier::distribute_event(IEvent& event)
void EventNotifier::distribute_event(Event& event)
{
event_queue.push(event);
}
@@ -56,7 +56,7 @@ void EventNotifier::notify()
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
for(auto handler : event_handler)
{
boost::thread handler_caller(boost::bind(&EventHandler::handle, handler, event));
boost::thread handler_caller(boost::bind(&IEventHandler::handle, handler, event));
if(!handler_caller.timed_join(boost::posix_time::milliseconds(HANDLER_TIMEOUT)))
{

View File

@@ -23,7 +23,7 @@
namespace Input
{
class EventNotifier : IEventNotifier
class EventNotifier : public IEventNotifier
{
public:
@@ -33,13 +33,13 @@ public:
void register_event_handler(IEventHandler* handler);
void unregister_event_handler(IEventHandler* handler);
void distribute_event(IEvent& event);
void distribute_event(Event& event);
private:
void notify();
private:
BlockingQueue<IEvent> event_queue;
BlockingQueue<Event> event_queue;
std::set<IEventHandler*> event_handler;
bool is_running;

View File

@@ -8,11 +8,16 @@
#ifndef SRC_INPUT_IEVENTHANDLER_H_
#define SRC_INPUT_IEVENTHANDLER_H_
#include "Event.hpp"
namespace Input {
class IEventHandler
{
public:
virtual ~IEventHandler() = 0;
virtual void handle(Event& event) = 0;
};
}

View File

@@ -8,12 +8,17 @@
#ifndef SRC_INPUT_IINPUTDRIVER_H_
#define SRC_INPUT_IINPUTDRIVER_H_
#include "IEventHandler.h"
#include "IEventNotifier.h"
namespace Input {
class IInputDriver
{
public:
virtual ~IInputDriver() = 0;
virtual void register_event_handler(IEventHandler* handler) = 0;
virtual void unregister_event_handler(IEventHandler* handler) = 0;
};
}

View File

@@ -6,21 +6,20 @@
*/
#ifndef SRC_INPUT_INPUTDRIVER_HPP_
#define SRC_INPUT_INPUTDRIVER_HPP_
#include "IInputDriver.h"
#include "../utilities/config.h"
#include "EventHandler.hpp"
#include "EventNotifier.h"
#include "Detector.h"
#include "IDetector.h"
namespace Input
{
class InputDriver
class InputDriver : public IInputDriver
{
public:
InputDriver(EventNotifier* event_notifier, Detector* detector) :
InputDriver(IEventNotifier* event_notifier, IDetector* detector) :
event_notifier(event_notifier), detector(detector)
{
CLOG(INFO, INPUT_LOGGER) << "Created InputDriver";
@@ -35,19 +34,19 @@ public:
detector = NULL;
}
void register_event_handler(EventHandler* handler)
void register_event_handler(IEventHandler* handler)
{
event_notifier->register_event_handler(handler);
}
void unregister_event_handler(EventHandler* handler)
void unregister_event_handler(IEventHandler* handler)
{
event_notifier->unregister_event_handler(handler);
}
private:
EventNotifier* event_notifier;
Detector* detector;
IDetector* detector;
};
}

View File

@@ -10,13 +10,13 @@
#include <fstream>
#include "Detector.h"
#include "IDetector.h"
#include "../utilities/InputGPIOInterface.h"
#include "../utilities/config.h"
#include "../lib/json/json.hpp"
#include "../lib/easylogging/easylogging++.h"
#include "EventNotifier.h"
#include "IEventNotifier.h"
using namespace nlohmann;
@@ -29,7 +29,7 @@ class InputFactory
{
public:
static shared_ptr<InputDriver*> get_InputDriver(std::string& input_config_path, std::string& matrix_config_path)
static shared_ptr<IInputDriver*> get_InputDriver(std::string& input_config_path, std::string& matrix_config_path)
{
this->ConfigureLogger();
auto event_notifier = new EventNotifier();
@@ -40,7 +40,7 @@ public:
}
private:
static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
{
std::ifstream input_config_stream(input_config_path);
json input_config;
@@ -53,7 +53,7 @@ private:
auto input_gpio_interface = new InputGPIOInterface(input_config);
auto input_notifier = new InputEventNotifier();
std::map<char, InputEvent> input_events = this->create_input_events(matrix_config);
std::map<char, Event> input_events = this->create_input_events(matrix_config);
return new Detector(input_gpio_interface, input_events, input_notifier);
}

View File

@@ -23,11 +23,11 @@ SCENARIO("An InputDriver gets created", "[construction}")
WHEN("The InputDriver gets created")
{
Input::InputDriver input_driver(&event_notifier);
Input::InputDriver input_driver(&event_notifier_mock.get());
THEN("It saved the EventNotifier")
{
REQUIRE(input_driver.event_notifier == &event_notifier);
REQUIRE(input_driver.event_notifier == &event_notifier_mock.get());
}
}
}