File tree 6 files changed +58
-29
lines changed
6 files changed +58
-29
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 @@ -67,7 +67,7 @@ class Notecard
67
67
begin (make_note_serial (&arduino_parameters));
68
68
}
69
69
inline void setDebugOutputStream (Stream &dbgserial) {
70
- setDebugOutputStream (make_note_log (& dbgserial));
70
+ setDebugOutputStream (make_note_log (dbgserial));
71
71
}
72
72
inline void setTransactionPins (uint8_t ctx_pin, uint8_t rtx_pin) {
73
73
uint8_t txn_pins[2 ] = {ctx_pin, rtx_pin};
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
Original file line number Diff line number Diff line change 1
1
#include " mock/NoteLog_Mock.hpp"
2
2
3
- MakeNoteLog_Parameters make_note_log_Parameters;
3
+ MakeNoteLog_Parameters<HardwareSerial> make_note_log_Parameters;
4
4
NoteLogPrint_Parameters noteLogPrint_Parameters;
5
5
6
6
NoteLog *
7
7
make_note_log (
8
- NoteLog::param_t log_parameters_
8
+ nullptr_t
9
+ ) {
10
+ // Record invocation(s)
11
+ ++make_note_log_Parameters.invoked ;
12
+
13
+ // Stash parameter(s)
14
+ make_note_log_Parameters.log_parameters = nullptr ;
15
+
16
+ // Return user-supplied result
17
+ return make_note_log_Parameters.result ;
18
+ }
19
+
20
+ template <typename T>
21
+ NoteLog *
22
+ make_note_log (
23
+ T & log_parameters_
9
24
)
10
25
{
11
26
// Record invocation(s)
Original file line number Diff line number Diff line change 5
5
#include < stdint.h>
6
6
7
7
#include " NoteLog.hpp"
8
+ #include " mock-arduino.hpp"
8
9
9
10
class NoteLog_Mock final : public NoteLog
10
11
{
11
12
public:
12
13
size_t print (const char * message) override ;
13
14
};
14
15
16
+ template <typename T>
15
17
struct MakeNoteLog_Parameters {
16
18
MakeNoteLog_Parameters (
17
19
void
@@ -28,7 +30,7 @@ struct MakeNoteLog_Parameters {
28
30
result = nullptr ;
29
31
}
30
32
size_t invoked;
31
- NoteLog:: param_t log_parameters;
33
+ T * log_parameters;
32
34
NoteLog * result;
33
35
};
34
36
@@ -52,7 +54,7 @@ struct NoteLogPrint_Parameters {
52
54
size_t result;
53
55
};
54
56
55
- extern MakeNoteLog_Parameters make_note_log_Parameters;
57
+ extern MakeNoteLog_Parameters<HardwareSerial> make_note_log_Parameters;
56
58
extern NoteLogPrint_Parameters noteLogPrint_Parameters;
57
59
58
60
#endif // MOCK_NOTE_LOG_HPP
You can’t perform that action at this time.
0 commit comments