diff options
Diffstat (limited to 'apps/X11/VCL/property')
-rw-r--r-- | apps/X11/VCL/property | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/apps/X11/VCL/property b/apps/X11/VCL/property new file mode 100644 index 0000000..aee9eda --- /dev/null +++ b/apps/X11/VCL/property @@ -0,0 +1,77 @@ +#ifndef PROPERTY_H +#define PROPERTY_H + +#include <iostream> + +template<class Context, class T> struct Property_index; + +template<class Context, class T> +class property { + Context* view; // an Item needs to access values in its context + T (Context::*Getter)(); + void (Context::*Setter)(T); + +protected: + T get() const { return (Getter) ? (view->*Getter)() : 0; } + void set(T val) { if (Setter) (view->*Setter)(val); } + +public: + property(Context* v, + T (Context::*G)() = &Context::get, + void (Context::*S)(T) = &Context::set) + :view(v), Getter(G), Setter(S) { } + + operator T() const { return get(); } + property& operator=(T val) { set(val); return *this; } + property& operator+=(T val) { set(get() + val); return *this; } + property& operator=(const property &other) { set(T(other)); return *this; } + + // specialized operations: + + T operator->() { return get(); } + + // string specific + property<Property_index<Context,T>,char> operator[](int i); + int size() { return (view->*Getter)().size(); } + +}; + +template<class Context, class T> +struct Property_index { // inefficient + property<Context,T>* p; + int i; + Property_index(property<Context,T>* pp, int ii) : p(pp), i(ii) { } + char get() const { return p->get()[i]; } + void set(char ch) { T v = p->get(); v[i] = ch; p->set(v); } +}; + +template<class Context, class T> +property<Property_index<Context,T>,char> property<Context,T>::operator[](int i) +{ + return property<Property_index<Context,T>,char>(new Property_index<Context,T>(this,i)); +// leaks +} + +template<class Context, class T> +inline ostream& operator<<(ostream& s, property<Context,T> p) +{ + s << T(p); +} + +template<class Context, class T> +inline istream& operator>>(istream& s, property<Context,T>& p) +{ + T t; + s >> t; + p = t; +} + + + +/* examples +property<TTreeView,TTreeNodes*> Items; +Items(this, &get, &set); +*/ + + +#endif |