1
1
#include " NoteSerial_Arduino.hpp"
2
2
3
+ #ifndef NOTE_MOCK
4
+ #include < SoftwareSerial.h>
5
+ #else
6
+ #include " mock/mock-arduino.hpp"
7
+ #endif
8
+
9
+ // Template Meta-Programming (TMP) to extract the nested template type
10
+ template <typename nested_type>
11
+ struct ExtractNestedTemplateType {
12
+ // Default case: no extraction
13
+ };
14
+ template <typename nested_type>
15
+ struct ExtractNestedTemplateType <MakeNoteSerial_ArduinoParameters<nested_type>> {
16
+ using type = nested_type;
17
+ };
18
+
19
+ // Singleton instance of the NoteSerial_Arduino class
20
+ namespace instance {
21
+ inline NoteSerial* & note_serial (void ) {
22
+ static NoteSerial* note_serial = nullptr ;
23
+ return note_serial;
24
+ }
25
+ };
26
+
27
+ NoteSerial *
28
+ make_note_serial (
29
+ nullptr_t
30
+ ) {
31
+ NoteSerial* & note_serial = instance::note_serial ();
32
+ if (note_serial) {
33
+ delete note_serial;
34
+ note_serial = nullptr ;
35
+ }
36
+ return note_serial;
37
+ }
38
+
39
+ template <typename T>
3
40
NoteSerial *
4
41
make_note_serial (
5
- NoteSerial:: param_t serial_parameters_
42
+ T & serial_parameters_
6
43
)
7
44
{
8
- static NoteSerial * note_serial = nullptr ;
9
- if (!serial_parameters_) {
10
- if (note_serial) {
11
- delete note_serial;
12
- note_serial = nullptr ;
13
- }
14
- } else if (!note_serial) {
15
- MakeNoteSerial_ArduinoParameters * arduino_parameters = reinterpret_cast <MakeNoteSerial_ArduinoParameters *>(serial_parameters_);
16
- note_serial = new NoteSerial_Arduino (arduino_parameters->hw_serial , arduino_parameters->baud_rate );
45
+ NoteSerial* & note_serial = instance::note_serial ();
46
+ if (!note_serial) {
47
+ using serial_type = typename ExtractNestedTemplateType<T>::type;
48
+ note_serial = new NoteSerial_Arduino<serial_type>(serial_parameters_.hw_serial , serial_parameters_.baud_rate );
17
49
}
18
50
return note_serial;
19
51
}
20
52
21
- NoteSerial_Arduino::NoteSerial_Arduino
53
+ template <typename T>
54
+ NoteSerial_Arduino<T>::NoteSerial_Arduino
22
55
(
23
- HardwareSerial & hw_serial_,
56
+ T & hw_serial_,
24
57
size_t baud_rate_
25
58
) :
26
59
_notecardSerial (hw_serial_),
@@ -29,31 +62,35 @@ NoteSerial_Arduino::NoteSerial_Arduino
29
62
_notecardSerial.begin (_notecardSerialSpeed);
30
63
}
31
64
32
- NoteSerial_Arduino::~NoteSerial_Arduino (
65
+ template <typename T>
66
+ NoteSerial_Arduino<T>::~NoteSerial_Arduino (
33
67
void
34
68
)
35
69
{
36
70
_notecardSerial.end ();
37
71
}
38
72
73
+ template <typename T>
39
74
size_t
40
- NoteSerial_Arduino::available (
75
+ NoteSerial_Arduino<T> ::available (
41
76
void
42
77
)
43
78
{
44
79
return _notecardSerial.available ();
45
80
}
46
81
82
+ template <typename T>
47
83
char
48
- NoteSerial_Arduino::receive (
84
+ NoteSerial_Arduino<T> ::receive (
49
85
void
50
86
)
51
87
{
52
88
return _notecardSerial.read ();
53
89
}
54
90
91
+ template <typename T>
55
92
bool
56
- NoteSerial_Arduino::reset (
93
+ NoteSerial_Arduino<T> ::reset (
57
94
void
58
95
)
59
96
{
@@ -63,8 +100,9 @@ NoteSerial_Arduino::reset (
63
100
return true ;
64
101
}
65
102
103
+ template <typename T>
66
104
size_t
67
- NoteSerial_Arduino::transmit (
105
+ NoteSerial_Arduino<T> ::transmit (
68
106
uint8_t *buffer,
69
107
size_t size,
70
108
bool flush
@@ -77,3 +115,10 @@ NoteSerial_Arduino::transmit (
77
115
}
78
116
return result;
79
117
}
118
+
119
+ // Explicitly instantiate the classes and methods for the supported types
120
+ template class NoteSerial_Arduino <HardwareSerial>;
121
+ template class NoteSerial_Arduino <SoftwareSerial>;
122
+
123
+ template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<HardwareSerial>>(MakeNoteSerial_ArduinoParameters<HardwareSerial> &);
124
+ template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<SoftwareSerial>>(MakeNoteSerial_ArduinoParameters<SoftwareSerial> &);
0 commit comments