Boost logo

Boost Users :

Subject: [Boost-users] make member functions (in)accessible
From: Stefan Funck (stefanfunck_at_[hidden])
Date: 2009-02-26 09:04:01


Dear all,

I'm trying to find some magic that makes a member function of some object x
accessible if and only if it were called from within a class that has object x
as a member. Is there a (boost)-way of doing this?

As this may seem a bit obscure, I'm trying to give an example:

template <typename T, bool Writable>
class Property
{
public:
        T get() const;
        void set(const T& val);
private:
        T m_val;
};
is a simple class holding a value of T.

I have some classes holding potentially many Properties. I'd like to have these
properties publically available, some as only readable, others writable as well,
so that others (e.g. gui-classes) can access them:

class A
{
public:
        Property<double, true > propA;
        Property<int , false> propB;
};

so I may do:

A a;
a.propA.get();
a.propA.set(0.5);
a.propB.get();

but NOT:

a.propB.set(1);

because propB was defined with Writable=false. BUT, from within class A, I would
like to do

propB.set(1);

because otherwise this property is useless. (How) can this be achieved (with boost)?

I could make set() private in the Writable=false - specialisation of Property
and declare A as a friend, but that's prohibitive since Property shouldn't know
about A and it won't work for derived classes of A. Another option would be

class A
{
public:
        A() : propA(propA_priv), propB(propB_priv) {}

        PropertyWrapper<double, true > propA;
        PropertyWrapper<int , false> propB;
private:
        Property<double> propA_priv;
        Property<int > propB_priv;
};

so the actual properties are private and the wrapper provides the actual
interface to the outside world. But as there may be many properties I'd like to
get away without the wrapper objects ...

Thanks for any ideas!
Stefan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net