Skip to content

Commit

Permalink
Use observer pattern in view interface, code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kwarc93 committed Oct 18, 2023
1 parent 0a6c7db commit 83c122d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 42 deletions.
6 changes: 4 additions & 2 deletions .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="libs|app|drivers|hal|system" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry excluding="middlewares|libs|app|drivers|hal|system" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="app"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="drivers"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="hal"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="libs"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="middlewares"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="system"/>
</sourceEntries>
</configuration>
Expand Down Expand Up @@ -274,11 +275,12 @@
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="libs|app|drivers|hal|system" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry excluding="middlewares|libs|app|drivers|hal|system" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="app"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="drivers"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="hal"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="libs"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="middlewares"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="system"/>
</sourceEntries>
</configuration>
Expand Down
17 changes: 7 additions & 10 deletions app/controller/default_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ void default_controller::model_new_data_callback(const spl::data_t &model_data)
}
}

void default_controller::update(const event_t &data)
{
std::visit([this](auto &&e) { return this->event_handler(e); }, data);
}

void default_controller::event_handler(const change_averaging_evt_t &e)
{
this->model->set_averaging(e.averaging);
Expand All @@ -87,18 +92,15 @@ void default_controller::event_handler(const clear_max_spl_data_evt_t &e)
this->model->reset_max_spl_data();
}


//-----------------------------------------------------------------------------
/* public */

default_controller::default_controller(meter &model, std::vector<view_interface*> &views) :
model {&model},
views {views}
default_controller::default_controller(meter &model, std::vector<view_interface*> &views) : model {&model}, views {views}
{
this->error_code = 0;

for (auto view : this->views)
view->set_event_sender_callback([this](const event_t &e) { this->handle_event(e); });
view->attach(this);

this->model->set_new_data_callback([this](const spl::data_t &data) { this->model_new_data_callback(data); });
this->model->enable();
Expand All @@ -119,8 +121,3 @@ void default_controller::process(void)
view->process();
}
}

void default_controller::handle_event(const event_t &e)
{
std::visit([this](const auto &e) { return this->event_handler(e); }, e);
}
7 changes: 5 additions & 2 deletions app/controller/default_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@
#include <app/model/data_types.hpp>
#include <app/view/view_interface.hpp>

#include <middlewares/observer.hpp>

#include "events.hpp"

namespace spl
{
/* Forward declaration */
class meter;

class default_controller
class default_controller : public middlewares::observer<event_t>
{
public:
default_controller(meter &model, std::vector<view_interface*> &views);
~default_controller();

void process(void);
void handle_event(const event_t &e);
private:
void update(const event_t &data) override;

/* Event handlers */
void event_handler(const change_averaging_evt_t &e);
void event_handler(const change_weighting_evt_t &e);
Expand Down
13 changes: 7 additions & 6 deletions app/controller/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ struct clear_max_spl_data_evt_t

};

using event_t = std::variant<change_averaging_evt_t,
change_weighting_evt_t,
clear_min_spl_data_evt_t,
clear_max_spl_data_evt_t>;

typedef std::function<void(const event_t &e)> event_cb_t;
using event_t = std::variant
<
change_averaging_evt_t,
change_weighting_evt_t,
clear_min_spl_data_evt_t,
clear_max_spl_data_evt_t
>;

}

Expand Down
8 changes: 4 additions & 4 deletions app/view/console_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void console_view::process(void)
/* Weighting */
change_weighting_evt_t e;
e.weighting = utils::switch_weighting(this->current_data.weighting);
this->send_event_cb(e);
this->notify(e);
break;
}
case 'a':
Expand All @@ -115,16 +115,16 @@ void console_view::process(void)
else if (this->current_data.averaging == 'F')
e.averaging = spl::averaging_t::slow;

this->send_event_cb(e);
this->notify(e);
break;
}
case 's':
{
/* Statistics (clear MIN/MAX) */
clear_max_spl_data_evt_t e1;
clear_min_spl_data_evt_t e2;
this->send_event_cb(e1);
this->send_event_cb(e2);
this->notify(e1);
this->notify(e2);
break;
}
case 'e':
Expand Down
16 changes: 5 additions & 11 deletions app/view/lcd_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ void lcd_view::update_view_mode(view_mode view)
this->update_lcd(nullptr);
}

void lcd_view::send_event(const event_t &e)
{
if (this->send_event_cb)
this->send_event_cb(e);
}

//-----------------------------------------------------------------------------
/* public */

Expand Down Expand Up @@ -116,7 +110,7 @@ void lcd_view::process(void)
{
change_weighting_evt_t e;
e.weighting = utils::switch_weighting(this->current_data.weighting);
this->send_event_cb(e);
this->notify(e);
return;
}

Expand All @@ -126,14 +120,14 @@ void lcd_view::process(void)
if (this->up_btn.was_pressed())
{
clear_max_spl_data_evt_t e;
this->send_event_cb(e);
this->notify(e);
this->update_view_mode(view_mode::max);
}

if (this->down_btn.was_pressed())
{
clear_min_spl_data_evt_t e;
this->send_event_cb(e);
this->notify(e);
this->update_view_mode(view_mode::min);
}

Expand All @@ -143,7 +137,7 @@ void lcd_view::process(void)
{
change_averaging_evt_t e;
e.averaging = spl::averaging_t::slow;
this->send_event_cb(e);
this->notify(e);
}
}

Expand All @@ -153,7 +147,7 @@ void lcd_view::process(void)
{
change_averaging_evt_t e;
e.averaging = spl::averaging_t::fast;
this->send_event_cb(e);
this->notify(e);
}
}
}
1 change: 0 additions & 1 deletion app/view/lcd_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace spl

void update_lcd(const data *data);
void update_view_mode(view_mode view);
void send_event(const event_t &e);

view_mode current_view_mode;

Expand Down
8 changes: 2 additions & 6 deletions app/view/view_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
#define VIEW_VIEW_INTERFACE_HPP_

#include <app/controller/events.hpp>
#include <middlewares/observer.hpp>

namespace spl
{
/* Forward declaration */
class default_controller;

class view_interface
class view_interface : public middlewares::subject<event_t>
{
public:
virtual ~view_interface() {};
Expand All @@ -33,10 +31,8 @@ namespace spl
virtual void update(const data &data) = 0;
virtual void process(void) = 0;

void set_event_sender_callback(const event_cb_t &cb) { this->send_event_cb = cb; };
protected:
data current_data;
event_cb_t send_event_cb;
};
}

Expand Down
51 changes: 51 additions & 0 deletions middlewares/observer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* observer.hpp
*
* Created on: 13 wrz 2023
* Author: kwarc
*/

#ifndef OBSERVER_HPP_
#define OBSERVER_HPP_

#include <vector>

namespace middlewares
{

template<typename T>
class observer
{
public:
virtual ~observer() {};
virtual void update(const T &data) = 0;
};

template<typename T>
class subject
{
public:
virtual ~subject() {};

void attach(observer<T> *o)
{
this->observers.push_back(o);
}

void detach(observer<T> *o)
{
this->observers.erase(std::remove(this->observers.begin(), this->observers.end(), o), this->observers.end());
}

void notify(const T &data)
{
for (const auto &o : this->observers)
o->update(data);
}
private:
std::vector<observer<T>*> observers;
};

}

#endif /* OBSERVER_HPP_ */

0 comments on commit 83c122d

Please sign in to comment.