473,581 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::list and interfaces question

Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(strin g 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_f unction1()
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_f unction2(string value)
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function2(valu e);
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
7 1985
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
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(strin g 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
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
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_callback s(Callback * new_func) {
m_callbacks.pus h_front(new_fun c); }
void destroy_callbac ks(void);

void call_function() ;
Manager() {}
~Manager() { destroy_callbac ks( ); }
//.....
};
void Manager::call_f unction()
{
list<Callback *>::iterator it = m_callbacks.beg in(),
end = m_callbacks.end ();
while (it != end)
{
(*it)->function();
it++;
}

}

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

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

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

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

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

Callback *f4 = new Function4(444);
manager->insert_callbac ks(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
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
heltena wrote:
Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(strin g 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_f unction1()
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_f unction2(string value)
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function2(valu e);
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.beg in(),
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.beg in(),
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::funct ion1, 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.begi n(), m_callbacks.end (),
bind(&Callback: :function1, _1, cref(value));
}
Tom
Apr 20 '06 #7

"heltena" <he*****@gmail. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Hi,

I have this "interface" (abstract class):

class Callback
{
public:
virtual void function1() = 0;
virtual void function2(strin g 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_f unction1()
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function1();
it++;
}
}

void Manager::call_f unction2(string value)
{
list<Callback *>::iterator it = m_callbacks.beg in(), end =
m_callbacks.end ();
while (it != end)
{
(*it)->function2(valu e);
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6610
by: Martin Magnusson | last post by:
I know a similar question was recently posted here, but after trying the solutions in that thread I still have problems. list<int> the_list; the_list.push_back(1); the_list.push_back(2); the_list.push_back(3); the_list.push_back(4); list<int>::iterator i = the_list.end(); i--;
8
2830
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
8
2857
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
4
2467
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic...
5
468
by: JustSomeGuy | last post by:
Can you access elements of the std::list like they were an array? eg. std::list<int> xlist; int y; .... y = xlist; // Returns the 10th element of a list.
11
5775
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary substraction operator. Peeking in the algoritm header file it's clear why seeing that sort there calls _sort(_First, _Last, _Last-_First) which might be a tad...
44
3840
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be specialized for anything. Thanks, Josh McFarlane
7
2962
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ Contents cn; list < BTree_node < Contents children; ........
17
4161
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
11
4152
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
0
7808
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.