diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index b9701c6..8698211 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -15,8 +15,8 @@ namespace Input { -Detector::Detector(IInputGPIOInterface* input_gpio_interface, std::map events, IEventNotifier* event_notifier) : - input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier) +Detector::Detector(std::unique_ptr input_gpio_interface, std::map events, std::shared_ptr 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. diff --git a/FlippR-Driver/src/input/Detector.h b/FlippR-Driver/src/input/Detector.h index cf3200f..57b1d91 100644 --- a/FlippR-Driver/src/input/Detector.h +++ b/FlippR-Driver/src/input/Detector.h @@ -31,7 +31,7 @@ class Detector : public IDetector { public: - Detector(IInputGPIOInterface* input_gpio_interface, std::map events, IEventNotifier* event_notifier); + Detector(std::unique_ptr input_gpio_interface, std::map events, std::shared_ptr event_notifier); ~Detector(); private: @@ -39,11 +39,11 @@ private: bool check_inputs(char& address); private: - IInputGPIOInterface* input_gpio_interface; + std::unique_ptr input_gpio_interface; std::map events; - IEventNotifier* event_notifier; + std::shared_ptr event_notifier; bool is_running; std::thread detect_thread; diff --git a/FlippR-Driver/src/input/EventNotifier.cpp b/FlippR-Driver/src/input/EventNotifier.cpp index 5bad02a..e2cd069 100644 --- a/FlippR-Driver/src/input/EventNotifier.cpp +++ b/FlippR-Driver/src/input/EventNotifier.cpp @@ -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 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 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)); diff --git a/FlippR-Driver/src/input/InputDriver.cpp b/FlippR-Driver/src/input/InputDriver.cpp index fd1e4b1..9b67ac8 100644 --- a/FlippR-Driver/src/input/InputDriver.cpp +++ b/FlippR-Driver/src/input/InputDriver.cpp @@ -9,21 +9,12 @@ namespace Input { -InputDriver::InputDriver(IEventNotifier* event_notifier, IDetector* detector) : - event_notifier(event_notifier), detector(detector) +InputDriver::InputDriver(std::shared_ptr event_notifier, std::unique_ptr 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); diff --git a/FlippR-Driver/src/input/InputDriver.h b/FlippR-Driver/src/input/InputDriver.h index 6660f1c..0ec45ef 100644 --- a/FlippR-Driver/src/input/InputDriver.h +++ b/FlippR-Driver/src/input/InputDriver.h @@ -20,14 +20,13 @@ class InputDriver : public IInputDriver public: - InputDriver(IEventNotifier* event_notifier, IDetector* detector); - ~InputDriver(); + InputDriver(std::shared_ptr event_notifier, std::unique_ptr 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 event_notifier; + std::unique_ptr detector; }; } diff --git a/FlippR-Driver/src/input/InputDriverFactory.cpp b/FlippR-Driver/src/input/InputDriverFactory.cpp index ea66893..b1a0451 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.cpp +++ b/FlippR-Driver/src/input/InputDriverFactory.cpp @@ -18,28 +18,27 @@ using namespace nlohmann; namespace Input { -std::shared_ptr InputDriverFactory::get_InputDriver(std::string& input_config_path, std::string& matrix_config_path) +std::shared_ptr 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 event_notifier = std::make_shared(); - return std::shared_ptr(new InputDriver(event_notifier, detector)); + std::unique_ptr input_gpio_interface = std::make_unique(input_config_stream); + + std::unique_ptr detector(std::move(get_detector(matrix_config_stream, std::move(input_gpio_interface), event_notifier))); + + return std::shared_ptr(new InputDriver(event_notifier, std::move(detector))); } -Detector* InputDriverFactory::get_detector(std::string& input_config_path, std::string& matrix_config_path) +std::unique_ptr InputDriverFactory::get_detector(std::istream& matrix_config_stream, std::unique_ptr input_gpio_interface, std::shared_ptr 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 input_events = create_input_events(matrix_config); - return new Detector(input_gpio_interface, input_events, input_notifier); + return std::unique_ptr(new Detector(std::move(input_gpio_interface), input_events, event_notifier)); } std::map InputDriverFactory::create_input_events(json matrix_config) diff --git a/FlippR-Driver/src/input/InputDriverFactory.h b/FlippR-Driver/src/input/InputDriverFactory.h index 6859ef5..8899a0d 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.h +++ b/FlippR-Driver/src/input/InputDriverFactory.h @@ -8,7 +8,7 @@ #ifndef INPUTFACTORY_H_ #define INPUTFACTORY_H_ -#include +#include #include #include "Detector.h" @@ -30,9 +30,9 @@ class InputDriverFactory { public: - static std::shared_ptr get_InputDriver(std::string& input_config_path, std::string& matrix_config_path); + static std::shared_ptr 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 get_detector(std::istream& matrix_config_stream, std::unique_ptr input_gpio_interface, std::shared_ptr event_notifier); static std::map create_input_events(nlohmann::json matrix_config); static void ConfigureLogger(); }; diff --git a/FlippR-Driver/src/utilities/InputGPIOInterface.cpp b/FlippR-Driver/src/utilities/InputGPIOInterface.cpp index 31d4022..eb8609c 100644 --- a/FlippR-Driver/src/utilities/InputGPIOInterface.cpp +++ b/FlippR-Driver/src/utilities/InputGPIOInterface.cpp @@ -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; diff --git a/FlippR-Driver/src/utilities/InputGPIOInterface.h b/FlippR-Driver/src/utilities/InputGPIOInterface.h index f5bd7b1..b21540d 100644 --- a/FlippR-Driver/src/utilities/InputGPIOInterface.h +++ b/FlippR-Driver/src/utilities/InputGPIOInterface.h @@ -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: diff --git a/FlippR-Driver/tests/input/TestDetector.cpp b/FlippR-Driver/tests/input/TestDetector.cpp index beab98e..ba35262 100644 --- a/FlippR-Driver/tests/input/TestDetector.cpp +++ b/FlippR-Driver/tests/input/TestDetector.cpp @@ -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(&gpio_interface_mock.get()), events, std::shared_ptr(&event_notifier_mock.get())); THEN("a thread should be created") { REQUIRE(typeid(detector.detect_thread).hash_code() == typeid(std::thread).hash_code()); diff --git a/FlippR-Driver/tests/input/TestEventNotifier.cpp b/FlippR-Driver/tests/input/TestEventNotifier.cpp index b8e6cf4..821f0a8 100644 --- a/FlippR-Driver/tests/input/TestEventNotifier.cpp +++ b/FlippR-Driver/tests/input/TestEventNotifier.cpp @@ -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> 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; diff --git a/FlippR-Driver/tests/input/TestInputDriver.cpp b/FlippR-Driver/tests/input/TestInputDriver.cpp index 3bab6fd..eb671d8 100644 --- a/FlippR-Driver/tests/input/TestInputDriver.cpp +++ b/FlippR-Driver/tests/input/TestInputDriver.cpp @@ -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 detector_mock; + Mock detector_mock; Fake(Dtor(detector_mock)); - Mock event_notifier_mock; + Mock event_notifier_mock; Fake(Dtor(event_notifier_mock)); Mock 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 event_notifier_ptr(&event_notifier_mock.get()); + InputDriver input_driver(event_notifier_ptr, std::unique_ptr(&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 event_notifier_ptr(&event_notifier_mock.get()); + InputDriver input_driver(event_notifier_ptr, std::unique_ptr(&detector_mock.get())); WHEN("The EventHandler registers at the driver") {