Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXMessageChannel.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *         I n t e r - T h r e a d    M e s s a g i n g    C h a n n e l         *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2006,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXMessageChannel.h,v 1.9 2009/01/06 13:07:26 fox Exp $                   *
00022 ********************************************************************************/
00023 #ifndef FXMESSAGECHANNEL_H
00024 #define FXMESSAGECHANNEL_H
00025 
00026 #ifndef FXOBJECT_H
00027 #include "FXObject.h"
00028 #endif
00029 
00030 
00031 namespace FX {
00032 
00033 class FXApp;
00034 
00035 
00036 /**
00037 * FXMessageChannel manages a messaging channel between a worker thread and the main
00038 * user-interface thread.
00039 * When an FXMessageChannel is constructed, it automatically calls addInput() function to
00040 * register itself as the message handler for the SEL_IO_READ message from FXApp.
00041 * Likewise, when FXMessageChannel is destroyed, it calls removeInput() to remove itself
00042 * as the message handler for the SEL_IO_READ message from FXApp.
00043 * When a worker thread calls the message() API, the target and message, as well
00044 * as optional message data, are written into the message channel.
00045 * The main user-interface thread is awakened and subsequently dispatches to the
00046 * onMessage handler of FXMessageChannel, which reads the target, selector, and optional
00047 * message data from the channel and then dispatches to this target using the given
00048 * selector.
00049 * Thus, FXMessageChannel provides a worker thread with a way to asynchronously invoke
00050 * any message handler in the context of the main user-interface thread.
00051 * If the size of the optional data is zero, the message handler will be passed a
00052 * NULL pointer.
00053 */
00054 class FXAPI FXMessageChannel : public FXObject {
00055   FXDECLARE(FXMessageChannel)
00056 private:
00057   FXApp *app;
00058 private:
00059   FXInputHandle h[3];
00060   FXMutex       m;
00061 protected:
00062   FXMessageChannel();
00063 private:
00064   FXMessageChannel(const FXMessageChannel&);
00065   FXMessageChannel& operator=(const FXMessageChannel&);
00066 public:
00067   enum{
00068     ID_IO_READ=1,
00069     ID_LAST
00070     };
00071 public:
00072   long onMessage(FXObject*,FXSelector,void*);
00073 public:
00074 
00075   /// Initialize message channel
00076   FXMessageChannel(FXApp* a);
00077 
00078   /// Get application pointer
00079   FXApp* getApp() const { return app; }
00080 
00081   /// Send a message msg comprising of FXSEL(type,id) to a target tgt, and pass optional data of size bytes
00082   FXbool message(FXObject* tgt,FXSelector msg,const void* data=NULL,FXint size=0);
00083 
00084   /// Clean up message channel
00085   virtual ~FXMessageChannel();
00086   };
00087 
00088 }
00089 
00090 #endif
00091 
00092 

Copyright © 1997-2009 Jeroen van der Zijp