Skip to content

Commit 32b496f

Browse files
committed
Template Singleton make_ functions
1 parent be22b4c commit 32b496f

7 files changed

+69
-29
lines changed

src/NoteTxn.hpp

Lines changed: 6 additions & 7 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,13 @@ class NoteTxn
4438
the platform specific transaction implementation.
4539
*/
4640
/******************************************************************************/
41+
template <typename T>
42+
NoteTxn * make_note_txn (
43+
T & txn_parameters
44+
);
45+
4746
NoteTxn * make_note_txn (
48-
NoteTxn::param_t txn_parameters
47+
nullptr_t
4948
);
5049

5150
#endif // NOTE_TXN_HPP

src/NoteTxn_Arduino.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,38 @@
99

1010
NoteTxn *
1111
make_note_txn (
12-
NoteTxn::param_t txn_parameters_
13-
)
14-
{
12+
nullptr_t
13+
) {
14+
const uint8_t invoke_deletion[2] = {0, 0}; // Invalid tuple invokes deletion
15+
return make_note_txn(invoke_deletion);
16+
}
17+
18+
template <>
19+
NoteTxn *
20+
make_note_txn <uint8_t [2]> (
21+
uint8_t (&txn_pins_)[2]
22+
) {
23+
return make_note_txn(const_cast<const uint8_t (&)[2]>(txn_pins_));
24+
}
25+
26+
template <>
27+
NoteTxn *
28+
make_note_txn <const uint8_t [2]> (
29+
const uint8_t (&txn_pins_)[2]
30+
) {
31+
// Singleton
1532
static NoteTxn * note_txn = nullptr;
16-
if (!txn_parameters_) {
33+
34+
if (txn_pins_[0] == txn_pins_[1]) {
35+
// Invalid tuple invokes deletion
1736
if (note_txn) {
1837
delete note_txn;
1938
note_txn = nullptr;
2039
}
2140
} 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]);
41+
note_txn = new NoteTxn_Arduino(txn_pins_[0], txn_pins_[1]);
2442
}
43+
2544
return note_txn;
2645
}
2746

src/NoteTxn_Arduino.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ class NoteTxn_Arduino final : public NoteTxn
1515
uint8_t _rtx_pin;
1616
};
1717

18+
template <> NoteTxn * make_note_txn <uint8_t [2]> (uint8_t (&txn_pins_)[2]);
19+
template <> NoteTxn * make_note_txn <const uint8_t [2]> (const uint8_t (&txn_pins_)[2]);
20+
1821
#endif // NOTE_TXN_ARDUINO_HPP

src/Notecard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class Notecard
9696
setDebugOutputStream(nullptr);
9797
}
9898
inline void clearTransactionPins(void) {
99-
setTransactionPins(nullptr);
99+
uint8_t txn_pins[2] = {0};
100+
setTransactionPins(make_note_txn(txn_pins));
100101
}
101102
bool debugSyncStatus (int pollFrequencyMs, int maxLevel);
102103
void deleteResponse(J *rsp) const;

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)