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