diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2016-08-08 17:51:34 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2016-08-09 18:32:34 +0200 |
commit | a2da17c8608d25f64239047d0ff497adb015e411 (patch) | |
tree | c6d4b656280fe0b91903ac3398af8ac56c051ca6 | |
parent | 8bb22cbc88d930f4e88989bfbce0d19dcd0887e2 (diff) | |
download | libnitrokey-a2da17c8608d25f64239047d0ff497adb015e411.tar.gz libnitrokey-a2da17c8608d25f64239047d0ff497adb015e411.tar.bz2 |
Moving to shared ptr for device
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r-- | NitrokeyManager.cc | 18 | ||||
-rw-r--r-- | include/NitrokeyManager.h | 3 | ||||
-rw-r--r-- | include/device_proto.h | 2 |
3 files changed, 10 insertions, 13 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 93b11fa..c6eb51b 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -24,7 +24,7 @@ namespace nitrokey{ // package type to auth, auth type [Authorize,UserAuthorize] template <typename S, typename A, typename T> - void auth_package(T& package, const char* admin_temporary_password, Device * device){ + void auth_package(T& package, const char* admin_temporary_password, shared_ptr<Device> device){ auto auth = get_payload<A>(); strcpyT(auth.temporary_password, admin_temporary_password); auth.crc_to_authorize = S::CommandTransaction::getCRC(package); @@ -33,22 +33,18 @@ namespace nitrokey{ NitrokeyManager * NitrokeyManager::_instance = nullptr; - NitrokeyManager::NitrokeyManager(): device(nullptr) { + NitrokeyManager::NitrokeyManager() { set_debug(true); } NitrokeyManager::~NitrokeyManager() { - delete device; } bool NitrokeyManager::connect() { device = nullptr; - vector<Device*> devices = { new Stick10(), new Stick20() }; + vector< shared_ptr<Device> > devices = { make_shared<Stick10>(), make_shared<Stick20>() }; for( auto d : devices ){ - if (device != nullptr){ - delete d; - } - if (device == nullptr && d->connect()){ - device = d; + if (d->connect()){ + device = std::shared_ptr<Device>(d); } } return device != nullptr; @@ -58,10 +54,10 @@ namespace nitrokey{ bool NitrokeyManager::connect(const char *device_model) { switch (device_model[0]){ case 'P': - device = new Stick10(); + device = make_shared<Stick10>(); break; case 'S': - device = new Stick20(); + device = make_shared<Stick20>(); break; default: throw std::runtime_error("Unknown model"); diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index be367fd..6fa096a 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -7,6 +7,7 @@ #include "stick10_commands.h" #include "stick20_commands.h" #include <vector> +#include <memory> namespace nitrokey { using namespace nitrokey::device; @@ -86,7 +87,7 @@ namespace nitrokey { static NitrokeyManager *_instance; bool connected; - Device *device; + std::shared_ptr<Device> device; bool is_valid_hotp_slot_number(uint8_t slot_number) const; bool is_valid_totp_slot_number(uint8_t slot_number) const; diff --git a/include/device_proto.h b/include/device_proto.h index 40b2a22..4044cdc 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -144,7 +144,7 @@ public: bzero(&packet, sizeof(packet)); } - response_payload & data(){ + response_payload & data() { return packet.payload; } |