File tree 7 files changed +59
-30
lines changed 7 files changed +59
-30
lines changed Original file line number Diff line number Diff line change 7
7
class NoteI2c
8
8
{
9
9
public:
10
- /* *************************************************************************/
11
- /* !
12
- @brief Type used to abstract specific hardware implementation types.
13
- */
14
- /* *************************************************************************/
15
- typedef void * param_t ;
16
10
17
11
virtual ~NoteI2c (void ) {}
18
12
@@ -94,8 +88,7 @@ class NoteI2c
94
88
the platform specific I2C implementation.
95
89
*/
96
90
/* *****************************************************************************/
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 );
100
93
101
94
#endif // NOTE_I2C_HPP
Original file line number Diff line number Diff line change 4
4
static const char *i2cerr = " i2c {io}" ;
5
5
#endif
6
6
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
+
7
15
NoteI2c *
8
16
make_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_
10
31
)
11
32
{
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_);
20
36
}
37
+
21
38
return note_i2c;
22
39
}
23
40
@@ -182,3 +199,6 @@ NoteI2c_Arduino::transmit (
182
199
183
200
return result;
184
201
}
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 @@ -63,7 +63,7 @@ class Notecard
63
63
inline void begin (uint32_t i2cAddress = NOTE_I2C_ADDR_DEFAULT,
64
64
uint32_t i2cMax = NOTE_I2C_MAX_DEFAULT,
65
65
TwoWire &wirePort = Wire) {
66
- begin (make_note_i2c (& wirePort), i2cAddress, i2cMax);
66
+ begin (make_note_i2c (wirePort), i2cAddress, i2cMax);
67
67
}
68
68
inline void begin (HardwareSerial &serial, uint32_t speed = 9600 ) {
69
69
MakeNoteSerial_ArduinoParameters<HardwareSerial> arduino_parameters (serial, speed);
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ int test_make_note_i2c_instantiates_notei2c_object()
16
16
NoteI2c * notei2c = nullptr ;
17
17
18
18
// Action
19
- notei2c = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
19
+ notei2c = make_note_i2c (Wire);
20
20
21
21
// Assert
22
22
if (nullptr != notei2c)
@@ -42,10 +42,10 @@ int test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_a
42
42
int result;
43
43
44
44
// Arrange
45
- NoteI2c * const notei2c_1 = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
45
+ NoteI2c * const notei2c_1 = make_note_i2c (Wire);
46
46
47
47
// Action
48
- NoteI2c * const notei2c_2 = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
48
+ NoteI2c * const notei2c_2 = make_note_i2c (Wire);
49
49
50
50
// Assert
51
51
if (notei2c_1 == notei2c_2)
@@ -72,7 +72,7 @@ int test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter()
72
72
int result;
73
73
74
74
// Arrange
75
- NoteI2c * notei2c = make_note_i2c (reinterpret_cast <NoteI2c:: param_t >(& Wire) );
75
+ NoteI2c * notei2c = make_note_i2c (Wire);
76
76
assert (notei2c);
77
77
78
78
// 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
1706
1706
Notecard notecard;
1707
1707
NoteI2c_Mock mockI2c; // Instantiate NoteI2c (mocked)
1708
1708
make_note_i2c_Parameters.reset ();
1709
- make_note_i2c_Parameters.i2c_parameters = &mockI2c ;
1709
+ make_note_i2c_Parameters.i2c_parameters = &Wire ;
1710
1710
notecard.begin (&mockI2c);
1711
1711
1712
1712
// Action
Original file line number Diff line number Diff line change 1
1
#include " mock/NoteI2c_Mock.hpp"
2
2
3
- MakeNoteI2c_Parameters make_note_i2c_Parameters;
3
+ MakeNoteI2c_Parameters<TwoWire> make_note_i2c_Parameters;
4
4
NoteI2cReceive_Parameters noteI2cReceive_Parameters;
5
5
NoteI2cReset_Parameters noteI2cReset_Parameters;
6
6
NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;
7
7
8
8
NoteI2c *
9
9
make_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
+ ) {
13
27
// Record invocation(s)
14
28
++make_note_i2c_Parameters.invoked ;
15
29
Original file line number Diff line number Diff line change 7
7
#include < string>
8
8
9
9
#include " NoteI2c.hpp"
10
+ #include " mock/mock-arduino.hpp"
10
11
11
12
class NoteI2c_Mock final : public NoteI2c
12
13
{
@@ -16,6 +17,7 @@ class NoteI2c_Mock final : public NoteI2c
16
17
const char * transmit (uint16_t device_address, uint8_t * buffer, uint16_t size) override ;
17
18
};
18
19
20
+ template <typename T>
19
21
struct MakeNoteI2c_Parameters {
20
22
MakeNoteI2c_Parameters (
21
23
void
@@ -32,7 +34,7 @@ struct MakeNoteI2c_Parameters {
32
34
result = nullptr ;
33
35
}
34
36
size_t invoked;
35
- NoteI2c:: param_t i2c_parameters;
37
+ T * i2c_parameters;
36
38
NoteI2c * result;
37
39
};
38
40
@@ -115,7 +117,7 @@ struct NoteI2cTransmit_Parameters {
115
117
const char * result;
116
118
};
117
119
118
- extern MakeNoteI2c_Parameters make_note_i2c_Parameters;
120
+ extern MakeNoteI2c_Parameters<TwoWire> make_note_i2c_Parameters;
119
121
extern NoteI2cReceive_Parameters noteI2cReceive_Parameters;
120
122
extern NoteI2cReset_Parameters noteI2cReset_Parameters;
121
123
extern NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;
You can’t perform that action at this time.
0 commit comments