]> Some of my projects - localmylist.git/commitdiff
Add RaiiMyList.
authorAPTX <marek321@gmail.com>
Sun, 28 Sep 2014 13:48:37 +0000 (15:48 +0200)
committerAPTX <marek321@gmail.com>
Sun, 28 Sep 2014 13:49:53 +0000 (15:49 +0200)
RaiiMyList is a "threadsafe" way of using LocalMyList.

localmylist/include/LocalMyList/MyListNode
localmylist/include/LocalMyList/RaiiMyList [new file with mode: 0644]
localmylist/localmylist.pro
localmylist/raiimylist.cpp [new file with mode: 0644]
localmylist/raiimylist.h [new file with mode: 0644]

index 4820d890a42516f68d643b8d35920b26fef4e750..a53ff656af03ffd12a7e3c7fea878f90c03c147d 100644 (file)
@@ -1,2 +1 @@
 #include "../../mylistnode.h"
-
diff --git a/localmylist/include/LocalMyList/RaiiMyList b/localmylist/include/LocalMyList/RaiiMyList
new file mode 100644 (file)
index 0000000..0b0f8ce
--- /dev/null
@@ -0,0 +1 @@
+#include "../../raiimylist.h"
index 0d670827fd4d52aea172355b976ffe2656e0d5c9..dcb3be5ed63abfd281b5ac5945391973731fba60 100644 (file)
@@ -35,6 +35,7 @@ SOURCES += \
        messagehandler.cpp \
        asyncquerytask.cpp \
        service.cpp \
+       raiimylist.cpp \
        servicemanager.cpp \
        dynamicmodel/data.cpp \
        dynamicmodel/node.cpp \
@@ -72,6 +73,7 @@ HEADERS += \
        sqlresultiteratorinterface.h \
        service.h \
        servicemanager.h \
+       raiimylist.h \
        dynamicmodel/data.h \
        dynamicmodel/node.h \
        dynamicmodel/model.h \
@@ -100,7 +102,8 @@ CONV_HEADERS += \
        include/LocalMyList/UnknownFileLookupTask \
        include/LocalMyList/FileLocationCheckTask \
        include/LocalMyList/RequestHandler \
-       include/LocalMyList/DirectoryWatcher
+       include/LocalMyList/DirectoryWatcher \
+       include/LocalMyList/RaiiMyList
 
 !noscript {
        QT *= script
diff --git a/localmylist/raiimylist.cpp b/localmylist/raiimylist.cpp
new file mode 100644 (file)
index 0000000..36483aa
--- /dev/null
@@ -0,0 +1,25 @@
+#include "raiimylist.h"
+
+namespace LocalMyList {
+
+RaiiMyList::RaiiMyList() : mutex{}, locker{&mutex}, ok{true}, m_connected{false}
+{
+       if (MyList::instance()->thread() != QThread::currentThread())
+       {
+               qWarning("MyList instance created from a different thread");
+               ok = false;
+               return;
+       }
+       MyList::instance()->loadLocalSettings();
+       m_connected = MyList::instance()->database()->connect();
+}
+
+RaiiMyList::~RaiiMyList()
+{
+       if (!ok) return;
+
+       MyList::instance()->database()->disconnect();
+       MyList::instance()->destroy();
+}
+
+} // namespace LocalMyList
diff --git a/localmylist/raiimylist.h b/localmylist/raiimylist.h
new file mode 100644 (file)
index 0000000..1e42a61
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef RAIIMYLIST_H
+#define RAIIMYLIST_H
+
+#include "localmylist_global.h"
+#include <QMutex>
+#include <QMutexLocker>
+#include <QThread>
+#include "mylist.h"
+
+namespace LocalMyList {
+
+/**
+ * @brief A "threadsafe" way of using LocalMyList in threaded environments where there
+ * is no control over the thread from which MyList and Database are used.
+ *
+ * This is mostly a hack.
+ *
+ * Example use:
+ * {
+ *             RaiiMyList rml;
+ *             if (!rml || !rml.connected()) return ERROR;
+ *             // do something useful here, databaseconnection is open
+ * }
+ */
+class LOCALMYLISTSHARED_EXPORT RaiiMyList
+{
+  public:
+       RaiiMyList();
+       ~RaiiMyList();
+
+       operator bool() const { return ok; }
+       bool connected() const { return m_connected; }
+
+  private:
+       QMutex mutex;
+       QMutexLocker locker;
+       bool ok;
+       bool m_connected;
+};
+
+}
+
+#endif // RAIIMYLIST_H