[ create a new paste ] login | about

Link: http://codepad.org/Y0z3nEk2    [ raw code | output | fork ]

burbelgruff - C++, pasted on Oct 21:
#define BOOST_PROPERTY_OFFSET(Class,Name)\
        size_t offset() const {\
            union {\
                Class* class_pointer;\
                size_t rep;\
            } c;\
            c.rep=0;\
            return size_t(&c.class_pointer->Name);\
        }\

#define BOOST_PROPERTY_ACCESS_SELF(Class,Name)\
    BOOST_PROPERTY_OFFSET(Class,Name)\
    Class* self()\
    {\
        return reinterpret_cast<Class*>(size_t(this)-offset());\
    }\
    const Class* self() const\
    {\
        return reinterpret_cast<Class*>(size_t(this)-offset());\
    }

class my_vector {
public:    
    struct property_x {
    private:
        BOOST_PROPERTY_ACCESS_SELF(my_vector,x);
        friend class my_vector;
    public:
        const double& operator()() const;
        void operator()(const double& arg);
    private:
        double m_x;
    } x;

    void refresh() {std::cout <<std::endl << "Changed x to " << x() << std::endl;}
};

const double& my_vector::property_x::operator()() const
{
    return m_x;
}

void my_vector::property_x::operator()(const double& arg)
{
    m_x=arg;
    self()->refresh();
}

int main()
{
    my_vector a;
    a.x(5);    
}


Output:
1
2

Changed x to 5


Create a new paste based on this one


Comments: