Skip to content

Commit 4c58bfd

Browse files
committed
Template NoteLog Singleton function
1 parent 3ab14c2 commit 4c58bfd

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/NoteLog.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
class NoteLog
77
{
88
public:
9-
/**************************************************************************/
10-
/*!
11-
@brief Type used to abstract specific hardware implementation types.
12-
*/
13-
/**************************************************************************/
14-
typedef void * param_t;
159

1610
virtual ~NoteLog(void) {}
1711

@@ -36,8 +30,7 @@ class NoteLog
3630
the platform specific log output implementation.
3731
*/
3832
/******************************************************************************/
39-
NoteLog * make_note_log (
40-
NoteLog::param_t log_parameters
41-
);
33+
template <typename T> NoteLog * make_note_log (T & log_parameters);
34+
NoteLog * make_note_log (nullptr_t);
4235

4336
#endif // NOTE_LOG_HPP

src/NoteLog_Arduino.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
#include "NoteLog_Arduino.hpp"
22

3+
// Singleton instance of the NoteLog_Arduino class
4+
namespace instance {
5+
inline NoteLog* & note_log (void) {
6+
static NoteLog* note_log = nullptr;
7+
return note_log;
8+
}
9+
};
10+
311
NoteLog *
412
make_note_log (
5-
NoteLog::param_t log_parameters_
6-
)
7-
{
8-
static NoteLog * note_log = nullptr;
9-
if (!log_parameters_) {
10-
if (note_log) {
11-
delete note_log;
12-
note_log = nullptr;
13-
}
14-
} else if (!note_log) {
15-
note_log = new NoteLog_Arduino(reinterpret_cast<Stream *>(log_parameters_));
13+
nullptr_t
14+
) {
15+
NoteLog* & note_log = instance::note_log();
16+
if (note_log) {
17+
delete note_log;
18+
note_log = nullptr;
19+
}
20+
return note_log;
21+
}
22+
23+
template <typename T>
24+
NoteLog *
25+
make_note_log (
26+
T & log_parameters_
27+
) {
28+
NoteLog* & note_log = instance::note_log();
29+
if (!note_log) {
30+
note_log = new NoteLog_Arduino(reinterpret_cast<T *>(&log_parameters_));
1631
}
32+
1733
return note_log;
1834
}
1935

@@ -35,3 +51,6 @@ NoteLog_Arduino::print (
3551

3652
return result;
3753
}
54+
55+
// Explicitly instantiate the template function for the supported types
56+
template NoteLog * make_note_log<Stream>(Stream &);

test/NoteLog_Arduino.test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int test_make_note_log_instantiates_notelog_object()
1616
NoteLog * notelog = nullptr;
1717

1818
// Action
19-
notelog = make_note_log(reinterpret_cast<NoteLog::param_t>(&Serial));
19+
notelog = make_note_log(*reinterpret_cast<Stream *>(&Serial));
2020

2121
// Assert
2222
if (nullptr != notelog)
@@ -42,10 +42,10 @@ int test_make_note_log_enforces_singleton_by_returning_same_notelog_object_for_a
4242
int result;
4343

4444
// Arrange
45-
NoteLog * const notelog_1 = make_note_log(reinterpret_cast<NoteLog::param_t>(&Serial));
45+
NoteLog * const notelog_1 = make_note_log(*reinterpret_cast<Stream *>(&Serial));
4646

4747
// Action
48-
NoteLog * const notelog_2 = make_note_log(reinterpret_cast<NoteLog::param_t>(&Serial));
48+
NoteLog * const notelog_2 = make_note_log(*reinterpret_cast<Stream *>(&Serial));
4949

5050
// Assert
5151
if (notelog_1 == notelog_2)
@@ -72,7 +72,7 @@ int test_make_note_log_deletes_singleton_when_nullptr_is_passed_as_parameter()
7272
int result;
7373

7474
// Arrange
75-
NoteLog * notelog = make_note_log(reinterpret_cast<NoteLog::param_t>(&Serial));
75+
NoteLog * notelog = make_note_log(*reinterpret_cast<Stream *>(&Serial));
7676
assert(notelog);
7777

7878
// Action

0 commit comments

Comments
 (0)