11#include " NoteSerial_Arduino.hpp"
22
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+
327NoteSerial *
428make_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 );
29+ nullptr_t
30+ ) {
31+ NoteSerial* & note_serial = instance::note_serial ();
32+ if (note_serial) {
33+ delete note_serial;
34+ note_serial = nullptr ;
1735 }
1836 return note_serial;
1937}
2038
21- NoteSerial_Arduino::NoteSerial_Arduino
39+ template <typename T>
40+ NoteSerial *
41+ make_note_serial (
42+ T & serial_parameters_
43+ ) {
44+ NoteSerial* & note_serial = instance::note_serial ();
45+ if (!note_serial) {
46+ using serial_type = typename ExtractNestedTemplateType<T>::type;
47+ note_serial = new NoteSerial_Arduino<serial_type>(serial_parameters_.hw_serial , serial_parameters_.baud_rate );
48+ }
49+
50+ return note_serial;
51+ }
52+
53+ template <typename T>
54+ NoteSerial_Arduino<T>::NoteSerial_Arduino
2255(
23- HardwareSerial & hw_serial_,
56+ T & hw_serial_,
2457 size_t baud_rate_
2558) :
2659 _notecardSerial (hw_serial_),
@@ -29,31 +62,35 @@ NoteSerial_Arduino::NoteSerial_Arduino
2962 _notecardSerial.begin (_notecardSerialSpeed);
3063}
3164
32- NoteSerial_Arduino::~NoteSerial_Arduino (
65+ template <typename T>
66+ NoteSerial_Arduino<T>::~NoteSerial_Arduino (
3367 void
3468)
3569{
3670 _notecardSerial.end ();
3771}
3872
73+ template <typename T>
3974size_t
40- NoteSerial_Arduino::available (
75+ NoteSerial_Arduino<T> ::available (
4176 void
4277)
4378{
4479 return _notecardSerial.available ();
4580}
4681
82+ template <typename T>
4783char
48- NoteSerial_Arduino::receive (
84+ NoteSerial_Arduino<T> ::receive (
4985 void
5086)
5187{
5288 return _notecardSerial.read ();
5389}
5490
91+ template <typename T>
5592bool
56- NoteSerial_Arduino::reset (
93+ NoteSerial_Arduino<T> ::reset (
5794 void
5895)
5996{
@@ -63,8 +100,9 @@ NoteSerial_Arduino::reset (
63100 return true ;
64101}
65102
103+ template <typename T>
66104size_t
67- NoteSerial_Arduino::transmit (
105+ NoteSerial_Arduino<T> ::transmit (
68106 uint8_t *buffer,
69107 size_t size,
70108 bool flush
@@ -77,3 +115,10 @@ NoteSerial_Arduino::transmit (
77115 }
78116 return result;
79117}
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