c++ - How can I delete a QSharedPointer -
i have written wrapper around qtextedit use qiodevice. want able use multiple wrapper same qtextedit, may use different text color each wrapper.
to make wrapper thread-safe added qmutex protect usage of qtextedit. thinking must use 1 mutex protect 1 qtextedit.
i end following implementation, using qsharedpointer protect qtextedit.
texteditiodevice.h
class texteditiodevice : public qiodevice { q_object public: texteditiodevice(qtextedit * qtextedit, qcolor color, qobject * parent); virtual ~texteditiodevice(); protected: qint64 readdata(char *data, qint64 maxlen); qint64 writedata(const char *data, qint64 len); private: /** * @brief pointer qtextedit */ qpointer<qtextedit> textedit; /** * @brief text color */ qcolor color; /** * @brief shared pointer qtextedit associated mutex */ qsharedpointer<qmutex> mutex; /** * @brief storage qtextedit associated mutexes */ static qmap<qpointer<qtextedit>, qsharedpointer<qmutex>> mutexes; };
texteditiodevice.cpp
qmap<qpointer<qtextedit>, qsharedpointer<qmutex>> texteditiodevice::mutexes; texteditiodevice::texteditiodevice(qtextedit * qtextedit, qcolor color, qobject * parent) : qiodevice(parent), textedit(qtextedit), color(color) { open(qiodevice::writeonly | qiodevice::text); qregistermetatype<qtextcharformat>("qtextcharformat"); qregistermetatype<qtextblock>("qtextblock"); qregistermetatype<qtextcursor>("qtextcursor"); if(mutexes.contains(textedit)) mutex = mutexes[textedit]; else { mutex = qsharedpointer<qmutex>(new qmutex()); mutexes.insert(textedit, mutex); } } texteditiodevice::~texteditiodevice() { } qint64 texteditiodevice::readdata(char *data, qint64 maxlen) { q_unused(data); q_unused(maxlen); return 0; } qint64 texteditiodevice::writedata(const char *data, qint64 len) { if(textedit) { mutex->lock(); const qcolor lastcolor = textedit->textcolor(); textedit->settextcolor(color); textedit->append(qstring(data)); textedit->settextcolor(lastcolor); mutex->unlock(); } return len; }
i want know can remove qsharedpointer instance mutexes
mapping qmutex deleted.
thanks help
as long shared pointer in static mutexes
map, never deallocated, , lifetime of mutexes
lifetime of program.
if want delete mutex, have remove mutexes
mapping.
Comments
Post a Comment