File tree 3 files changed +36
-24
lines changed 3 files changed +36
-24
lines changed Original file line number Diff line number Diff line change 6
6
class NoteLog
7
7
{
8
8
public:
9
- /* *************************************************************************/
10
- /* !
11
- @brief Type used to abstract specific hardware implementation types.
12
- */
13
- /* *************************************************************************/
14
- typedef void * param_t ;
15
9
16
10
virtual ~NoteLog (void ) {}
17
11
@@ -36,8 +30,7 @@ class NoteLog
36
30
the platform specific log output implementation.
37
31
*/
38
32
/* *****************************************************************************/
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 );
42
35
43
36
#endif // NOTE_LOG_HPP
Original file line number Diff line number Diff line change 1
1
#include " NoteLog_Arduino.hpp"
2
2
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
+
3
11
NoteLog *
4
12
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_));
16
31
}
32
+
17
33
return note_log;
18
34
}
19
35
@@ -35,3 +51,6 @@ NoteLog_Arduino::print (
35
51
36
52
return result;
37
53
}
54
+
55
+ // Explicitly instantiate the template function for the supported types
56
+ template NoteLog * make_note_log<Stream>(Stream &);
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ int test_make_note_log_instantiates_notelog_object()
16
16
NoteLog * notelog = nullptr ;
17
17
18
18
// Action
19
- notelog = make_note_log (reinterpret_cast <NoteLog:: param_t >(&Serial));
19
+ notelog = make_note_log (* reinterpret_cast <Stream * >(&Serial));
20
20
21
21
// Assert
22
22
if (nullptr != notelog)
@@ -42,10 +42,10 @@ int test_make_note_log_enforces_singleton_by_returning_same_notelog_object_for_a
42
42
int result;
43
43
44
44
// 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));
46
46
47
47
// 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));
49
49
50
50
// Assert
51
51
if (notelog_1 == notelog_2)
@@ -72,7 +72,7 @@ int test_make_note_log_deletes_singleton_when_nullptr_is_passed_as_parameter()
72
72
int result;
73
73
74
74
// Arrange
75
- NoteLog * notelog = make_note_log (reinterpret_cast <NoteLog:: param_t >(&Serial));
75
+ NoteLog * notelog = make_note_log (* reinterpret_cast <Stream * >(&Serial));
76
76
assert (notelog);
77
77
78
78
// Action
You can’t perform that action at this time.
0 commit comments