File: settergetter.h

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (106 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// settergetter.h

#ifndef SETTERGETTER_H
#define SETTERGETTER_H

#include "mxv_types.h"

template <class Type>
class SetterGetter {
public:
        virtual ~SetterGetter() {}
        virtual boolean set(Type) = 0;
        virtual Type get() const = 0;
};

template <class Type>
class PointerSetterGetter : public SetterGetter<Type> {
public:
        PointerSetterGetter(Type *val) : ptr(val) {}
	redefined boolean set(Type val) { *ptr = val; return true; }
	redefined Type get() const { return *ptr; }
private:
	Type* ptr;
};

template <class Type>
inline SetterGetter<Type>* createSetterGetter(Type* val) {
	return new PointerSetterGetter<Type>(val);
}

template <class Object, class Type>
class MethodVoidSetterGetter : public SetterGetter<Type> {
public:
        MethodVoidSetterGetter(Object* objptr,
			     void (Object::*setfunc)(Type),
			     Type (Object::*getfunc)() const)
		: obj(objptr), setter(setfunc), getter(getfunc) {}
	redefined boolean set(Type value) {
		(obj->*setter)(value); return true;
	}
	redefined Type get() const { return (obj->*getter)(); }
private:
	Object* obj;
	void (Object::*setter)(Type);
	Type (Object::*getter)() const;
};

#ifndef __GNUG__

template <class Object, class Type>
inline SetterGetter<Type>* createSetterGetter(
		Object* objptr,
		void (Object::*setfunc)(Type),
		Type (Object::*getfunc)() const) {
	return new MethodVoidSetterGetter<Object, Type>(objptr, setfunc, getfunc);
}

#endif

template <class Object, class Status, class Type>
class MethodSetterGetter : public SetterGetter<Type> {
public:
        MethodSetterGetter(Object* objptr,
			     Status (Object::*setfunc)(Type),
			     Type (Object::*getfunc)() const)
		: obj(objptr), setter(setfunc), getter(getfunc) {}
	redefined boolean set(Type value) {
		return boolean((obj->*setter)(value));
	}
	redefined Type get() const { return (obj->*getter)(); }
private:
	Object* obj;
	Status (Object::*setter)(Type);
	Type (Object::*getter)() const;
};

template <class Object, class Status, class Type>
inline SetterGetter<Type>* createSetterGetter(
		Object* objptr,
		Status (Object::*setfunc)(Type),
		Type (Object::*getfunc)() const) {
	return new MethodSetterGetter<Object, Status, Type>(objptr, setfunc, getfunc);
}

//********

template <class Status, class Type>
class FunctionSetterGetter : public SetterGetter<Type> {
public:
        FunctionSetterGetter(Status (*setfunc)(Type), Type (*getfunc)())
		: setter(setfunc), getter(getfunc) {}
	redefined boolean set(Type value) { (*setter)(value);  return true; }
	redefined Type get() const { return (*getter)(); }
private:
	Status (*setter)(Type);
	Type (*getter)();
};

template <class Status, class Type>
inline SetterGetter<Type>* createSetterGetter(
		Status (*setfunc)(Type),
		Type (*getfunc)()) {
	return new FunctionSetterGetter<Status, Type>(setfunc, getfunc);
}

#endif