Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-02-13 17:56:31


----- Original Message -----
From: "Luis Pedro Coelho" <deepblack9_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, February 13, 2002 7:12 PM
Subject: Re: [boost] properties in C++

> Em Quarta, 13 de Fevereiro de 2002 16:01, escreveste:
> > When you do
> > A.value = 100 ;
> > how does the property 'value' receives &A?
> >
> > My point is that there is NO WAY to store &A inside 'value' at compile
> > time. You can store &A::foo, but without &A this still requires runtime
> > support.
>
> Off the top of my head:
>
> value doesn't know where A is, but it knows where it is and it knows that
it
> lies inside A. In typical implementations (the begginning of) A will lie
in a
> fixed offset from where value is.
>
Yes

> So
>
> void value::operator = (const int x) {
> A* a = reinterpret_cast<A*>(reinterpret_cast<unsigned char*>(this) -
offset);
> a->set_value(x);
> }
>
> All we got to do is find a way to calculate OFFSET at compile time.
Yes.

> I think
> that something like:
>
> T A::*ptr = &A::value;
>
> A* A = reinterpret_cast<A*>(&ptr);
> // <- I just use ptr, could be anything!
> T* t = A->*ptr;
> long offset = reinterpret_cast<unsigned char*>(t) -
reinterpret_cast<unsigned
> char*>(A);
>
I'm lost here...
What is A::value, a nested class type or a data member of type
property<...>?

> Of course, we can do this in one constant expression inside the template
> class definition:
>
> enum { offset = (reinterpret_cast<unsigned char*>
> (reinterpret_cast<unsigned char*>(&whatever)->*Ptr_to_member)
> -
> reinterpret_cast<unsigned char*>(&whatever)));
> }
>
> Where Ptr_to_member is passed as a template parameter.
>
This depends on the exact nature of &whatever and Ptr_to_member.
They have to be valid integral constant expressions, so they really can't be
'whatever'.

> Note that we have to write:
>
> boost::property<A, &A::value, read<&A::get_x>, write<&A::set_x> > value;
>
I still don't get what &A::value means, but I doubt it can be a constant
expression.

Let's go back to your original reasoning:

> value doesn't know where A is, but
> it knows where it is
> ...

'value', being in any case something inside an instance of class A, only
knows where it is at run-time, not at compile-time.
There is really no way to get to the final address of the actual 'value' at
compile-time.

Once again: the actual address of the any data inside any C++ object isn't
know at compile-time not at link-time, only at runtime.
There is no way out of this inherent limitation, you need one way or another
runtime support to bind the accesors to the actual value.

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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