11#include " NoteSerial_Arduino.hpp"
22
3+ #ifndef NOTE_MOCK
4+ #ifdef NOTE_ARDUINO_SOFTWARE_SERIAL_SUPPORT
5+ #include < SoftwareSerial.h>
6+ #endif
7+ #else
8+ #include " mock/mock-arduino.hpp"
9+ #endif
10+
11+ // Template Meta-Programming (TMP) to extract the nested template type
12+ template <typename nested_type>
13+ struct ExtractNestedTemplateType {
14+ // Default case: no extraction
15+ };
16+ template <typename nested_type>
17+ struct ExtractNestedTemplateType <MakeNoteSerial_ArduinoParameters<nested_type>> {
18+ using type = nested_type;
19+ };
20+
21+ // Singleton instance of the NoteSerial_Arduino class
22+ namespace instance {
23+ inline NoteSerial* & note_serial (void ) {
24+ static NoteSerial* note_serial = nullptr ;
25+ return note_serial;
26+ }
27+ };
28+
329NoteSerial *
430make_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 );
31+ nullptr_t
32+ ) {
33+ NoteSerial* & note_serial = instance::note_serial ();
34+ if (note_serial) {
35+ delete note_serial;
36+ note_serial = nullptr ;
1737 }
1838 return note_serial;
1939}
2040
21- NoteSerial_Arduino::NoteSerial_Arduino
41+ template <typename T>
42+ NoteSerial *
43+ make_note_serial (
44+ T & serial_parameters_
45+ ) {
46+ NoteSerial* & note_serial = instance::note_serial ();
47+ if (!note_serial) {
48+ using serial_type = typename ExtractNestedTemplateType<T>::type;
49+ note_serial = new NoteSerial_Arduino<serial_type>(serial_parameters_.hw_serial , serial_parameters_.baud_rate );
50+ }
51+
52+ return note_serial;
53+ }
54+
55+ template <typename T>
56+ NoteSerial_Arduino<T>::NoteSerial_Arduino
2257(
23- HardwareSerial & hw_serial_,
58+ T & hw_serial_,
2459 size_t baud_rate_
2560) :
2661 _notecardSerial (hw_serial_),
@@ -29,31 +64,35 @@ NoteSerial_Arduino::NoteSerial_Arduino
2964 _notecardSerial.begin (_notecardSerialSpeed);
3065}
3166
32- NoteSerial_Arduino::~NoteSerial_Arduino (
67+ template <typename T>
68+ NoteSerial_Arduino<T>::~NoteSerial_Arduino (
3369 void
3470)
3571{
3672 _notecardSerial.end ();
3773}
3874
75+ template <typename T>
3976size_t
40- NoteSerial_Arduino::available (
77+ NoteSerial_Arduino<T> ::available (
4178 void
4279)
4380{
4481 return _notecardSerial.available ();
4582}
4683
84+ template <typename T>
4785char
48- NoteSerial_Arduino::receive (
86+ NoteSerial_Arduino<T> ::receive (
4987 void
5088)
5189{
5290 return _notecardSerial.read ();
5391}
5492
93+ template <typename T>
5594bool
56- NoteSerial_Arduino::reset (
95+ NoteSerial_Arduino<T> ::reset (
5796 void
5897)
5998{
@@ -63,8 +102,9 @@ NoteSerial_Arduino::reset (
63102 return true ;
64103}
65104
105+ template <typename T>
66106size_t
67- NoteSerial_Arduino::transmit (
107+ NoteSerial_Arduino<T> ::transmit (
68108 uint8_t *buffer,
69109 size_t size,
70110 bool flush
@@ -77,3 +117,12 @@ NoteSerial_Arduino::transmit (
77117 }
78118 return result;
79119}
120+
121+ // Explicitly instantiate the classes and methods for the supported types
122+ template class NoteSerial_Arduino <HardwareSerial>;
123+ template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<HardwareSerial>>(MakeNoteSerial_ArduinoParameters<HardwareSerial> &);
124+
125+ #ifdef NOTE_ARDUINO_SOFTWARE_SERIAL_SUPPORT
126+ template class NoteSerial_Arduino <SoftwareSerial>;
127+ template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<SoftwareSerial>>(MakeNoteSerial_ArduinoParameters<SoftwareSerial> &);
128+ #endif
0 commit comments