
Peder Holt wrote:
<snip>
Thanks ! Very clever. The self() idea is very worthy, and I may steal it for my own idea/implementation about "property" in C++, which I have been working on indefinitely ( may never get completed to my satisfaction ). The metaprogramming is breathtaking, if I can slowly figure it out <g> . In my own implementation a "property" does not need to be nested class but can exist as a global/static object, so I don't know how I can use your self() idea but I really appreciate your doing it and publishing it.
Thank you :)
It would be very interesting to look at your implementation to see how you tackle the problem. Have you posted your code anywhere that I can look?
No, I have no posted it anywhere yet. I also have still to test all of it, and the parts I have tested have been only on VC9 although I feel confident it should work on gcc and Comeau also. I am unfortunately, in a way, one of those people who feel that an implementation of mine must be complete as far as code, documentation, and testing are concerned before I can post/release it anywhere. I am also much less of a template metaprogrammer, largely by practice, than you and other Boost programmers so my implementation of "property" is much simpler in template metaprogramming techniques than what you present. Also my goals are different. If you are still interested I would be glad to zip up what I have done so far, albeit with no documentation as yet although I would be willing to discuss it privately, and send it directly to you, but I fear this private exchange is not in the spirit of Boost. You of course would have no obligation to pay any attention to it if you like, especially since my goals for a C++ "property" implementation are somewhat different. I can not see posting it anywhere until I am happy that my implementation is complete given my goals, but I can assure you that my reply was not an effort to "tease" you or anyone with my own implementation but rather simply to show my interest in a C++ "property" implementation and in appreciation of you own techniques in implementing your C#-like property.
Actually, the property implementation does not have to be a nested class.
What I should have said is that my "property" does not have to be a variable which is a member of a class. It can be a global/static variable. So there is not necessarily a "self", which is an enclosing class pointer. Also my property comes in many flavors, with slightly different names, corresponding to the various ways in which one can refer to the backing "data". In some of those flavors no actual data in memory needs to even exist. This latter may of course be seen as overkill. However I intend to study your code to see if it is possible to use the self() technique when a flavor of my property is used as a member of a class, and still be able go discard the self() implementation when it is not used as a member of a class, without having to bifurcate my property flavors under more different names.
The following: template<typename Base> class Property_Length : public Base { public: const double& get() const { return sqrt( Base::self()->X*Base::self()->X+ Base::self()->Y*Base::self()->Y+ Base::self()->Z*Base::self()->Z ); } }; template<typename Base> class Property_Double : public Base { public: const double& get() const {return m_arg;} void set(const double& arg) {m_arg=arg;} private: double m_arg; }; class Vector { public: BOOST_PROPERTY(Vector,Property_Double,X); BOOST_PROPERTY(Vector,Property_Double,Y); BOOST_PROPERTY(Vector,Property_Double,Z); BOOST_PROPERTY(Vector,Property_Length,Length); };
is legal. (but untested.)
Yes, this is understandable, thanks ! Your BOOST_PROPERTY properties must still be members of a class, but that follows perfectly your design goals of emulating C#-style properties.