Skip to content

Commit f507db2

Browse files
committed
notification: Switch to CircularBuffer for notification buffer
1 parent 853e788 commit f507db2

2 files changed

Lines changed: 18 additions & 42 deletions

File tree

src/components/ble/NotificationManager.cpp

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ void NotificationManager::Push(NotificationManager::Notification&& notif) {
1111
notif.id = GetNextId();
1212
notif.valid = true;
1313
newNotification = true;
14-
if (beginIdx > 0) {
15-
--beginIdx;
16-
} else {
17-
beginIdx = notifications.size() - 1;
18-
}
19-
notifications[beginIdx] = std::move(notif);
20-
if (size < notifications.size()) {
14+
notifications--;
15+
notifications[0] = std::move(notif);
16+
if (size < notifications.Size()) {
2117
size++;
2218
}
2319
}
@@ -30,30 +26,12 @@ NotificationManager::Notification NotificationManager::GetLastNotification() con
3026
if (this->IsEmpty()) {
3127
return {};
3228
}
33-
return this->At(0);
34-
}
35-
36-
const NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) const {
37-
if (idx >= notifications.size()) {
38-
assert(false);
39-
return notifications.at(beginIdx); // this should not happen
40-
}
41-
size_t read_idx = (beginIdx + idx) % notifications.size();
42-
return notifications.at(read_idx);
43-
}
44-
45-
NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) {
46-
if (idx >= notifications.size()) {
47-
assert(false);
48-
return notifications.at(beginIdx); // this should not happen
49-
}
50-
size_t read_idx = (beginIdx + idx) % notifications.size();
51-
return notifications.at(read_idx);
29+
return notifications[0];
5230
}
5331

5432
NotificationManager::Notification::Idx NotificationManager::IndexOf(NotificationManager::Notification::Id id) const {
5533
for (NotificationManager::Notification::Idx idx = 0; idx < this->size; idx++) {
56-
const NotificationManager::Notification& notification = this->At(idx);
34+
const NotificationManager::Notification& notification = notifications[idx];
5735
if (notification.id == id) {
5836
return idx;
5937
}
@@ -66,29 +44,29 @@ NotificationManager::Notification NotificationManager::Get(NotificationManager::
6644
if (idx == this->size) {
6745
return {};
6846
}
69-
return this->At(idx);
47+
return notifications[idx];
7048
}
7149

7250
NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) const {
7351
NotificationManager::Notification::Idx idx = this->IndexOf(id);
7452
if (idx == this->size) {
7553
return {};
7654
}
77-
if (idx == 0 || idx > notifications.size()) {
55+
if (idx == 0 || idx > notifications.Size()) {
7856
return {};
7957
}
80-
return this->At(idx - 1);
58+
return notifications[idx - 1];
8159
}
8260

8361
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) const {
8462
NotificationManager::Notification::Idx idx = this->IndexOf(id);
8563
if (idx == this->size) {
8664
return {};
8765
}
88-
if (static_cast<size_t>(idx + 1) >= notifications.size()) {
66+
if (idx + 1u >= notifications.Size()) {
8967
return {};
9068
}
91-
return this->At(idx + 1);
69+
return notifications[idx + 1];
9270
}
9371

9472
void NotificationManager::DismissIdx(NotificationManager::Notification::Idx idx) {
@@ -100,14 +78,14 @@ void NotificationManager::DismissIdx(NotificationManager::Notification::Idx idx)
10078
return; // this should not happen
10179
}
10280
if (idx == 0) { // just remove the first element, don't need to change the other elements
103-
notifications.at(beginIdx).valid = false;
104-
beginIdx = (beginIdx + 1) % notifications.size();
81+
notifications[0].valid = false;
82+
notifications++;
10583
} else {
10684
// overwrite the specified entry by moving all later messages one index to the front
10785
for (size_t i = idx; i < size - 1; ++i) {
108-
this->At(i) = this->At(i + 1);
86+
notifications[i] = notifications[i + 1];
10987
}
110-
this->At(size - 1).valid = false;
88+
notifications[size - 1].valid = false;
11189
}
11290
--size;
11391
}

src/components/ble/NotificationManager.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <cstddef>
66
#include <cstdint>
77

8+
#include "utility/CircularBuffer.h"
9+
810
namespace Pinetime {
911
namespace Controllers {
1012
class NotificationManager {
@@ -61,14 +63,10 @@ namespace Pinetime {
6163
private:
6264
Notification::Id nextId {0};
6365
Notification::Id GetNextId();
64-
const Notification& At(Notification::Idx idx) const;
65-
Notification& At(Notification::Idx idx);
6666
void DismissIdx(Notification::Idx idx);
6767

68-
static constexpr uint8_t TotalNbNotifications = 5;
69-
std::array<Notification, TotalNbNotifications> notifications;
70-
size_t beginIdx = TotalNbNotifications - 1; // index of the newest notification
71-
size_t size = 0; // number of valid notifications in buffer
68+
Utility::CircularBuffer<Notification, 5> notifications;
69+
size_t size = 0; // number of valid notifications in buffer
7270

7371
std::atomic<bool> newNotification {false};
7472
};

0 commit comments

Comments
 (0)