473,396 Members | 1,799 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,396 software developers and data experts.

Function Objects with Iterators

Hi there,

Please give me at least a hint...
I have a problem implementing a function object with parameters two
iterators. That is:

A class 'node' produces messages using a routing policy. The routing
policy needs to take the node's neighbours and return a subset of them
based on several criteria. Each message may have different routing
policy. Thus, the policy should be specified while the new message is
produced. The policy should be a different class that can be inherited
and be able to take the (begin,end) iterators of the neighbour list
and return a std::stack of a subset of them.

The node should not reveal any of its internal structures, not even
the neighbours structure. This is why I created a templated function
object but I do not know how to declare the two iterators to be
general without having to specify their exact type.

template<class _TYPEclass router {
public:
// function to take the start and end iterators of a container and
return a subset of them in a stack.
// it decides using further inheritance which routing policy will be
used for each message
std::stack<_TYPEoperator()(iterator _begin, iterator _end);
};

template<class _MSG, class _CLKclass node { //this is a node
class with MESSAGE and CLOCK parameters
private:
// map holding node's neighbours and their expiry time
std::map<node<_MSG, _CLK>*, _CLK_fanin, _fanout;
public:
// function to produce a message ready to deliver with the actual
content, expiry and routing policy
node<_MSG, _CLK>& produce(_MSG _msg, _CLK _clock, router _rout);
};

The user of the node::produce funtion does not need to specify the
exact iterator type since than would violate the principle of hiding
the neighbour's structure.

I need for the iterators a form so that I declare & define & use the
produce function without having to specify std::map<node<_MSG, _CLK>*,
_CLK>::iterator.

Please, help me or post a an idea that has the same effect.

Thanks in advance,
-- George

Feb 11 '07 #1
6 1579
gexarchakos wrote:
Hi there,

Please give me at least a hint...
I have a problem implementing a function object with parameters two
iterators. That is:

A class 'node' produces messages using a routing policy. The routing
policy needs to take the node's neighbours and return a subset of them
based on several criteria. Each message may have different routing
policy. Thus, the policy should be specified while the new message is
produced. The policy should be a different class that can be inherited
and be able to take the (begin,end) iterators of the neighbour list
and return a std::stack of a subset of them.

The node should not reveal any of its internal structures, not even
the neighbours structure. This is why I created a templated function
object but I do not know how to declare the two iterators to be
general without having to specify their exact type.

template<class _TYPEclass router {
public:
// function to take the start and end iterators of a container and
return a subset of them in a stack.
// it decides using further inheritance which routing policy will be
used for each message
std::stack<_TYPEoperator()(iterator _begin, iterator _end);
};

template<class _MSG, class _CLKclass node { //this is a node
class with MESSAGE and CLOCK parameters
private:
// map holding node's neighbours and their expiry time
std::map<node<_MSG, _CLK>*, _CLK_fanin, _fanout;
public:
// function to produce a message ready to deliver with the actual
content, expiry and routing policy
node<_MSG, _CLK>& produce(_MSG _msg, _CLK _clock, router _rout);
};

The user of the node::produce funtion does not need to specify the
exact iterator type since than would violate the principle of hiding
the neighbour's structure.

I need for the iterators a form so that I declare & define & use the
produce function without having to specify std::map<node<_MSG, _CLK>*,
_CLK>::iterator.

Please, help me or post a an idea that has the same effect.

Thanks in advance,
-- George
I think I understand you.

Write your template so that the iterator is the template parameter

template<class _ITER>
class router
{
public:
std::stack<typename _ITER::value_type operator()(_ITER begin,
_ITER end);
};

All iterators have a member called value_type which is the type that the
iterator refers to.

John
Feb 11 '07 #2
Many thanks John,

this would be a possible solution but the problem that appears now is
that when I call
node<_MSG,_CLK>::produce(_msg, _clock, router<std::map<node<_MSG,
_CLK>*,_CLK>::iterator>());
I need to know that the neighbours are in a map structure so that to
initialize the 'router' with this specific iterator.
e.g. if i have a function

void test() {
...
node<int,int_n;
_n.produce(8,2,router<std::map<node<_MSG,
_CLK>*,_CLK>::iterator>());
}

This is something I need to avoid. The test function is outside the
class 'node' but still needs to know the neighbours structure.

However, your solution had something that really helped:
std::stack<typename _ITER::value_type... (good point)

What if:
class router {
public:
template<class _ITERstd::stack<typename _ITER::value_type>
operator()(_ITER _begin, _ITER _end);
};

This, I think, would solve the problem above but poses a new one:
template functions cannot be virtual so this posses some kind of
inheritance problem...

Any idea is welcome... and again many thanks John!

-- George

Feb 11 '07 #3
gexarchakos wrote:
What if:
class router {
public:
template<class _ITERstd::stack<typename _ITER::value_type>
operator()(_ITER _begin, _ITER _end);
};
A problem you are bound to run into using "_ITER::value_type" is that
pointers can be iterators, but obviously don't have a member called
"value_type".

Fortunately the standard anticipated this and provides an
"iterator_traits" template to solve the problem. You use it like:
std::iterator_traits<_ITER>::value_type

For most iterators this just becomes "_ITER::value_type", but for
pointer types it becomes the type the pointer points to.

--
Alan Johnson
Feb 11 '07 #4
John Harrison wrote:
gexarchakos wrote:
>[redacted]
template<class _TYPEclass router {
public:
// function to take the start and end iterators of a container
and
return a subset of them in a stack.
// it decides using further inheritance which routing policy
will be
used for each message
std::stack<_TYPEoperator()(iterator _begin, iterator _end);
};

template<class _MSG, class _CLKclass node { //this is a node
class with MESSAGE and CLOCK parameters
private:
// map holding node's neighbours and their expiry time
std::map<node<_MSG, _CLK>*, _CLK_fanin, _fanout;
public:
// function to produce a message ready to deliver with the actual
content, expiry and routing policy
node<_MSG, _CLK>& produce(_MSG _msg, _CLK _clock, router _rout);
};

[redacted]
template<class _ITER>
class router
{
public:
std::stack<typename _ITER::value_type>
operator()(_ITER begin, _ITER end);
};
Both gexarchakos and John's code is improper. Any identifier with a
leading underscore followed by an uppercase letter is reserved to the
implementation.

Use TYPE_, MSG_, CLK_, ITER_ instead.
Feb 12 '07 #5
On Feb 12, 12:47 am, red floyd <no.s...@here.dudewrote:
John Harrison wrote:
gexarchakos wrote:
[redacted]
template<class _TYPEclass router {
public:
// function to take the start and end iterators of a container
and
return a subset of them in a stack.
// it decides using further inheritance which routing policy
will be
used for each message
std::stack<_TYPEoperator()(iterator _begin, iterator _end);
};
template<class _MSG, class _CLKclass node { //this is a node
class with MESSAGE and CLOCK parameters
private:
// map holding node's neighbours and their expiry time
std::map<node<_MSG, _CLK>*, _CLK_fanin, _fanout;
public:
// function to produce a message ready to deliver with the actual
content, expiry and routing policy
node<_MSG, _CLK>& produce(_MSG _msg, _CLK _clock, router _rout);
};
[redacted]
template<class _ITER>
class router
{
public:
std::stack<typename _ITER::value_type>
operator()(_ITER begin, _ITER end);
};

Both gexarchakos and John's code is improper. Any identifier with a
leading underscore followed by an uppercase letter is reserved to the
implementation.

Use TYPE_, MSG_, CLK_, ITER_ instead.
Thank you red floyd for your comment... One more thing I didn't know!
However, any compilation/building phase of my code didn't catch this.
Anyway, good to know for the future.

Thanks,
-- George

Feb 12 '07 #6
On Feb 11, 11:35 pm, Alan Johnson <a...@yahoo.comwrote:
gexarchakos wrote:
What if:
class router {
public:
template<class _ITERstd::stack<typename _ITER::value_type>
operator()(_ITER _begin, _ITER _end);
};

A problem you are bound to run into using "_ITER::value_type" is that
pointers can be iterators, but obviously don't have a member called
"value_type".

Fortunately the standard anticipated this and provides an
"iterator_traits" template to solve the problem. You use it like:
std::iterator_traits<_ITER>::value_type

For most iterators this just becomes "_ITER::value_type", but for
pointer types it becomes the type the pointer points to.

--
Alan Johnson
Hi Alan,

thank you for your reply. I have the Stroupstrup book in front of me
and I try to figure out how exactly I am going to use the
iterator_traits. My only problem is that it is again a templated-based
solution which is perfectly fine except for the case I want to use
inheritance (virtual) with the templated operator() of 'router'.

Correct me if I am wrong; I am supposed to write:
class router {
public:
template<class _ITER>
std::stack<std::iterator_traits<_ITER>::value_type operator()(_ITER
_begin, _ITER _end);
};

If yes, then what if I what inheritance as well? Routing policies must
around 4-5 and I was planing to create a router abstract base class
with some subclasses to implement the actual policies.

Many thanks,
-- George

Feb 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
2
by: Rex_chaos | last post by:
Hi all, suppose A is an array. With the help of template function find_first_of and find_end, I got two desired elements of array. How can I tell the distance between these two elements?
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
13
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
6
by: alan.patterson5 | last post by:
I have a vector of N values and a vector of N functors. I want to apply each functor to its corresponding value. What would be the 'correct' STL way to do this. for_each takes on functor and...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
13
by: r.z. | last post by:
I logged construtor and destructor calls for one of my classes and I discovered that the constructor was called only once while the destructor was called 3 times for a single object. I have a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.