By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

std::list and interfaces question

P: n/a
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,

--
Helio Tejedor
english student ;-)

Apr 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
heltena wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 19 '06 #2

P: n/a
On 19 Apr 2006 11:25:29 -0700, "heltena" <he*****@gmail.com> wrote:
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
Apr 19 '06 #3

P: n/a
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.

Apr 20 '06 #4

P: n/a
heltena wrote:
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

Apr 20 '06 #5

P: n/a
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!,

--
Helio Tejedor

Apr 20 '06 #6

P: n/a
heltena wrote:
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
Apr 20 '06 #7

P: n/a

"heltena" <he*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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.

Thanks,

--
Helio Tejedor
english student ;-)

Apr 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.