From: Konrad Rosenbaum Date: Mon, 25 Feb 2013 21:19:40 +0000 (+0100) Subject: mehr kommentare X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;p=konrad%2Fbex.git mehr kommentare --- diff --git a/LIESMICH.txt b/LIESMICH.txt index 7bc4362..fd82705 100644 --- a/LIESMICH.txt +++ b/LIESMICH.txt @@ -178,4 +178,7 @@ QPointer -------- ...ist sehr praktisch wenn man ein QObject nicht besitzt oder es sich -unvermittelt löschen kann. So wie unser Thread. \ No newline at end of file +unvermittelt löschen kann. So wie unser Thread. + +Wenn ein QObject gelöscht wird werden alle QPointer die darauf zeigen +auf NULL gesetzt. Man kann das mit isNull() testen. \ No newline at end of file diff --git a/base/thingy.cpp b/base/thingy.cpp index 3b14031..24d1ea7 100644 --- a/base/thingy.cpp +++ b/base/thingy.cpp @@ -1,5 +1,5 @@ #include "thingy.h" - +//just default implementations Thingy::Thingy(){} Thingy::~Thingy(){} diff --git a/base/thingy.h b/base/thingy.h index 74d712f..028ea5b 100644 --- a/base/thingy.h +++ b/base/thingy.h @@ -3,14 +3,27 @@ #include +///The Thingy class - it shows how to load library functionality in C++. +///It is a kind of Plugin. class Thingy { public: + ///Create a thingy. Thingy(); + ///Delete it. virtual ~Thingy(); + + ///Example for an interface that provides functionality. + /// + ///Normally these are either pure virtual or have a default implementation + ///that makes sense in the context. + /// + ///Usually a plugin interface has lots and lots of those. + ///With or without parameters... virtual QString getThingy()=0; }; +///Convenience function pointer type for the factory function. typedef Thingy* (*ThingyFunc)(); #endif diff --git a/dylib1/lib1.cpp b/dylib1/lib1.cpp index 0a171f7..457a658 100644 --- a/dylib1/lib1.cpp +++ b/dylib1/lib1.cpp @@ -1,10 +1,13 @@ #include "lib1.h" -static int mything=1; - +///just return something nice for this interface QString Thingy1::getThingy() { return "Thingy1: "+QString::number(mything++); } +///the factory function: returns a thingy implementation +/// +///it creates a derived instance that actually implementats the interface, +///but it returns a pointer to the abstract interface base class extern "C" Thingy*getthing(){return new Thingy1;} \ No newline at end of file diff --git a/dylib1/lib1.h b/dylib1/lib1.h index 4441ae7..0a9b94e 100644 --- a/dylib1/lib1.h +++ b/dylib1/lib1.h @@ -1,7 +1,12 @@ #include "../base/thingy.h" +///Implementation number 1 of a Thingy. +///This is one example of a plugin interface implementations. class Thingy1:public Thingy { public: + ///implementation of the interface virtual QString getThingy(); + private: + int mything=1; }; \ No newline at end of file diff --git a/dylib2/lib2.cpp b/dylib2/lib2.cpp index d1f7853..d84612a 100644 --- a/dylib2/lib2.cpp +++ b/dylib2/lib2.cpp @@ -1,10 +1,10 @@ #include "lib2.h" -static int mything=1; QString Thingy2::getThingy() { - return "Completely different Thingy 2: "+QString::number(mything++); + return QString("Completely different Thingy 2: %1").arg(mything+=1.1); } +///the other factory method, see lib1 for details extern "C" Thingy*getthing(){return new Thingy2;} \ No newline at end of file diff --git a/dylib2/lib2.h b/dylib2/lib2.h index e73096e..016ebb6 100644 --- a/dylib2/lib2.h +++ b/dylib2/lib2.h @@ -1,7 +1,13 @@ #include "../base/thingy.h" +///second implementation of the thingy plugin interface class Thingy2:public Thingy { public: + ///return something completely different virtual QString getThingy(); + private: + ///it is perfectly alright to add data, as long as we have + ///a virtual destructor - even if it is implicitly defined + double mything=0.; }; \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index 3417b22..81796dc 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -95,6 +95,7 @@ void MainWin::startThread() void MainWin::pokeThread() { + //if we still have that thread: poke it if(mthread.isNull()) mthingylabel->setText("Sorry, cannot poke a null thread"); else diff --git a/main/main.h b/main/main.h index 8bec118..8b67712 100644 --- a/main/main.h +++ b/main/main.h @@ -31,12 +31,13 @@ class MainWin:public QDialog ///call that thingy from one of those libraries void callThingy(); private: - //label to show what we are doing + ///label to show what we are doing QLabel*mthingylabel; - //the library that we loaded + ///the library that we loaded QLibrary*mthinglib; - //the thingy from that library + ///the thingy from that library Thingy*mthingy; + ///smart pointer to the thread example QPointermthread; }; diff --git a/main/thready.cpp b/main/thready.cpp index 58beffb..6435f4e 100644 --- a/main/thready.cpp +++ b/main/thready.cpp @@ -48,11 +48,13 @@ void MyTask::gotime() void MyThread::disruptThread() { + //relay this action emit disruptThreadSignal(); } void MyTask::disruptThread() { + //go screw with the counter ctr+=1000; gotime(); } diff --git a/main/thready.h b/main/thready.h index 136dc28..835c31e 100644 --- a/main/thready.h +++ b/main/thready.h @@ -14,8 +14,10 @@ class MyThread:public QThread ///\param slot the slot to call for updates, must be SLOT(foo(QString)) or SLOT(foo()) static MyThread* startMe(QObject*callee,const char*slot); public slots: + ///public interface of the proxy/facade to disrupt the counting of the task void disruptThread(); signals: + ///cascade signal to relay this interface to the task void disruptThreadSignal(); protected: ///internal: creates the thread and remembers who to call @@ -23,7 +25,7 @@ class MyThread:public QThread ///runs the thread void run()override; private: - //remember who to call between startMe and run + ///remember who to call between startMe and run QObject*mcallee; const char*mcallslot; };