From cfceadbca71ddaf170ea3d869adb8ea4d91ea819 Mon Sep 17 00:00:00 2001 From: Marek Vavrusa Date: Tue, 21 Oct 2008 21:53:14 +0200 Subject: [PATCH] Use boost::shared_ptr for reference counted memory handling --- ftdipp/ftdi.cpp | 46 ++++++++++++++++++++-------------------------- ftdipp/ftdi.hpp | 13 +++++-------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index 7c1ba60..5333d30 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -26,6 +26,15 @@ public: Private() : ftdi(0), dev(0), open(false) { + ftdi = ftdi_new(); + } + + ~Private() + { + if(open) + ftdi_usb_close(ftdi); + + ftdi_free(ftdi); } bool open; @@ -43,18 +52,12 @@ public: Context::Context() : d( new Private() ) { - d->ftdi = ftdi_new(); } /*! \brief Destructor. */ Context::~Context() { - if (d->open) - close(); - - ftdi_free(d->ftdi); - delete d; } bool Context::is_open() @@ -341,7 +344,6 @@ Eeprom::Eeprom(Context* parent) Eeprom::~Eeprom() { - delete d; } void Eeprom::init_defaults() @@ -387,44 +389,36 @@ int Eeprom::erase() class List::Private { public: - Private() - : list(0) + Private(struct ftdi_device_list* devlist) + : list(devlist) {} + ~Private() + { + ftdi_list_free(&list); + } + struct ftdi_device_list* list; }; List::List(struct ftdi_device_list* devlist) - : ListBase(), d( new Private() ) + : ListBase(), d( new Private(devlist) ) { if (devlist != 0) { // Iterate list - Context* c = 0; for (d->list = devlist; d->list != 0; d->list = d->list->next) { - c = new Context(); - c->set_usb_device(d->list->dev); + Context c; + c.set_usb_device(d->list->dev); + c.get_strings(); push_back(c); } - - // Store pointer - d->list = devlist; } } List::~List() { - // Deallocate instances - for (iterator it = begin(); it != end(); it++) - delete *it; - - // Clear list - clear(); - ftdi_list_free(&d->list); - - // Delete d-ptr - delete d; } List* List::find_all(int vendor, int product) diff --git a/ftdipp/ftdi.hpp b/ftdipp/ftdi.hpp index 77aee85..478d9a4 100644 --- a/ftdipp/ftdi.hpp +++ b/ftdipp/ftdi.hpp @@ -19,6 +19,7 @@ #include #include +#include #include "ftdi.h" namespace Ftdi @@ -124,11 +125,7 @@ protected: private: class Private; - Private *d; - - /* Disable copy constructor */ - Context(const Context &) {} - Context& operator=(const Context &) {} + boost::shared_ptr d; }; /*! \brief Device EEPROM. @@ -150,10 +147,10 @@ public: private: class Private; - Private *d; + boost::shared_ptr d; }; -typedef std::list ListBase; +typedef std::list ListBase; /*! \brief Device list. */ @@ -167,7 +164,7 @@ public: private: class Private; - Private *d; + boost::shared_ptr d; }; } -- 1.7.1