std::list and interfaces question

H

heltena

Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(string value) = 0;
};

a class that implement Callback interface and this other class:

class Manager
{
public:
....
void call_function1();
void call_function2(string value);

private:
list<Callback *> m_callbacks; // it contains correct pointers
};

implementation:

void Manager::call_function1()
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_function2(string value)
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function2(value);
it++;
}
}

and the question:
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

Thanks,
 
V

Victor Bazarov

heltena said:
[..]
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

Are you asking our permission? You have it. Go right ahead.

Are you asking whether you're capable? We don't know.

V
 
R

Roland Pibinger

Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(string value) = 0;
};

a class that implement Callback interface and this other class:

class Manager
{
public:
...
void call_function1();
void call_function2(string value);

private:
list<Callback *> m_callbacks; // it contains correct pointers
}; ....
and the question:
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

Not easily. Your functions have different names and different
parameters.

Best wishes,
Roland Pibinger
 
G

Guest

Maybe template is a not too bad idea. However, first you must
understand it,.i mean you shoule know when , where, and how to use it.
 
D

dan2online

heltena said:
Hi,

I have this "interface" (abstract class): [...]
and the question:
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

Based on your requirement, I think you can consider each function as
one object.
all callback functions will derive from base class. Manager class will
easy to manage them. If you want to add a new function, you only derive
the base class to implement the function. It may be one solution shown
in the following code:

======= TEST CASE =================

#include <iostream>
#include <list>

using namespace std;

class Callback
{
public:
Callback() {}
virtual ~Callback() {}
virtual void function()=0;
//...
};

class Function1 : public Callback
{
public:
Function1(){}
virtual void function() { cout << "Function1: no argument" <<
endl; }
//....
};

class Function2: public Callback
{
private:
string value;
public:
Function2(const string &v): value(v) {}
virtual void function() { cout << "Function2: string value=" <<
value << endl; }
//....
};

class Function3: public Callback
{
private:
int value;
public:
Function3(int v): value(v) { }
virtual void function() { cout << "Function3: int value" << value
<< endl; }
//....
};

class Function4: public Callback
{
private:
int value;
public:
Function4(int v): value(v) { }
virtual void function() { cout << "Function4: int value" << value
<< endl; }
//....
};

// more functions ....


class Manager
{
private:
list<Callback *> m_callbacks; // it contains correct pointers
public:
void insert_callbacks(Callback * new_func) {
m_callbacks.push_front(new_func); }
void destroy_callbacks(void);

void call_function();
Manager() {}
~Manager() { destroy_callbacks( ); }
//.....
};


void Manager::call_function()
{
list<Callback *>::iterator it = m_callbacks.begin(),
end = m_callbacks.end();
while (it != end)
{
(*it)->function();
it++;
}

}

void Manager::destroy_callbacks()
{
list<Callback *>::iterator it = m_callbacks.begin(),
end = m_callbacks.end();
while (it != end )
{
delete (*it);
it++;
}
}



int main()
{
Manager *manager = new Manager();

Callback *f1 = new Function1();
manager->insert_callbacks(f1);

string s = "Hello world! ";
Callback *f2 = new Function2(s);
manager->insert_callbacks(f2);

Callback *f3 = new Function3(333);
manager->insert_callbacks(f3);

Callback *f4 = new Function4(444);
manager->insert_callbacks(f4);


// call function

manager->call_function();

return 0;
}


==== output ===

Function4: int value444
Function3: int value333
Function2: string value=Hello world!
Function1: no argument
 
H

heltena

Thanks for your honest nice code.

I am trying to implement a class that manages a set of listeners.
Therefore, all callbacks must implement all functions.

Fins aviat!,
 
T

Tom Widmer

heltena said:
Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(string value) = 0;
};

a class that implement Callback interface and this other class:

class Manager
{
public:
...
void call_function1();
void call_function2(string value);

private:
list<Callback *> m_callbacks; // it contains correct pointers
};

implementation:

void Manager::call_function1()
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_function2(string value)
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function2(value);
it++;
}
}

and the question:
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

You could add to Manager:

private:
list<Callback *> m_callbacks; // it contains correct pointers

template <class R>
void callCallbacks(R (Callback::*ptr)())
{
for (list<Callback *>::iterator it = m_callbacks.begin(),
end = m_callbacks.end();
it != end;
++it)
{
((*it)->ptr)();
}
}
template <class R, class A1>
void callCallbacks(R (Callback::*ptr)(A1), A1 value1)
{
for (list<Callback *>::iterator it = m_callbacks.begin(),
end = m_callbacks.end();
it != end;
++it)
{
((*it)->ptr)(value);
}
}
//add more to support more args, A1
//possibly overload for const member functions.

Now you just need to do e.g.

void call_function1(string value)
{
callCallbacks(&Callback::function1, value);
}

Alternatively, you could get boost from www.boost.org, and then use
boost::bind to do:

void call_function1(string value)
{
std::for_each(m_callbacks.begin(), m_callbacks.end(),
bind(&Callback::function1, _1, cref(value));
}


Tom
 
F

Fei Liu

heltena said:
Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(string value) = 0;
};

a class that implement Callback interface and this other class:

class Manager
{
public:
...
void call_function1();
void call_function2(string value);

private:
list<Callback *> m_callbacks; // it contains correct pointers
};

implementation:

void Manager::call_function1()
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_function2(string value)
{
list<Callback *>::iterator it = m_callbacks.begin(), end =
m_callbacks.end();
while (it != end)
{
(*it)->function2(value);
it++;
}
}

and the question:
I have a lot of functions (more that two) in the callback interface,
can I use macros or templates to make more easy the new functions
implementation?

Try boost::lambda, it's a functor generator.
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top