Re: Overloaded constructors and cast operators for Glib::Value?



On Fri, Jan 07, 2005 at 21:07:09 +0100, Murray Cumming wrote:
> On Fri, 2005-01-07 at 20:26 +0100, Jan Hudec wrote:
> > Hello All,
> > 
> > I am planning a project. It's in a design stage. For that project,
> > I need a generic value type.
> > 
> > By that I mean, that it should be possible to construct that type from
> > any (some reasonable set) other type and be castable to any such type,
> > perhaps using dynamic_cast method like RefPtr does. This cast or method
> > would throw bad_cast, if the object actualy contained incompatible type.
> > 
> > What I want is to allow:
> > 
> >     void foo(int x);
> > 
> >     Value a = 1;
> >     Value b = "string";
> >     
> >     foo(a); // OK, a is an integer
> >     foo(b); // throws bad_cast, because it's not a number.
> > 
> > Since I will be passing that type to Gtkmm calls a lot, I want it
> > convertible to Glib::ValueBase.
> 
> Glib::Value and Glib::ValueBase base are used only rarely in the gtkmm
> API, so this would not be a big advantage.

I will be accessing object properties by names, for which they are
needed.

> However, I can see the advantage of reusing the existing code.

> > [...]
> >     // And now the highest madness! The cast operators!
> >     // FIXME This won't work. It does not do sane checking.
> >     template <class VT> operator VT()
> >     {
> > 	Glib::Value<VT> tmp;
> > 	tmp = *this; // FIXME This does not exist!
> > 	return tmp.get();
> >     }
> > [...] g_value_transform [...]

Is there anything I will have to take special care of when using the
g_value_transform here? Where do I get the proper C types?

Also do you see some way to make this work for enums/flags too? These
use Glib::Value_Enum/Glib::Value_Flags instead of Glib::Value, so this
template won't do. (If g_value_transform allows transforming enum/flags
to integer, than it will work, but won't do the typechecking as good as
it could).

Thanks.

-------------------------------------------------------------------------------
						 Jan 'Bulb' Hudec <bulb ucw cz>

Attachment: signature.asc
Description: Digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]