Skip to content

Commit 834ed7f

Browse files
metsmamrts
authored andcommitted
Use lambda capture to avoid duplicating code
Signed-off-by: Raul Metsma <[email protected]>
1 parent e85751c commit 834ed7f

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

lib/libpcsc-cpp/src/SCardCall.hpp

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,62 +35,50 @@
3535
namespace pcsc_cpp
3636
{
3737

38-
inline std::string buildErrorMessage(const char* callerFunctionName, const char* scardFunctionName,
39-
const LONG result, const char* file, int line)
38+
template <auto Func, typename... Args>
39+
inline void SCardCall(const char* callerFunctionName, const char* file, int line,
40+
const char* scardFunctionName, Args&&... args)
4041
{
41-
return std::string(scardFunctionName) + " returned " + int2hexstr(result) + " in "
42-
+ removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':' + callerFunctionName;
43-
}
44-
45-
template <typename Func, typename... Args>
46-
void SCardCall(const char* callerFunctionName, const char* file, int line,
47-
const char* scardFunctionName, Func scardFunction, Args... args)
48-
{
49-
// TODO: Add logging - or is exception error message enough?
50-
51-
const LONG result = scardFunction(args...);
42+
const LONG result = Func(std::forward<Args>(args)...);
43+
auto buildErrorMessage = [&] {
44+
return std::string(scardFunctionName) + " returned " + int2hexstr(result) + " in "
45+
+ removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':'
46+
+ callerFunctionName;
47+
};
5248

53-
// TODO: Add more cases when needed.
5449
switch (result) {
5550
case SCARD_S_SUCCESS:
5651
return;
5752
case LONG(SCARD_E_NO_SERVICE):
5853
case LONG(SCARD_E_SERVICE_STOPPED):
59-
throw ScardServiceNotRunningError(
60-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
54+
throw ScardServiceNotRunningError(buildErrorMessage());
6155
case LONG(SCARD_E_NO_READERS_AVAILABLE):
6256
case LONG(SCARD_E_READER_UNAVAILABLE):
63-
throw ScardNoReadersError(
64-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
57+
throw ScardNoReadersError(buildErrorMessage());
6558
case LONG(SCARD_E_NO_SMARTCARD):
6659
#ifdef _WIN32
6760
case ERROR_NO_MEDIA_IN_DRIVE:
6861
#endif // _WIN32
69-
throw ScardNoCardError(
70-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
62+
throw ScardNoCardError(buildErrorMessage());
7163
case LONG(SCARD_E_NOT_READY):
7264
case LONG(SCARD_E_INVALID_VALUE):
7365
case LONG(SCARD_E_COMM_DATA_LOST):
7466
case LONG(SCARD_W_RESET_CARD):
7567
#ifdef _WIN32
7668
case ERROR_IO_DEVICE:
7769
#endif // _WIN32
78-
throw ScardCardCommunicationFailedError(
79-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
70+
throw ScardCardCommunicationFailedError(buildErrorMessage());
8071
case LONG(SCARD_W_REMOVED_CARD):
81-
throw ScardCardRemovedError(
82-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
72+
throw ScardCardRemovedError(buildErrorMessage());
8373
case LONG(SCARD_E_NOT_TRANSACTED):
84-
throw ScardTransactionFailedError(
85-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
74+
throw ScardTransactionFailedError(buildErrorMessage());
8675
default:
87-
throw ScardError(
88-
buildErrorMessage(callerFunctionName, scardFunctionName, result, file, line));
76+
throw ScardError(buildErrorMessage());
8977
}
9078
}
9179

9280
} // namespace pcsc_cpp
9381

9482
#define SCard(APIFunctionName, ...) \
95-
SCardCall(__FUNCTION__, __FILE__, __LINE__, "SCard" #APIFunctionName, SCard##APIFunctionName, \
96-
__VA_ARGS__)
83+
SCardCall<SCard##APIFunctionName>(__FUNCTION__, __FILE__, __LINE__, "SCard" #APIFunctionName, \
84+
__VA_ARGS__)

0 commit comments

Comments
 (0)