Skip to content

Commit 7fe00d4

Browse files
committed
Code cleaning
1 parent 5ebf6e2 commit 7fe00d4

15 files changed

Lines changed: 210 additions & 187 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
### 🚀 Execution
4242
- [`Event`](modules/Execution/Event.mpp) - An event for thread synchronization
43-
- [`EventSystem`](modules/Execution/EventSystem.mpp) - Event system to subscribe functions and trigger actions by event name
43+
- [`EventDispatcher`](modules/Execution/EventDispatcher.mpp) - Event system to subscribe functions and trigger actions by event name
4444

4545
### 📁 File System
4646
- [`Watcher`](modules/FileSystem/Watcher.mpp) - File modification watcher
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
export module CppUtils.Execution.EventSystem;
1+
export module CppUtils.Execution.EventDispatcher;
22

33
import std;
44
import CppUtils.Type.Callable;
55
import CppUtils.String.Hash;
66

77
export namespace CppUtils::Execution
88
{
9-
class EventSystem final
9+
class EventDispatcher final
1010
{
1111
using Key = std::pair<String::Hash, std::type_index>;
1212

1313
public:
14-
template<String::Hasher eventName = String::Hash{}>
15-
inline auto emit(const auto& event = nullptr) -> void
14+
template<String::Hasher eventName = String::Hash{}, class Event = std::nullptr_t>
15+
inline auto emit(const Event& event = nullptr) -> void
1616
{
17-
using Event = std::remove_cvref_t<decltype(event)>;
1817
auto lockGuard = std::shared_lock{m_mutex};
19-
2018
auto key = std::make_pair(static_cast<String::Hash>(eventName), std::type_index{typeid(Event)});
2119
if (auto subscriberIt = m_subscribers.find(key); subscriberIt == std::cend(m_subscribers))
2220
return;
@@ -32,7 +30,7 @@ export namespace CppUtils::Execution
3230
using FunctionInformations = Type::CallableTrait<FunctionType>;
3331
using ArgumentsTypes = FunctionInformations::ArgumentsTypes;
3432
constexpr auto nbArguments = std::tuple_size_v<ArgumentsTypes>;
35-
static_assert(nbArguments == 1, "EventSystem: subscribed callable must take exactly one argument");
33+
static_assert(nbArguments == 1, "EventDispatcher: subscribed callable must take exactly one argument");
3634
using Event = std::remove_cvref_t<std::tuple_element_t<0, ArgumentsTypes>>;
3735

3836
auto lockGuard = std::unique_lock{m_mutex};

modules/Execution/Execution.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export module CppUtils.Execution;
22

33
export import CppUtils.Execution.Event;
4-
export import CppUtils.Execution.EventSystem;
4+
export import CppUtils.Execution.EventDispatcher;

modules/Terminal/Area.mpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ export module CppUtils.Terminal.Area;
22

33
import std;
44
import CppUtils.Container.Size;
5-
export import :AreaBuffer;
65
export import CppUtils.Terminal.CharAttributes;
7-
export import :Widget;
6+
export import :AreaBuffer;
87
export import :Viewport;
8+
export import :Widget;
9+
export import :WidgetManager;
910
export import :WritableAreaView;
1011

1112
export namespace CppUtils::Terminal
1213
{
13-
class Area: public AreaBuffer, public Widget
14+
class Area: public Widget, public AreaBuffer
1415
{
1516
public:
1617
inline Area(const Container::Size2& size, const Viewport& viewport = {{0, 0}, {0, 0}}):
@@ -20,6 +21,7 @@ export namespace CppUtils::Terminal
2021

2122
inline auto addWidget(std::unique_ptr<Widget> widget) -> void
2223
{
24+
widget->setWidgetManager(m_widgetManager);
2325
m_widget = std::move(widget);
2426
}
2527

@@ -60,11 +62,13 @@ export namespace CppUtils::Terminal
6062
// Draw scrollbars
6163
// Apply to view (applique la portion délimitée par viewport du buffer sur view)
6264
// view.applyArea(m_widget->getBuffer(), m_viewport);
65+
drawFinished();
6366
}
6467

6568
protected:
6669
bool m_fullRenderRequested = true;
6770
Viewport m_viewport;
6871
std::unique_ptr<Widget> m_widget;
72+
WidgetManager m_widgetManager;
6973
};
7074
}

modules/Terminal/Canvas.mpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import CppUtils.Terminal.Area;
1313
import CppUtils.Terminal.TextStyle;
1414
import CppUtils.Terminal.TextColor;
1515
import CppUtils.Terminal.BackgroundColor;
16-
import CppUtils.Execution.Event;
17-
import CppUtils.Thread.Scheduler;
18-
import CppUtils.Execution.EventSystem;
1916

2017
export namespace CppUtils::Terminal
2118
{
@@ -107,6 +104,10 @@ export namespace CppUtils::Terminal
107104
Area{size, optionalViewport.value_or(Viewport{size, {0, 0}})},
108105
m_previousBuffer{size}
109106
{
107+
m_widgetManager.eventDispatcher.subscribe<"RequestUpdate">([this](std::nullptr_t) -> void {
108+
print();
109+
});
110+
setWidgetManager(m_widgetManager);
110111
setConsoleOutputUTF8();
111112
enableAnsi();
112113
}
@@ -175,37 +176,31 @@ export namespace CppUtils::Terminal
175176
auto view = WritableAreaView{*this, m_viewport};
176177
m_widget->draw(view);
177178
}
178-
if (m_forceReprint)
179+
if (m_firstPrint)
179180
{
180181
std::print("{}", std::string(m_viewport.getSize().height(), '\n'));
181-
m_forceReprint = false;
182+
m_firstPrint = false;
182183
}
183184
applyDifferences();
184185
}
185186

186-
inline auto reprint() noexcept -> void
187-
{
188-
m_forceReprint = true;
189-
print();
190-
}
191-
192187
inline auto wait() noexcept -> void
193188
{
194189
print();
195-
m_stopEvent.wait();
190+
m_stopEvent.wait(false);
196191
}
197192

198193
inline auto close() noexcept -> void
199194
{
200-
m_stopEvent.notify();
195+
m_stopEvent.store(true, std::memory_order_release);
196+
m_stopEvent.notify_all();
201197
}
202198

203199
private:
204-
bool m_forceReprint = true;
200+
bool m_firstPrint = true;
201+
std::atomic_bool m_stopEvent = false;
205202
AreaBuffer m_previousBuffer;
206-
Execution::Event m_stopEvent;
207-
Thread::Scheduler m_scheduler;
208-
Execution::EventSystem m_eventSystem;
203+
WidgetManager m_widgetManager;
209204
};
210205
}
211206
}

modules/Terminal/Layout.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export namespace CppUtils::Terminal
88
class Layout: public Widget
99
{
1010
public:
11-
inline virtual ~Layout() = default;
11+
virtual ~Layout() = default;
1212

1313
inline auto addWidget(std::unique_ptr<Widget> widget) -> void
1414
{

modules/Terminal/Spinner.mpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export namespace CppUtils::Terminal
3131
view.applyArea(m_frames[m_frame]);
3232
if (++m_frame == std::size(m_frames))
3333
m_frame = 0;
34+
drawFinished();
35+
requestUpdate(m_duration);
3436
}
3537

3638
[[nodiscard]] inline constexpr auto getSize() const noexcept -> const auto&

modules/Terminal/Widget.mpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
export module CppUtils.Terminal.Area:Widget;
22

3+
import std;
4+
5+
import :WidgetManager;
36
import :WritableAreaView;
7+
import CppUtils.Chrono.Concept;
48

59
export namespace CppUtils::Terminal
610
{
711
class Widget
812
{
913
public:
10-
inline virtual ~Widget() = default;
14+
virtual ~Widget() = default;
1115
virtual auto draw(WritableAreaView& view) noexcept -> void = 0;
16+
17+
inline auto drawFinished() noexcept -> void
18+
{
19+
m_needUpdate = false;
20+
}
21+
22+
inline auto setWidgetManager(WidgetManager& widgetManager) noexcept -> void
23+
{
24+
m_widgetManagerRef = widgetManager;
25+
}
26+
27+
[[nodiscard]] inline auto getWidgetManager() -> WidgetManager&
28+
{
29+
return m_widgetManagerRef.value().get();
30+
}
31+
32+
inline auto requestUpdate(Chrono::Duration auto delay = std::chrono::milliseconds{10}) -> void
33+
{
34+
getWidgetManager().scheduler.schedule([this]() -> void {
35+
m_needUpdate = true;
36+
getWidgetManager().eventDispatcher.emit<"RequestUpdate">();
37+
}, delay);
38+
}
39+
40+
private:
41+
std::atomic_bool m_needUpdate = true;
42+
std::optional<std::reference_wrapper<WidgetManager>> m_widgetManagerRef = std::nullopt;
1243
};
1344
}

modules/Terminal/WidgetManager.mpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export module CppUtils.Terminal.Area:WidgetManager;
2+
3+
import std;
4+
5+
import CppUtils.Thread.Scheduler;
6+
import CppUtils.Execution.EventDispatcher;
7+
8+
export namespace CppUtils::Terminal
9+
{
10+
struct WidgetManager final
11+
{
12+
Thread::Scheduler scheduler = {std::chrono::milliseconds{10}};
13+
Execution::EventDispatcher eventDispatcher;
14+
};
15+
}

modules/UnitTest/DummyObject.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export namespace CppUtils::UnitTest
2929
return m_moveCount;
3030
}
3131

32-
inline virtual ~DummyObject()
32+
virtual ~DummyObject()
3333
{
3434
Logger::print("{}~{}()\n", std::string(m_indentationLevel, '\t'), m_name);
3535
}

0 commit comments

Comments
 (0)