MultiFunctor implementation trouble

  • Thread starter Krivenok Dmitry
  • Start date
K

Krivenok Dmitry

Hello all!

I want to implement MultiFunctor class.
Consider the following example:

// MultiFunctor object
MultiFunctor<void (int, string)> mf;
// Type of MultiFunctor's content
typedef Functor<void (int,string)> Func_t;
// Functors
Func_t f1(&some_func);
Func_t f2(some_functor);
Func_t f3(&som_obj, &SomeClass::SomeMemberFunction);
// Add functors
mf+=f1;
mf+=f2;
mf+=f3;
// Call ALL functors
mf(100,"Hello World"); // Calls all functors
// Delete one functor
mf-=f2;
// Call f1 and f3
mf(200,"Only f1 and f3");

It's fine!

But there is a big problem - how to implement operator -=?

template <class T>
MultiFunctor& MultiFunctor::eek:perator -=(const T& functor )
{
typedef SomeContainer::iterator IT;
for(IT i = container_.begin(); i!=container_.end();++i)
{
if( *i == functor) // !!! Main problem is here !!!
{
container_.erase(i);
break; // Supposing there are not duplicates in container
}
}
return *this;
}

The main question:
Is there a way to compare two functors???

Thanks!

P.S.
I know about another approach:

ID_t id1 = mf.add(f1);
ID_t id2 = mf.add(f2);
ID_t id3 = mf.add(f3);
mf(1000,"Bad solution"); // Calls f1,f2,f3
mf.delete(id2);
mf(1000,"Bad solution"); // Calls f1,f3

But it's a ugly hack :(
 
V

Victor Bazarov

Krivenok said:
I want to implement MultiFunctor class.
Consider the following example:

// MultiFunctor object
MultiFunctor<void (int, string)> mf;
// Type of MultiFunctor's content
typedef Functor<void (int,string)> Func_t;
// Functors
Func_t f1(&some_func);
Func_t f2(some_functor);
Func_t f3(&som_obj, &SomeClass::SomeMemberFunction);
// Add functors
mf+=f1;
mf+=f2;
mf+=f3;
// Call ALL functors
mf(100,"Hello World"); // Calls all functors
// Delete one functor
mf-=f2;
// Call f1 and f3
mf(200,"Only f1 and f3");

It's fine!

Does that mean you have implemented it already?
But there is a big problem - how to implement operator -=?

template <class T>
MultiFunctor& MultiFunctor::eek:perator -=(const T& functor )
{
typedef SomeContainer::iterator IT;
for(IT i = container_.begin(); i!=container_.end();++i)
{
if( *i == functor) // !!! Main problem is here !!!
{
container_.erase(i);
break; // Supposing there are not duplicates in container
}
}
return *this;
}

The main question:
Is there a way to compare two functors???

Well, if you managed to stuff them in the same class (you named it
'Func_t'), what is stopping you from comparing two objects of the
same type? Equality is very simple. Use member-wise comparison
semantics. The compiler doesn't implement one for you, I know, but
that shouldn't concern you. Just like you should be able to assign
(by using member-wise assignment semantics), there is nothing to
equality. Just add one more equal sign.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top