473,378 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

std::list and interfaces question

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
7 1970
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(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
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_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
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(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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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);...
8
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...
8
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
4
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<>...
5
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
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...
44
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...
7
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{ ...
17
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...
11
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'...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.