Skip to content

Commit 3ab14c2

Browse files
committed
Template NoteTxn Singleton make_ function
1 parent 7962702 commit 3ab14c2

File tree

5 files changed

+68
-31
lines changed

5 files changed

+68
-31
lines changed

src/NoteTxn.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
class NoteTxn
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 ~NoteTxn(void) {}
1812

@@ -44,8 +38,7 @@ class NoteTxn
4438
the platform specific transaction implementation.
4539
*/
4640
/******************************************************************************/
47-
NoteTxn * make_note_txn (
48-
NoteTxn::param_t txn_parameters
49-
);
41+
template<typename T> NoteTxn * make_note_txn (T & txn_parameters);
42+
NoteTxn * make_note_txn (nullptr_t);
5043

5144
#endif // NOTE_TXN_HPP

src/NoteTxn_Arduino.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,43 @@
77
#include "mock/mock-parameters.hpp"
88
#endif
99

10+
// Singleton instance of the NoteTxn_Arduino class
11+
namespace instance {
12+
inline NoteTxn* & note_txn (void) {
13+
static NoteTxn* note_txn = nullptr;
14+
return note_txn;
15+
}
16+
};
17+
1018
NoteTxn *
1119
make_note_txn (
12-
NoteTxn::param_t txn_parameters_
13-
)
14-
{
15-
static NoteTxn * note_txn = nullptr;
16-
if (!txn_parameters_) {
20+
nullptr_t
21+
) {
22+
NoteTxn* & note_txn = instance::note_txn();
23+
if (note_txn) {
24+
delete note_txn;
25+
note_txn = nullptr;
26+
}
27+
return note_txn;
28+
}
29+
30+
template <typename T>
31+
NoteTxn *
32+
make_note_txn (
33+
T & txn_pins_
34+
) {
35+
NoteTxn* & note_txn = instance::note_txn();
36+
37+
if (txn_pins_[0] == txn_pins_[1]) {
38+
// Invalid tuple invokes deletion
1739
if (note_txn) {
1840
delete note_txn;
1941
note_txn = nullptr;
2042
}
2143
} else if (!note_txn) {
22-
const uint8_t * txn_pins = reinterpret_cast<uint8_t *>(txn_parameters_);
23-
note_txn = new NoteTxn_Arduino(txn_pins[0], txn_pins[1]);
44+
note_txn = new NoteTxn_Arduino(txn_pins_[0], txn_pins_[1]);
2445
}
46+
2547
return note_txn;
2648
}
2749

@@ -80,3 +102,7 @@ NoteTxn_Arduino::stop (
80102
// Float RTX pin
81103
::pinMode(_rtx_pin, INPUT);
82104
}
105+
106+
// Explicitly instantiate the template function for array types
107+
template NoteTxn * make_note_txn<uint8_t[2]>(uint8_t(&)[2]);
108+
template NoteTxn * make_note_txn<const uint8_t[2]>(const uint8_t(&)[2]);

test/NoteTxn_Arduino.test.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int test_make_note_txn_instantiates_notetxn_object()
1616
uint8_t txn_pins[2] = {19, 79};
1717

1818
// Action
19-
notetxn = make_note_txn(reinterpret_cast<NoteTxn::param_t>(txn_pins));
19+
notetxn = make_note_txn(txn_pins);
2020

2121
// Assert
2222
if (nullptr != notetxn)
@@ -32,7 +32,8 @@ int test_make_note_txn_instantiates_notetxn_object()
3232
}
3333

3434
// Clean-up
35-
make_note_txn(nullptr);
35+
uint8_t invalid_pins[2] = {0};
36+
make_note_txn(invalid_pins);
3637

3738
return result;
3839
}
@@ -43,11 +44,11 @@ int test_make_note_txn_enforces_singleton_by_returning_same_notetxn_object_for_a
4344

4445
// Arrange
4546
uint8_t txn_pins_1[2] = {19, 79};
46-
NoteTxn * const notetxn_1 = make_note_txn(reinterpret_cast<NoteTxn::param_t>(txn_pins_1));
47+
NoteTxn * const notetxn_1 = make_note_txn(txn_pins_1);
4748

4849
// Action
4950
uint8_t txn_pins_2[2] = {9, 17};
50-
NoteTxn * const notetxn_2 = make_note_txn(reinterpret_cast<NoteTxn::param_t>(txn_pins_2));
51+
NoteTxn * const notetxn_2 = make_note_txn(txn_pins_2);
5152

5253
// Assert
5354
if (notetxn_1 == notetxn_2)
@@ -63,23 +64,25 @@ int test_make_note_txn_enforces_singleton_by_returning_same_notetxn_object_for_a
6364
}
6465

6566
// Clean-up
66-
make_note_txn(nullptr);
67+
uint8_t invalid_pins[2] = {0};
68+
make_note_txn(invalid_pins);
6769

6870
return result;
6971
}
7072

71-
//int test_make_note_txn_returns_nullptr_when_nullptr_is_passed_as_parameter()
72-
int test_make_note_txn_deletes_singleton_when_nullptr_is_passed_as_parameter()
73+
//int test_make_note_txn_returns_nullptr_when_same_pins_are_passed_as_parameter()
74+
int test_make_note_txn_deletes_singleton_when_same_pins_are_passed_as_parameter()
7375
{
7476
int result;
7577

7678
// Arrange
7779
uint8_t txn_pins[2] = {19, 79};
78-
NoteTxn * notetxn = make_note_txn(reinterpret_cast<NoteTxn::param_t>(txn_pins));
80+
NoteTxn * notetxn = make_note_txn(txn_pins);
7981
assert(notetxn);
8082

8183
// Action
82-
notetxn = make_note_txn(nullptr);
84+
uint8_t invalid_pins[2] = {0};
85+
notetxn = make_note_txn(invalid_pins);
8386

8487
// Assert
8588
if (nullptr == notetxn)
@@ -605,7 +608,7 @@ int main(void)
605608
TestFunction tests[] = {
606609
{test_make_note_txn_instantiates_notetxn_object, "test_make_note_txn_instantiates_notetxn_object"},
607610
{test_make_note_txn_enforces_singleton_by_returning_same_notetxn_object_for_all_calls, "test_make_note_txn_enforces_singleton_by_returning_same_notetxn_object_for_all_calls"},
608-
{test_make_note_txn_deletes_singleton_when_nullptr_is_passed_as_parameter, "test_make_note_txn_deletes_singleton_when_nullptr_is_passed_as_parameter"},
611+
{test_make_note_txn_deletes_singleton_when_same_pins_are_passed_as_parameter, "test_make_note_txn_deletes_singleton_when_same_pins_are_passed_as_parameter"},
609612
{test_notetxn_arduino_constructor_floats_ctx_pin, "test_notetxn_arduino_constructor_floats_ctx_pin"},
610613
{test_notetxn_arduino_constructor_floats_rtx_pin, "test_notetxn_arduino_constructor_floats_rtx_pin"},
611614
{test_notetxn_arduino_start_initially_configures_ctx_pin_as_input_pullup, "test_notetxn_arduino_start_initially_configures_ctx_pin_as_input_pullup"},

test/mock/NoteTxn_Mock.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
#include "mock/NoteTxn_Mock.hpp"
22

3-
MakeNoteTxn_Parameters make_note_txn_Parameters;
3+
MakeNoteTxn_Parameters<const uint8_t [2]> make_note_txn_Parameters;
44
NoteTxnStart_Parameters noteTxnStart_Parameters;
55
NoteTxnStop_Parameters noteTxnStop_Parameters;
66

77
NoteTxn *
88
make_note_txn (
9-
NoteTxn::param_t txn_parameters_
9+
nullptr_t
10+
) {
11+
// Record invocation(s)
12+
++make_note_txn_Parameters.invoked;
13+
14+
// Stash parameter(s)
15+
16+
// Return user-supplied result
17+
return make_note_txn_Parameters.result;
18+
}
19+
20+
template <typename T>
21+
NoteTxn *
22+
make_note_txn (
23+
T & txn_parameters_
1024
)
1125
{
1226
// Record invocation(s)

test/mock/NoteTxn_Mock.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class NoteTxn_Mock final : public NoteTxn
1313
void stop (void) override;
1414
};
1515

16+
template <typename T>
1617
struct MakeNoteTxn_Parameters {
1718
MakeNoteTxn_Parameters(
1819
void
1920
) :
2021
invoked(0),
21-
txn_parameters(nullptr),
22+
txn_parameters{0, 0},
2223
result(nullptr)
2324
{ }
2425
void reset (
@@ -29,7 +30,7 @@ struct MakeNoteTxn_Parameters {
2930
result = nullptr;
3031
}
3132
size_t invoked;
32-
NoteTxn::param_t txn_parameters;
33+
T txn_parameters;
3334
NoteTxn * result;
3435
};
3536

@@ -67,7 +68,7 @@ struct NoteTxnStop_Parameters {
6768
size_t invoked;
6869
};
6970

70-
extern MakeNoteTxn_Parameters make_note_txn_Parameters;
71+
extern MakeNoteTxn_Parameters<const uint8_t [2]> make_note_txn_Parameters;
7172
extern NoteTxnStart_Parameters noteTxnStart_Parameters;
7273
extern NoteTxnStop_Parameters noteTxnStop_Parameters;
7374

0 commit comments

Comments
 (0)