11#include " NoteSerial_Arduino.hpp"
22
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+
331NoteSerial *
432make_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 );
1752 }
53+
1854 return note_serial;
1955}
2056
21- NoteSerial_Arduino::NoteSerial_Arduino
57+ template <typename T>
58+ NoteSerial_Arduino<T>::NoteSerial_Arduino
2259(
23- HardwareSerial & hw_serial_,
60+ T & hw_serial_,
2461 size_t baud_rate_
2562) :
2663 _notecardSerial (hw_serial_),
@@ -29,31 +66,35 @@ NoteSerial_Arduino::NoteSerial_Arduino
2966 _notecardSerial.begin (_notecardSerialSpeed);
3067}
3168
32- NoteSerial_Arduino::~NoteSerial_Arduino (
69+ template <typename T>
70+ NoteSerial_Arduino<T>::~NoteSerial_Arduino (
3371 void
3472)
3573{
3674 _notecardSerial.end ();
3775}
3876
77+ template <typename T>
3978size_t
40- NoteSerial_Arduino::available (
79+ NoteSerial_Arduino<T> ::available (
4180 void
4281)
4382{
4483 return _notecardSerial.available ();
4584}
4685
86+ template <typename T>
4787char
48- NoteSerial_Arduino::receive (
88+ NoteSerial_Arduino<T> ::receive (
4989 void
5090)
5191{
5292 return _notecardSerial.read ();
5393}
5494
95+ template <typename T>
5596bool
56- NoteSerial_Arduino::reset (
97+ NoteSerial_Arduino<T> ::reset (
5798 void
5899)
59100{
@@ -63,8 +104,9 @@ NoteSerial_Arduino::reset (
63104 return true ;
64105}
65106
107+ template <typename T>
66108size_t
67- NoteSerial_Arduino::transmit (
109+ NoteSerial_Arduino<T> ::transmit (
68110 uint8_t *buffer,
69111 size_t size,
70112 bool flush
@@ -77,3 +119,12 @@ NoteSerial_Arduino::transmit (
77119 }
78120 return result;
79121}
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