EXT C++ Library - vartor

The ext::vartor wrapper template maps access to a member variable to calling a set and get member functions, effectively implementing what is called a property in few other programming languages.


Details:

The ext::vartor provides a syntactic conversion of calling a member functions into accessing a member variable. The ext::vartor has two specializations, one for ext::policy::none policy, this is default, and one for ext::policy::cached. The cached version backs the retrieved value up and provides it as a hint when reading again. See S and G member functions below for details.

For simple, read-only, mapping see ext::accessor template.

Interface:

template <typename T,
          typename C,
          T (C::* S)(const T &),
          T (C::* G)(const T &) const
          ext::policy::type Policy = ext::policy::none>
class ext::vartor {
    public:
        explicit vartor (C *);
        vartor (C *, const T &);

        vartor & operator = (const T & v);
        operator T () const;

        vartor & operator ++ ();
        vartor & operator -- ();
        const T  operator ++ (int);
        const T  operator -- (int);

        vartor & operator += (const T &);
        vartor & operator -= (const T &);
        vartor & operator *= (const T &);
        vartor & operator /= (const T &);
        vartor & operator %= (const T &);

        vartor & operator >>= (int);
        vartor & operator <<= (int);
        vartor & operator |= (const T &);
        vartor & operator &= (const T &);
        vartor & operator ^= (const T &);
};

Examples:

#include <ext/c++>

class myClass {
    private:
        int set_x (const int & x) {
            // ...
            return t;
        };
        int get_x (const int & t) {
            // ...
            return x;
        };

    public:
        ext::vartor <int, myClass, &myClass::set_x, &myClass::get_x> x;

    public:
        myClass ()
            : x (this) {};

};

int main () {
    myClass c;

    c.x = 7; // calls myClass::set_x
    c.x += 2; // calls myClass::get_x and myClass::set_x
    int y = c.x; // calls myClass::get_x

    return y;
};

Remarks

Note that ext::policy::cached version does not cache by itself. It does only provide means for implementing the caching or similar feature. It is up to the user how is the internal value used.


[index]