iteration from pure abstract classes
I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things don't mix well. My best so far is to create an abstract "iterator_assister" which provides dereference, (in|de)crement and is_equal, and use a boost::iterator_adaptor derived class to provide the iterator the client code uses, and allocate the iterator_assister derived class from the implementation class (derived from A) that's being iterated over. This solution has obvious downsides. I can't imagine that this is a unique problem. Certainly, people significantly smarter than me have figured out a good solution to this? In my reading and browsing, though, I've never seen the problem presented, never mind solved. In my early C++ days, we used allocated iterators rather than the current STL style. Can anyone provide pointers to what I SHOULD be reading for a good solution? I believe I NEED to use abstract classes here rather than meta- programming with known types at runtime - the client code will need to iterate over classes loaded from dynamic libraries that are not known to the client code (except by abstract interface). Thanks Hugh Hoover Enumclaw Software
Hugh Hoover
I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things don't mix well. My best so far is to create an abstract "iterator_assister" which provides dereference, (in|de)crement and is_equal, and use a boost::iterator_adaptor derived class to provide the iterator the client code uses, and allocate the iterator_assister derived class from the implementation class (derived from A) that's being iterated over.
Have you looked at boost::indirect_iterator? Just make a container of pointers to the abstract base and iterate over that :)
This solution has obvious downsides. I can't imagine that this is a unique problem. Certainly, people significantly smarter than me have figured out a good solution to this? In my reading and browsing, though, I've never seen the problem presented, never mind solved. In my early C++ days, we used allocated iterators rather than the current STL style.
Can anyone provide pointers to what I SHOULD be reading for a good solution?
I believe I NEED to use abstract classes here rather than meta- programming with known types at runtime - the client code will need to iterate over classes loaded from dynamic libraries that are not known to the client code (except by abstract interface).
There are sometimes interesting ways to move the boundary between static and dynamic polymorphism around that can solve problems like this one. http://boost-consulting.com/slides/connections05/Life%20On%20the%20Edge.ppt might be of some use to you. -- Dave Abrahams Boost Consulting www.boost-consulting.com
Hi, from Japan.I'm pretty new to c++ and I'm trying to work out on regex.
When I compile these lines,it ends with "undefined reference to ..."
Please let me know how to use regex properly.
I use on FC4 and newest boost version,compiling in Anjuta.
******************************
#include <iostream>
#include
Hi, from Japan.I'm pretty new to c++ and I'm trying to work out on regex. When I compile these lines,it ends with "undefined reference to ..." Please let me know how to use regex properly. I use on FC4 and newest boost version,compiling in Anjuta.
You don't say what compiler you're using? Presumably you need to add the -lboost_regex to your linkers command line. Note also that [0-9] will only match the first character of "1234567890" so the call to regex_match will fail (regex_match requires that the whole of the text is matched in order to succeed). Use regex_search if you want to match a sub-string of the text. John.
Hello,Sir and everybody.
Presumably you need to add the -lboost_regex to your linkers command line.
OK,it worked!!
Thank you for your advice.
I'v change my test program a little.
**************************
#include <iostream>
#include
Hi, from Japan.I'm pretty new to c++ and I'm trying to work out on regex. When I compile these lines,it ends with "undefined reference to ..." Please let me know how to use regex properly. I use on FC4 and newest boost version,compiling in Anjuta.
You don't say what compiler you're using? Presumably you need to add the -lboost_regex to your linkers command line.
Note also that [0-9] will only match the first character of "1234567890" so the call to regex_match will fail (regex_match requires that the whole of the text is matched in order to succeed). Use regex_search if you want to match a sub-string of the text.
John.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Dec 3, 2005, at 15:44, David Abrahams wrote:
Hugh Hoover
writes: I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things
<snip>
Have you looked at boost::indirect_iterator? Just make a container of pointers to the abstract base and iterate over that :)
Thanks for the suggestion, but that won't really work. <snip>
I believe I NEED to use abstract classes here rather than meta- programming with known types at runtime - the client code will need to iterate over classes loaded from dynamic libraries that are not known to the client code (except by abstract interface).
There are sometimes interesting ways to move the boundary between static and dynamic polymorphism around that can solve problems like this one. http://boost-consulting.com/slides/connections05/Life%20On%20the% 20Edge.ppt might be of some use to you.
Thanks - very useful, but I'm not sure it's useful for this problem :) I can't see how to reconcile my requirement for handling classes not known at compile time (in fact, CANNOT be known at compile time due to the dynamically loaded module requirement) with meta-programming. While I'd PREFER a meta-programming approach for performance and simplicity - I'm just not grokking an approach here other than the use of virtual functions. The notion introduced in the "loans" example of separating iteration by type may help, and I'll need to think on that more - perhaps pushing the iteration itself into the class-dependent code. In the meantime - I wrote a template that makes creating the iterators (and assistant) in the abstract interface easier ;^) Hugh Hoover Enumclaw Software
Hugh Hoover
On Dec 3, 2005, at 15:44, David Abrahams wrote:
Hugh Hoover
writes: I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things
<snip>
Have you looked at boost::indirect_iterator? Just make a container of pointers to the abstract base and iterate over that :)
Thanks for the suggestion, but that won't really work.
Why? -- Dave Abrahams Boost Consulting www.boost-consulting.com
On Dec 6, 2005, at 12:43, David Abrahams wrote:
Hugh Hoover
writes: On Dec 3, 2005, at 15:44, David Abrahams wrote:
Hugh Hoover
writes: I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things
<snip>
Have you looked at boost::indirect_iterator? Just make a container of pointers to the abstract base and iterate over that :)
Thanks for the suggestion, but that won't really work.
Why?
mainly performance. Imagine a tree structure where the nodes are of different concrete types based on a common abstract interface. So far, no problem... But, the nodes have subtree arity ranging from 0 to >10,000 - to walk the structure means creating a new vector for each node visited with a size equal to the node's arity, and initializing from another structure (which may not be a vector). If I use some kind of abstract iterator interface, the >iterator< gets allocated, but can walk the concrete structure directly. Clearly, there's a tradeoff here - and I'm betting that the abstract iterator is cheaper than the vector - I haven't proven that. Hugh Hoover Enumclaw Software
Hugh Hoover
On Dec 6, 2005, at 12:43, David Abrahams wrote:
Hugh Hoover
writes: On Dec 3, 2005, at 15:44, David Abrahams wrote:
Hugh Hoover
writes: I have a need to provide STL compliant iterators from an abstract class A, that is, without knowing the runtime type of the actual "collection" being iterated.. As near as I can tell, these things
<snip>
Have you looked at boost::indirect_iterator? Just make a container of pointers to the abstract base and iterate over that :)
Thanks for the suggestion, but that won't really work.
Why?
mainly performance. Imagine a tree structure where the nodes are of different concrete types based on a common abstract interface. So far, no problem... But, the nodes have subtree arity ranging from 0 to >10,000 - to walk the structure means creating a new vector for each node visited with a size equal to the node's arity, and initializing from another structure (which may not be a vector).
If I use some kind of abstract iterator interface, the >iterator< gets allocated, but can walk the concrete structure directly. Clearly, there's a tradeoff here - and I'm betting that the abstract iterator is cheaper than the vector - I haven't proven that.
Oh, are you iterating over homogeneous containers of X1,...Xn where every Xi is derived from some common base class, B? If instead you are iterating over containers of heterogeneous derived classes, I can't understand how you can do better than what I proposed. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
Hugh Hoover
-
John Maddock
-
shintarou_fujiwara