MvcCore/controllers/controllerthreadhost.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  controllerthreadhost.h
00004  *  Digest
00005  * 
00006  *  Created by Aidan Lane on Thu Sep 29 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 CONTROLLERTHREADHOST_H
00026 #define CONTROLLERTHREADHOST_H
00027 
00028 
00029 #include <QThread>
00030 
00031 #include <QCoreApplication>
00032 
00033 #include "abstractcontroller.h"
00034 #include "abstractmodel.h"
00035 
00036 
00037 template <class T>
00038 class ControllerThreadHost : public QThread {
00039 
00040 public:
00041   ControllerThreadHost( QObject* parent = 0 )
00042     : QThread(parent),
00043       m_initialModel(0) {}
00044 
00045   ControllerThreadHost( AbstractModel* model, QObject* parent = 0 )
00046     : QThread(parent),
00047       m_initialModel(model) {}
00048 
00049   virtual ~ControllerThreadHost() {
00050     quit();
00051     wait(); // TODO: make wait time-out a param
00052     // WARNING: we delete the controller at the end of run()
00053   }
00054 
00055   T* controller() const { return m_controller; }
00056 
00057 
00058 protected:
00059   virtual void run()
00060   {
00061     /* Warning:
00062      * The controller is created in this new thread, hence it must NOT take the
00063      * host object (ControllerThreadHost) as its parent.
00064      */
00065     m_controller = new T;
00066     if ( m_initialModel != 0 )
00067       QCoreApplication::postEvent( m_controller,
00068                                    new CChangeModelEvent(m_initialModel,this) );
00069     exec(); // begin event-loop
00070     delete m_controller;
00071     m_controller = 0;
00072   }
00073 
00074 
00075 private:
00076   T* m_controller;
00077   AbstractModel* m_initialModel;
00078 };
00079 
00080 
00081 #endif  // ! CONTROLLERTHREADHOST_H

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