Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit e7d702b

Browse files
committed
refactor(SmartCard): add null object constructor
WE2-716 Signed-off-by: Mart Somermaa <[email protected]>
1 parent d85c1f3 commit e7d702b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

include/pcsc-cpp/pcsc-cpp-utils.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ inline std::string removeAbsolutePathPrefix(const std::string& filePath)
5757

5858
#define THROW(ExceptionType, message) \
5959
THROW_WITH_CALLER_INFO(ExceptionType, message, __FILE__, __LINE__, __func__)
60+
61+
#define REQUIRE_NON_NULL(val) \
62+
if (!val) { \
63+
throw std::logic_error("Null " + std::string(#val) + " in " \
64+
+ pcsc_cpp::removeAbsolutePathPrefix(__FILE__) + ':' \
65+
+ std::to_string(__LINE__) + ':' + __func__); \
66+
}

include/pcsc-cpp/pcsc-cpp.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ constexpr uint8_t PIN_PAD_PIN_ENTRY_TIMEOUT = 90; // 1 minute, 30 seconds
207207
class SmartCard
208208
{
209209
public:
210-
enum class Protocol { T0, T1 }; // AUTO = T0 | T1
210+
enum class Protocol { UNDEFINED, T0, T1 }; // AUTO = T0 | T1
211211

212212
using ptr = std::unique_ptr<SmartCard>;
213213

@@ -229,6 +229,7 @@ class SmartCard
229229
};
230230

231231
SmartCard(const ContextPtr& context, const string_t& readerName, byte_vector atr);
232+
SmartCard(); // Null object constructor.
232233
~SmartCard();
233234

234235
// The rule of five.

src/SmartCard.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,23 @@ SmartCard::SmartCard(const ContextPtr& contex, const string_t& readerName, byte_
273273
// TODO: debug("Card ATR -> " + bytes2hexstr(atr));
274274
}
275275

276+
SmartCard::SmartCard() = default;
276277
SmartCard::~SmartCard() = default;
277278

278279
SmartCard::TransactionGuard SmartCard::beginTransaction()
279280
{
281+
REQUIRE_NON_NULL(card);
280282
return SmartCard::TransactionGuard {*card, transactionInProgress};
281283
}
282284

283285
bool SmartCard::readerHasPinPad() const
284286
{
285-
return card->readerHasPinPad();
287+
return card ? card->readerHasPinPad() : false;
286288
}
287289

288290
ResponseApdu SmartCard::transmit(const CommandApdu& command) const
289291
{
292+
REQUIRE_NON_NULL(card);
290293
if (!transactionInProgress) {
291294
THROW(std::logic_error, "Call SmartCard::transmit() inside a transaction");
292295
}
@@ -296,6 +299,7 @@ ResponseApdu SmartCard::transmit(const CommandApdu& command) const
296299

297300
ResponseApdu SmartCard::transmitCTL(const CommandApdu& command, uint16_t lang, uint8_t minlen) const
298301
{
302+
REQUIRE_NON_NULL(card);
299303
if (!transactionInProgress) {
300304
THROW(std::logic_error, "Call SmartCard::transmit() inside a transaction");
301305
}

0 commit comments

Comments
 (0)