changed to smart ptrs and trying to fix event_handler_tests
This commit is contained in:
@@ -15,8 +15,8 @@
|
||||
namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(IInputGPIOInterface* 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)
|
||||
Detector::Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, Event> events, std::shared_ptr<IEventNotifier> event_notifier) :
|
||||
input_gpio_interface(std::move(input_gpio_interface)), events(events), is_running(true), event_notifier(event_notifier)
|
||||
{
|
||||
this->detect_thread = std::thread(&Detector::detect, this);
|
||||
|
||||
@@ -28,12 +28,6 @@ Detector::~Detector()
|
||||
this->is_running = false;
|
||||
|
||||
this->detect_thread.join();
|
||||
|
||||
delete this->input_gpio_interface;
|
||||
this->input_gpio_interface = NULL;
|
||||
|
||||
delete this->event_notifier;
|
||||
this->event_notifier = NULL;
|
||||
}
|
||||
|
||||
// Cycles over all s and enqueues an event if detected.
|
||||
|
||||
@@ -31,7 +31,7 @@ class Detector : public IDetector
|
||||
{
|
||||
|
||||
public:
|
||||
Detector(IInputGPIOInterface* input_gpio_interface, std::map<char, Event> events, IEventNotifier* event_notifier);
|
||||
Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, Event> events, std::shared_ptr<IEventNotifier> event_notifier);
|
||||
~Detector();
|
||||
|
||||
private:
|
||||
@@ -39,11 +39,11 @@ private:
|
||||
bool check_inputs(char& address);
|
||||
|
||||
private:
|
||||
IInputGPIOInterface* input_gpio_interface;
|
||||
std::unique_ptr<IInputGPIOInterface> input_gpio_interface;
|
||||
|
||||
std::map<char, Event> events;
|
||||
|
||||
IEventNotifier* event_notifier;
|
||||
std::shared_ptr<IEventNotifier> event_notifier;
|
||||
|
||||
bool is_running;
|
||||
std::thread detect_thread;
|
||||
|
||||
@@ -26,12 +26,12 @@ EventNotifier::EventNotifier() :
|
||||
|
||||
EventNotifier::~EventNotifier()
|
||||
{
|
||||
is_running = false;
|
||||
this->is_running = false;
|
||||
|
||||
Event end_event(0, 0, "END");
|
||||
event_queue->push(end_event);
|
||||
this->event_queue->push(end_event);
|
||||
|
||||
notify_thread.join();
|
||||
this->notify_thread.join();
|
||||
|
||||
delete this->event_queue;
|
||||
}
|
||||
@@ -39,25 +39,25 @@ EventNotifier::~EventNotifier()
|
||||
void EventNotifier::register_event_handler(IEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
event_handlers.insert(handler);
|
||||
this->event_handlers.insert(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::unregister_event_handler(IEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
event_handlers.erase(handler);
|
||||
this->event_handlers.erase(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::distribute_event(Event& event)
|
||||
{
|
||||
event_queue->push(event);
|
||||
this->event_queue->push(event);
|
||||
}
|
||||
|
||||
void EventNotifier::notify()
|
||||
{
|
||||
while(is_running)
|
||||
while(this->is_running)
|
||||
{
|
||||
Event event = event_queue->pop();
|
||||
Event event = this->event_queue->pop();
|
||||
|
||||
// TODO schoener machen
|
||||
if(event.name == "END")
|
||||
@@ -66,8 +66,8 @@ void EventNotifier::notify()
|
||||
}
|
||||
|
||||
// getting a guard and calling all registered handlers
|
||||
std::lock_guard event_handler_guard(event_handler_mutex);
|
||||
for(auto handler : event_handlers)
|
||||
std::lock_guard event_handler_guard(this->event_handler_mutex);
|
||||
for(auto handler : this->event_handlers)
|
||||
{
|
||||
boost::thread handler_caller(boost::bind(&IEventHandler::handle, handler, event));
|
||||
|
||||
|
||||
@@ -9,21 +9,12 @@
|
||||
namespace Input
|
||||
{
|
||||
|
||||
InputDriver::InputDriver(IEventNotifier* event_notifier, IDetector* detector) :
|
||||
event_notifier(event_notifier), detector(detector)
|
||||
InputDriver::InputDriver(std::shared_ptr<IEventNotifier> event_notifier, std::unique_ptr<IDetector> detector) :
|
||||
event_notifier(event_notifier), detector(std::move(detector))
|
||||
{
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created InputDriver";
|
||||
}
|
||||
|
||||
InputDriver::~InputDriver()
|
||||
{
|
||||
delete event_notifier;
|
||||
event_notifier = NULL;
|
||||
|
||||
delete detector;
|
||||
detector = NULL;
|
||||
}
|
||||
|
||||
void InputDriver::register_event_handler(IEventHandler* handler)
|
||||
{
|
||||
event_notifier->register_event_handler(handler);
|
||||
|
||||
@@ -20,14 +20,13 @@ class InputDriver : public IInputDriver
|
||||
|
||||
public:
|
||||
|
||||
InputDriver(IEventNotifier* event_notifier, IDetector* detector);
|
||||
~InputDriver();
|
||||
InputDriver(std::shared_ptr<IEventNotifier> event_notifier, std::unique_ptr<IDetector> detector);
|
||||
virtual void register_event_handler(IEventHandler* handler) override;
|
||||
virtual void unregister_event_handler(IEventHandler* handler) override;
|
||||
|
||||
private:
|
||||
IEventNotifier* event_notifier;
|
||||
IDetector* detector;
|
||||
std::shared_ptr<IEventNotifier> event_notifier;
|
||||
std::unique_ptr<IDetector> detector;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -18,28 +18,27 @@ using namespace nlohmann;
|
||||
namespace Input
|
||||
{
|
||||
|
||||
std::shared_ptr<InputDriver> InputDriverFactory::get_InputDriver(std::string& input_config_path, std::string& matrix_config_path)
|
||||
std::shared_ptr<InputDriver> InputDriverFactory::get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream)
|
||||
{
|
||||
ConfigureLogger();
|
||||
auto event_notifier = new EventNotifier();
|
||||
|
||||
auto detector = get_detector(input_config_path, matrix_config_path);
|
||||
std::shared_ptr<IEventNotifier> event_notifier = std::make_shared<EventNotifier>();
|
||||
|
||||
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, detector));
|
||||
std::unique_ptr<IInputGPIOInterface> input_gpio_interface = std::make_unique<InputGPIOInterface>(input_config_stream);
|
||||
|
||||
std::unique_ptr<IDetector> detector(std::move(get_detector(matrix_config_stream, std::move(input_gpio_interface), event_notifier)));
|
||||
|
||||
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector)));
|
||||
}
|
||||
|
||||
Detector* InputDriverFactory::get_detector(std::string& input_config_path, std::string& matrix_config_path)
|
||||
std::unique_ptr<IDetector> InputDriverFactory::get_detector(std::istream& matrix_config_stream, std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::shared_ptr<IEventNotifier> event_notifier)
|
||||
{
|
||||
std::ifstream matrix_config_stream(matrix_config_path);
|
||||
json matrix_config;
|
||||
matrix_config_stream >> matrix_config;
|
||||
|
||||
auto input_gpio_interface = new InputGPIOInterface(input_config_path);
|
||||
auto input_notifier = new EventNotifier();
|
||||
|
||||
std::map<char, Event> input_events = create_input_events(matrix_config);
|
||||
|
||||
return new Detector(input_gpio_interface, input_events, input_notifier);
|
||||
return std::unique_ptr<Detector>(new Detector(std::move(input_gpio_interface), input_events, event_notifier));
|
||||
}
|
||||
|
||||
std::map<char, Event> InputDriverFactory::create_input_events(json matrix_config)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#ifndef INPUTFACTORY_H_
|
||||
#define INPUTFACTORY_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <istream>
|
||||
#include <atomic>
|
||||
|
||||
#include "Detector.h"
|
||||
@@ -30,9 +30,9 @@ class InputDriverFactory
|
||||
{
|
||||
|
||||
public:
|
||||
static std::shared_ptr<InputDriver> get_InputDriver(std::string& input_config_path, std::string& matrix_config_path);
|
||||
static std::shared_ptr<InputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream);
|
||||
private:
|
||||
static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path);
|
||||
static std::unique_ptr<IDetector> get_detector(std::istream& matrix_config_stream, std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::shared_ptr<IEventNotifier> event_notifier);
|
||||
static std::map<char, Event> create_input_events(nlohmann::json matrix_config);
|
||||
static void ConfigureLogger();
|
||||
};
|
||||
|
||||
@@ -42,9 +42,8 @@ void InputGPIOInterface::write_input_col(char data)
|
||||
}
|
||||
|
||||
|
||||
InputGPIOInterface::InputGPIOInterface(std::string matrix_config_path)
|
||||
InputGPIOInterface::InputGPIOInterface(std::istream& matrix_config_stream)
|
||||
{
|
||||
std::ifstream matrix_config_stream(matrix_config_path);
|
||||
json matrix_config;
|
||||
matrix_config_stream >> matrix_config;
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
|
||||
{
|
||||
public:
|
||||
InputGPIOInterface(std::string matrix_config_path);
|
||||
InputGPIOInterface(std::istream& matrix_config_stream);
|
||||
bool read_input_data(char pin);
|
||||
|
||||
private:
|
||||
|
||||
@@ -53,7 +53,7 @@ SCENARIO("Creating a Detector object", "")
|
||||
|
||||
WHEN("Detector is created")
|
||||
{
|
||||
Detector detector(&gpio_interface_mock.get(), events, &event_notifier_mock.get());
|
||||
Detector detector(std::unique_ptr<IInputGPIOInterface>(&gpio_interface_mock.get()), events, std::shared_ptr<IEventNotifier>(&event_notifier_mock.get()));
|
||||
THEN("a thread should be created")
|
||||
{
|
||||
REQUIRE(typeid(detector.detect_thread).hash_code() == typeid(std::thread).hash_code());
|
||||
|
||||
@@ -63,13 +63,13 @@ SCENARIO("An EventHandler gets [un]registered at the notifier", "[un-register no
|
||||
|
||||
AND_WHEN("The EventHandler gets unregistered at the driver")
|
||||
{
|
||||
REQUIRE(!notifier.event_handler.empty());
|
||||
REQUIRE(!notifier.event_handlers.empty());
|
||||
|
||||
notifier.unregister_event_handler(&event_handler_mock.get());
|
||||
|
||||
THEN("The list of event_handlers is empty")
|
||||
{
|
||||
REQUIRE(notifier.event_handler.empty());
|
||||
REQUIRE(notifier.event_handlers.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,7 @@ SCENARIO("An event should be distributed", "[distribute]")
|
||||
|
||||
Mock<IBlockingQueue<Event>> queue_mock;
|
||||
When(Method(queue_mock, push)).AlwaysReturn();
|
||||
When(Method(queue_mock, pop)).AlwaysDo([](){return Event(0,0,"mock");});
|
||||
Fake(Dtor(queue_mock));
|
||||
|
||||
EventNotifier notifier;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
|
||||
using namespace fakeit;
|
||||
using namespace Input;
|
||||
|
||||
SCENARIO("An InputDriver gets created", "[construction}")
|
||||
{
|
||||
@@ -26,21 +27,22 @@ SCENARIO("An InputDriver gets created", "[construction}")
|
||||
{
|
||||
LoggerFactory::CreateInputLogger();
|
||||
|
||||
Mock<Input::IDetector> detector_mock;
|
||||
Mock<IDetector> detector_mock;
|
||||
Fake(Dtor(detector_mock));
|
||||
|
||||
Mock<Input::IEventNotifier> event_notifier_mock;
|
||||
Mock<IEventNotifier> event_notifier_mock;
|
||||
Fake(Dtor(event_notifier_mock));
|
||||
Mock<IEventHandler> event_handler_mock;
|
||||
Fake(Dtor(event_handler_mock));
|
||||
WHEN("The InputDriver gets created")
|
||||
{
|
||||
Input::InputDriver input_driver(&event_notifier_mock.get(), &detector_mock.get());
|
||||
std::shared_ptr<IEventNotifier> event_notifier_ptr(&event_notifier_mock.get());
|
||||
InputDriver input_driver(event_notifier_ptr, std::unique_ptr<IDetector>(&detector_mock.get()));
|
||||
|
||||
THEN("It saves the EventNotifier and the Detector")
|
||||
{
|
||||
REQUIRE(input_driver.event_notifier == &event_notifier_mock.get());
|
||||
REQUIRE(input_driver.detector == &detector_mock.get());
|
||||
REQUIRE(input_driver.event_notifier == event_notifier_ptr);
|
||||
REQUIRE(&(*input_driver.detector) == &detector_mock.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,7 +66,8 @@ SCENARIO("An EventHandler [un]registers at the driver", "[un-register]")
|
||||
Fake(Method(event_notifier_mock, unregister_event_handler));
|
||||
Fake(Dtor(event_notifier_mock));
|
||||
|
||||
Input::InputDriver input_driver(&event_notifier_mock.get(), &detector_mock.get());
|
||||
std::shared_ptr<IEventNotifier> event_notifier_ptr(&event_notifier_mock.get());
|
||||
InputDriver input_driver(event_notifier_ptr, std::unique_ptr<IDetector>(&detector_mock.get()));
|
||||
|
||||
WHEN("The EventHandler registers at the driver")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user