digestcontrollerthreadhost.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  digestcontrollerthreadhost.h
00004  *  Digest
00005  * 
00006  *  Created by Aidan Lane on Fri Sep 30 2005.
00007  *  Copyright (c) 2005 Optimisation and Constraint Solving Group,
00008  *  Monash University. All rights reserved.
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00023  */
00024 
00025 #ifndef DIGESTCONTROLLERTHREADHOST_H
00026 #define DIGESTCONTROLLERTHREADHOST_H
00027 
00028 
00029 #include <QThread>
00030 
00031 #include <QCoreApplication>
00032 
00033 #include <jni.h>
00034 
00035 #include "abstractmodel.h"
00036 
00037 
00038 template <class T>
00039 class DigestControllerThreadHost : public QThread {
00040 
00041 public:
00042   DigestControllerThreadHost( JavaVM* jvm, QObject* parent = 0 )
00043     : QThread(parent),
00044       m_jvm(jvm),
00045       m_initialModel(0) {}
00046 
00047   DigestControllerThreadHost( AbstractModel* model,
00048                               JavaVM* jvm, QObject* parent = 0 )
00049     : QThread(parent),
00050       m_jvm(jvm),
00051       m_initialModel(model) {}
00052 
00053   virtual ~DigestControllerThreadHost() {
00054     quit();
00055     wait(); // TODO: make wait time-out a param
00056     // WARNING: we delete the controller at the end of run()
00057   }
00058 
00059   T* controller() const { return m_controller; }
00060 
00061 
00062 protected:
00063   virtual void run()
00064   {
00065     /* Warning:
00066      * The controller is created in this new thread, hence it must NOT take the
00067      * host object (ControllerThreadHost) as its parent.
00068      */
00069     m_controller = new T( m_jvm );
00070     if ( m_initialModel != 0 )
00071       QCoreApplication::postEvent( m_controller,
00072                                    new CChangeModelEvent(m_initialModel, this) );
00073     exec(); // begin event-loop
00074     delete m_controller;
00075     m_controller = 0;
00076   }
00077 
00078 
00079 private:
00080   T*              m_controller;
00081   JavaVM*         m_jvm;
00082   AbstractModel*  m_initialModel;
00083 };
00084 
00085 
00086 #endif  // ! DIGESTCONTROLLERTHREADHOST_H

Generated on Mon Jul 30 09:46:50 2007 for Digest by  doxygen 1.5.2