Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Promise.h

Go to the documentation of this file.
00001 /*
00002  * Promise.h
00003  *
00004  * Smalltalk like class library for C++
00005  * Thread wrapper returning value. Header.
00006  *
00007  * Copyright (c) 2003-05 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library 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 GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #ifndef __PROMISE_H
00025 #define __PROMISE_H
00026 
00027 #include <stlib/Object.h>
00028 #include <stlib/Array.h>
00029 #include <stlib/String.h>
00030 #include <stlib/GenericException.h>
00031 
00032 #include <stlib/Callback0.h>
00033 #include <stlib/Callback1.h>
00034 
00035 namespace OS {
00036 
00037 class Semaphore;
00038 class Thread;
00039 
00044 class Promise : public Core::Object
00045 {
00046   protected:
00047     Callback<Object *> *_receiver;
00048     Array *_arguments;
00049     Thread *thread;
00050 
00051     Semaphore *sync;
00052     Object *_value;
00053     bool has_value;
00054     GenericException *_exception;
00055 
00056   public:
00057     Promise(Callback<Object *> *receiver);
00058     virtual ~Promise(void);
00059 
00060     /* Class-accessing protocol */
00061     virtual String *className(void) const;
00062 
00063     /* Instance creation protocol */
00064     static Promise *perform(Callback<Object *> *receiver, Array *args = nil);
00065 
00066     template <class TReceiver>
00067     static Promise *perform(TReceiver *receiver, Object *(TReceiver::*execFunc)(void))
00068     {
00069         Callback0<TReceiver,Object *> *callback;
00070         callback = new Callback0<TReceiver,Object *>(receiver, execFunc);
00071         return perform(callback);
00072     }
00073 
00074     template <class TReceiver>
00075     static Promise *perform(TReceiver *receiver, Object *(TReceiver::*execFunc)(Array *),
00076                             Array *args)
00077     {
00078         Callback1<TReceiver,Object*,Array*> *callback;
00079         callback = new Callback1<TReceiver,Object*,Array*>(receiver, execFunc);
00080         return perform(callback, args);
00081     }
00082 
00083     virtual void run(void);
00084 
00085     /* Accessing-child protocol */
00086     virtual void exception(GenericException *ex);
00087     virtual void value(Object *object);
00088 
00089     /* Accessing-parent protocol */
00090     virtual void arguments(Array *args);
00091     virtual bool hasValue(void);
00092     virtual Object *value(void);
00093 
00094     /* Private protocol */
00095     virtual void executePromise(void);
00096 };
00097 
00098 };
00099 
00100 #endif /* __PROMISE_H */

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2