DeMarcus wrote:
>
>
> Claudio Puviani wrote:
>
>> "DeMarcus" <> wrote
>>
>>> Let's say I have a class like this:
>>>
>>> class Cafe : public Interface // explained later
>>> [...]
>>>
>>> class MakeCoffee : public Interaction
>>>
>>> [...]
>>>
>>> class Thread
>>> {
>>> public:
>>>
>>> void run( Interface* interface, Interaction* interaction )
>>> {
>>> interaction->executeOn( interface );
>>> }
>>> };
>>
>>
>>
>> Why be so intrusive? Classes shouldn't have to inherit from some
>> arbitrary class
>> just to be usable in a given context. If you want to be able to call
>> just about
>> anything, create a framework of functors that allow you to wrap regular
>> functions and method invocations and have your threads invoke the
>> functor. Now,
>> the rest of the system isn't artificially coupled with your threading
>> libraries
>> and you don't introduce a massively subjective view of "what threads
>> really
>> mean".
>>
>> Claudio Puviani
>>
>> "Low coupling. It's not just for breakfast any more."
>>
>>
>
> Yes, you're right. I have to go through my code and see what I really
> though when I designed it. But our solutions are quite close ain't
> they? I mean, if we remove the inheritance of Interface from class Cafe
> and change MakeCoffee to
>
> class MakeCoffe : public Functor
> {
> public:
> MakeCoffe( void* instance );
Why you provide it as void* and not just Cafe*
> virtual void execute( void )
> {
> dynamic_cast<Cafe*>(instance)->makeCoffe( sugar, milk );
> }
> };
>
> and change Thread::run() to
>
> void run( Functor* functor )
> {
> functor->execute();
> }
>
> ain't we then speaking almost the same language here?
>
> I agree with your idea, I just have make sure I got it right to be able
> to improve my framework.
>
>
> Daniel Marcus
>
>
> PS. Also to Michael Groys; seconds after I posted my message I realized
> that my situation actually can be solved quite easy with the old Java-
> overload-thread::run()-style as well.
>
>
I also think that in the case of thread object as in the case of other
callbacks from OS to user app the best way will be to
handle them like the functors. Then programmer can easily provide his
class that will perform any task he wishes during the invocation
of the callback. (And he can store in that object any data he wants)
In addition we can provide template implementation of callback object
that will call some function of some object and provide it with
some argument, like following
template<class T, class A, class R=void>
class CallObj: public Thread
{
public:
typedef R (T::*F)(A&);
F m_f;
T* m_t;
A m_a;
CallObj(T* t, F f, const A& a)
: m_t(t), m_f(f), m_a(a)
{}
void run() {
(m_t->*m_f)(m_a);
}
};
class MyClass
{
public:
void print(const int& i) {
printf("i=%d\n", i);
}
};
int main() {
MyClass p;
CallObj<MyClass, const int> co(&p, &MyClass:

rint, 10);
co.run();
return 0;
}
Michael.
PS. this compiles under g++.