Skip to content

Commit b39d291

Browse files
committed
Template NoteI2c Singleton make_ function
1 parent 1c35712 commit b39d291

7 files changed

+59
-30
lines changed

src/NoteI2c.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
class NoteI2c
88
{
99
public:
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

src/NoteI2c_Arduino.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,37 @@
44
static 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+
715
NoteI2c *
816
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_
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 &);

src/Notecard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Notecard
6363
inline void begin(uint32_t i2cAddress = NOTE_I2C_ADDR_DEFAULT,
6464
uint32_t i2cMax = NOTE_I2C_MAX_DEFAULT,
6565
TwoWire &wirePort = Wire) {
66-
begin(make_note_i2c(&wirePort), i2cAddress, i2cMax);
66+
begin(make_note_i2c(wirePort), i2cAddress, i2cMax);
6767
}
6868
inline void begin(HardwareSerial &serial, uint32_t speed = 9600) {
6969
MakeNoteSerial_ArduinoParameters<HardwareSerial> arduino_parameters(serial, speed);

test/NoteI2c_Arduino.test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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

test/Notecard.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

test/mock/NoteI2c_Mock.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
#include "mock/NoteI2c_Mock.hpp"
22

3-
MakeNoteI2c_Parameters make_note_i2c_Parameters;
3+
MakeNoteI2c_Parameters<TwoWire> make_note_i2c_Parameters;
44
NoteI2cReceive_Parameters noteI2cReceive_Parameters;
55
NoteI2cReset_Parameters noteI2cReset_Parameters;
66
NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;
77

88
NoteI2c *
99
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+
) {
1327
// Record invocation(s)
1428
++make_note_i2c_Parameters.invoked;
1529

test/mock/NoteI2c_Mock.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string>
88

99
#include "NoteI2c.hpp"
10+
#include "mock/mock-arduino.hpp"
1011

1112
class 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>
1921
struct 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;
119121
extern NoteI2cReceive_Parameters noteI2cReceive_Parameters;
120122
extern NoteI2cReset_Parameters noteI2cReset_Parameters;
121123
extern NoteI2cTransmit_Parameters noteI2cTransmit_Parameters;

0 commit comments

Comments
 (0)