Skip to content

Commit 907f764

Browse files
committed
Template Singleton make_ functions
1 parent be22b4c commit 907f764

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

src/NoteTxn.hpp

Lines changed: 2 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,9 @@ class NoteTxn
4438
the platform specific transaction implementation.
4539
*/
4640
/******************************************************************************/
41+
template <typename T>
4742
NoteTxn * make_note_txn (
48-
NoteTxn::param_t txn_parameters
43+
T & txn_parameters
4944
);
5045

5146
#endif // NOTE_TXN_HPP

src/NoteTxn_Arduino.cpp

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

10+
template <>
1011
NoteTxn *
11-
make_note_txn (
12-
NoteTxn::param_t txn_parameters_
13-
)
14-
{
12+
make_note_txn <std::nullptr_t> (
13+
const std::nullptr_t & txn_pins_
14+
) {
15+
// Invalid configuration indicates deletion
16+
if (note_txn) {
17+
delete note_txn;
18+
note_txn = nullptr;
19+
}
20+
return note_txn;
21+
}
22+
23+
template <>
24+
NoteTxn *
25+
make_note_txn <uint8_t [2]> (
26+
uint8_t (&txn_pins_)[2]
27+
) {
28+
return make_note_txn(const_cast<const uint8_t (&)[2]>(txn_pins_));
29+
}
30+
31+
template <>
32+
NoteTxn *
33+
make_note_txn <const uint8_t [2]> (
34+
const uint8_t (&txn_pins_)[2]
35+
) {
36+
// Singleton
1537
static NoteTxn * note_txn = nullptr;
16-
if (!txn_parameters_) {
38+
39+
if (txn_pins_[0] == txn_pins_[1]) {
40+
// Invalid configuration indicates deletion
1741
if (note_txn) {
1842
delete note_txn;
1943
note_txn = nullptr;
2044
}
2145
} 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]);
46+
note_txn = new NoteTxn_Arduino(txn_pins_[0], txn_pins_[1]);
2447
}
48+
2549
return note_txn;
2650
}
2751

src/NoteTxn_Arduino.hpp

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

18+
template <> NoteTxn * make_note_txn <std::nullptr_t> (const std::nullptr_t & txn_pins_);
19+
template <> NoteTxn * make_note_txn <uint8_t [2]> (uint8_t (&txn_pins_)[2]);
20+
template <> NoteTxn * make_note_txn <const uint8_t [2]> (const uint8_t (&txn_pins_)[2]);
21+
1822
#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"},

0 commit comments

Comments
 (0)