From 821f3946025bc82b79944b5c1ddabb3b1e46f1b9 Mon Sep 17 00:00:00 2001 From: Ondrej Mikle Date: Sat, 19 Jul 2014 15:54:57 +0200 Subject: [PATCH] C++ exceptions now propagate through python --- brmdoor_nfc.cpp | 6 +++--- brmdoor_nfc.h | 38 +++++++++++++++++++------------------- runme.py | 21 ++++++++++++--------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/brmdoor_nfc.cpp b/brmdoor_nfc.cpp index 94899d6..015c8b8 100644 --- a/brmdoor_nfc.cpp +++ b/brmdoor_nfc.cpp @@ -8,7 +8,7 @@ using namespace std; -NFCDevice::NFCDevice(): +NFCDevice::NFCDevice() throw(NFCError): pollNr(20), pollPeriod(2), _nfcContext(NULL), @@ -29,7 +29,7 @@ NFCDevice::~NFCDevice() nfc_exit(_nfcContext); } -void NFCDevice::open() +void NFCDevice::open() throw(NFCError) { if (opened()) { return; @@ -60,7 +60,7 @@ void NFCDevice::close() _opened = false; } -std::string NFCDevice::scanUID() +std::string NFCDevice::scanUID() throw(NFCError) { int res; nfc_target nt; diff --git a/brmdoor_nfc.h b/brmdoor_nfc.h index a3ec02a..36cb4ac 100644 --- a/brmdoor_nfc.h +++ b/brmdoor_nfc.h @@ -7,18 +7,34 @@ #include #include +class NFCError: public std::exception +{ + +public: + + NFCError(const std::string& msg); + + const char *what() const throw() {return _msg.c_str();} + + ~NFCError() throw() {} + +protected: + + std::string _msg; +}; + class NFCDevice { public: - NFCDevice(); + NFCDevice() throw(NFCError); virtual ~NFCDevice(); - std::string scanUID(); + std::string scanUID() throw(NFCError); - void open(); + void open() throw(NFCError); bool opened() const {return _opened;} @@ -43,19 +59,3 @@ protected: }; -class NFCError: public std::exception -{ - -public: - - NFCError(const std::string& msg); - - const char *what() const throw() {return _msg.c_str();} - - ~NFCError() throw() {} - -protected: - - std::string _msg; -}; - diff --git a/runme.py b/runme.py index 78fde3d..428f59e 100755 --- a/runme.py +++ b/runme.py @@ -1,12 +1,15 @@ #!/usr/bin/env python -from brmdoor_nfc import NFCDevice +from brmdoor_nfc import NFCDevice, NFCError from binascii import hexlify -nfc = NFCDevice() -nfc.close() -nfc.open() -print hexlify(nfc.scanUID()) -print "Device is opened:", nfc.opened() -print "Closing device" -nfc.close() -print "Device is opened:", nfc.opened() +try: + nfc = NFCDevice() + nfc.close() + nfc.open() + print hexlify(nfc.scanUID()) + print "Device is opened:", nfc.opened() + print "Closing device" + nfc.close() + print "Device is opened:", nfc.opened() +except NFCError, e: + print "Reading UID failed:", e.what()