File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77class NoteI2c
88{
99public:
10- /* *************************************************************************/
11- /* !
12- @brief Type used to abstract specific hardware implementation types.
13- */
14- /* *************************************************************************/
15- typedef void * param_t ;
1610
1711 virtual ~NoteI2c (void ) {}
1812
@@ -94,8 +88,7 @@ class NoteI2c
9488 the platform specific I2C implementation.
9589*/
9690/* *****************************************************************************/
97- NoteI2c * make_note_i2c (
98- NoteI2c::param_t i2c_parameters
99- );
91+ template <typename T> NoteI2c * make_note_i2c (T & i2c_parameters);
92+ NoteI2c * make_note_i2c (nullptr_t );
10093
10194#endif // NOTE_I2C_HPP
Original file line number Diff line number Diff line change 44static const char *i2cerr = " i2c {io}" ;
55#endif
66
7+ // Singleton instance of the NoteI2c_Arduino class
8+ namespace instance {
9+ inline NoteI2c* & note_i2c (void ) {
10+ static NoteI2c* note_i2c = nullptr ;
11+ return note_i2c;
12+ }
13+ };
14+
715NoteI2c *
816make_note_i2c (
9- NoteI2c::param_t i2c_parameters_
17+ nullptr_t
18+ ) {
19+ NoteI2c* & note_i2c = instance::note_i2c ();
20+ if (note_i2c) {
21+ delete note_i2c;
22+ note_i2c = nullptr ;
23+ }
24+ return note_i2c;
25+ }
26+
27+ template <typename T>
28+ NoteI2c *
29+ make_note_i2c (
30+ T & i2c_parameters_
1031)
1132{
12- static NoteI2c * note_i2c = nullptr ;
13- if (!i2c_parameters_) {
14- if (note_i2c) {
15- delete note_i2c;
16- note_i2c = nullptr ;
17- }
18- } else if (!note_i2c) {
19- note_i2c = new NoteI2c_Arduino (*reinterpret_cast <TwoWire *>(i2c_parameters_));
33+ NoteI2c* & note_i2c = instance::note_i2c ();
34+ if (!note_i2c) {
35+ note_i2c = new NoteI2c_Arduino (i2c_parameters_);
2036 }
37+
2138 return note_i2c;
2239}
2340
@@ -182,3 +199,6 @@ NoteI2c_Arduino::transmit (
182199
183200 return result;
184201}
202+
203+ // Explicitly instantiate the template function for the supported types
204+ template NoteI2c * make_note_i2c<TwoWire>(TwoWire &);
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ int test_make_note_i2c_instantiates_notei2c_object()
1616 NoteI2c * notei2c = nullptr ;
1717
1818 // Action
19- notei2c = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
19+ notei2c = make_note_i2c (Wire);
2020
2121 // Assert
2222 if (nullptr != notei2c)
@@ -42,10 +42,10 @@ int test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_a
4242 int result;
4343
4444 // Arrange
45- NoteI2c * const notei2c_1 = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
45+ NoteI2c * const notei2c_1 = make_note_i2c (Wire);
4646
4747 // Action
48- NoteI2c * const notei2c_2 = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
48+ NoteI2c * const notei2c_2 = make_note_i2c (Wire);
4949
5050 // Assert
5151 if (notei2c_1 == notei2c_2)
@@ -72,7 +72,7 @@ int test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter()
7272 int result;
7373
7474 // Arrange
75- NoteI2c * notei2c = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
75+ NoteI2c * notei2c = make_note_i2c (Wire);
7676 assert (notei2c);
7777
7878 // Action
Original file line number Diff line number Diff line change @@ -1706,7 +1706,7 @@ int test_notecard_end_provides_nullptr_to_make_note_i2c_to_free_associated_memor
17061706 Notecard notecard;
17071707 NoteI2c_Mock mockI2c; // Instantiate NoteI2c (mocked)
17081708 make_note_i2c_Parameters.reset ();
1709- make_note_i2c_Parameters.i2c_parameters = &mockI2c ;
1709+ make_note_i2c_Parameters.i2c_parameters = &Wire ;
17101710 notecard.begin (&mockI2c);
17111711
17121712 // Action
Original file line number Diff line number Diff line change 11#include " mock/NoteI2c_Mock.hpp"
22
3- MakeNoteI2c_Parameters make_note_i2c_Parameters;
3+ MakeNoteI2c_Parameters<TwoWire> make_note_i2c_Parameters;
44NoteI2cReceive_Parameters noteI2cReceive_Parameters;
55NoteI2cReset_Parameters noteI2cReset_Parameters;
66NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;
77
88NoteI2c *
99make_note_i2c (
10- NoteI2c::param_t i2c_parameters_
11- )
12- {
10+ nullptr_t
11+ ) {
12+ // Record invocation(s)
13+ ++make_note_i2c_Parameters.invoked ;
14+
15+ // Stash parameter(s)
16+ make_note_i2c_Parameters.i2c_parameters = nullptr ;
17+
18+ // Return user-supplied result
19+ return make_note_i2c_Parameters.result ;
20+ }
21+
22+ template <typename T>
23+ NoteI2c *
24+ make_note_i2c (
25+ T & i2c_parameters_
26+ ) {
1327 // Record invocation(s)
1428 ++make_note_i2c_Parameters.invoked ;
1529
Original file line number Diff line number Diff line change 77#include < string>
88
99#include " NoteI2c.hpp"
10+ #include " mock/mock-arduino.hpp"
1011
1112class NoteI2c_Mock final : public NoteI2c
1213{
@@ -16,6 +17,7 @@ class NoteI2c_Mock final : public NoteI2c
1617 const char * transmit (uint16_t device_address, uint8_t * buffer, uint16_t size) override ;
1718};
1819
20+ template <typename T>
1921struct MakeNoteI2c_Parameters {
2022 MakeNoteI2c_Parameters (
2123 void
@@ -32,7 +34,7 @@ struct MakeNoteI2c_Parameters {
3234 result = nullptr ;
3335 }
3436 size_t invoked;
35- NoteI2c:: param_t i2c_parameters;
37+ T * i2c_parameters;
3638 NoteI2c * result;
3739};
3840
@@ -115,7 +117,7 @@ struct NoteI2cTransmit_Parameters {
115117 const char * result;
116118};
117119
118- extern MakeNoteI2c_Parameters make_note_i2c_Parameters;
120+ extern MakeNoteI2c_Parameters<TwoWire> make_note_i2c_Parameters;
119121extern NoteI2cReceive_Parameters noteI2cReceive_Parameters;
120122extern NoteI2cReset_Parameters noteI2cReset_Parameters;
121123extern NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;
You can’t perform that action at this time.
0 commit comments