Boost logo

Boost :

From: Lin Xu (risingdragon3_at_[hidden])
Date: 2003-05-06 23:23:12


I have just been reading about dynamic any. As far as I can tell, it allows
the user to implement an interface to extend an any, so one can call
functions and access the internal value of the any without knowing the type.

I think this is a very good idea, but I'm not sure how complex adding new
'functions' / 'interfaces' is. It seems to me (seeing the new io_any) that
is looks like you need to define a class that inherits from the template
that takes a fuction type as the parameter. Then AFAIK (I coudl be wrong
here, probably am) the any template uses that...
First of all, are function calls supported? I can't see anything like that
and the documentation isn't completed. I'm not sure because I dont'
understand how you are distugishing between each 'function' addon (like
writer, and reader, or adder and subtracter)

I implemented a similar class, but using what appears to be a totally
different implementation method. I'm not sure if I shoudl continue to
develop it (I'm trying to allow casts between any_interface aka a
dynamic_any - which allows virtual templates ;) and other cool stuff. There
is always a trade off though, either register the interface or the type
held..so I'm trying other techniques. )
Here is an useage example..

struct printable {
struct intf {
virtual void print(ostream& out) = 0;
friend ostream& operator<<(ostream& out,intf& val) {
val.print(out);
};
template <class parent> struct impl : public parent {
virtual void print(ostream& out) {
out << value();
}
};
};

any_interface<printable> a;
a = 10;
cout << *a << endl;
a = "hi, I'm a string";
cout << *a << endl;

This to me seems clearer and also allows functions and so on, for example..
(Of course the current implementation probably does, but I coudlnt' find
any documentation to that effect.) The syntax useage is similar to an
iterator: -> gets the interface as a pointer, * as an &.

any_interface<moveable> a;
a->move(10,10);
//or
(*a).move(10,10);

As well, it works on VC6 even. (I'm not sure if the current implement does,
but considering it looks like it wants to distugish a function template
paramter....)

My implementation provides the same as the current one, just rename
any_interface to dynamic_any.
I'm wondering if my implementation has some serious problem I'm not seeing?

The cost is a virtual function call per function call.

Comments?

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk