1 /* 2 getter.h - classes to get a piece of data out of objects 3 Copyright (C) 2002 Matthew Mueller <donut AT dakotacom.net> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 #ifndef __GETTER_H__ 20 #define __GETTER_H__ 21 22 template <class RetType, class ClassType> 23 class MemGetter { 24 private: 25 typedef RetType ClassType::*member_t; 26 member_t member; 27 public: 28 //typedef const RetType &T; //may be faster for some types? maybe should be a seperate MemrefGetter class. 29 typedef RetType T; 30 typedef member_t init_t; MemGetter(init_t i)31 MemGetter(init_t i): member(i) {} operator()32 T operator()(ClassType *o) const {return o->*member;} 33 }; 34 35 template <class RetType, class ClassType> 36 class MemfuncGetter { 37 private: 38 typedef RetType (ClassType::*member_t)(void) const; 39 member_t member; 40 public: 41 typedef RetType T; 42 typedef member_t init_t; MemfuncGetter(init_t i)43 MemfuncGetter(init_t i): member(i) {} operator()44 T operator()(ClassType *o) const {return (o->*member)();} 45 }; 46 47 template <class RetType, class ClassType> 48 class FuncGetter { 49 private: 50 typedef RetType (*func_t)(ClassType *o); 51 func_t func; 52 public: 53 typedef RetType T; 54 typedef func_t init_t; FuncGetter(init_t i)55 FuncGetter(init_t i): func(i) {} operator()56 T operator()(ClassType *o) const {return func(o);} 57 }; 58 59 /* 60 template <class RetType, class ClassType, class getter_t> 61 class Getter { 62 private: 63 getter_t getter; 64 public: 65 typedef getter_t init_t; 66 Getter(init_t i): getter(i) {} 67 RetType operator()(ClassType *o); 68 }; 69 70 //func getter 71 template <class RetType, class ClassType> 72 class Getter<RetType, ClassType, RetType (*)(ClassType *)> { 73 public: 74 RetType operator()(ClassType *o) {return getter(o);} 75 }; 76 //member func getter 77 template <class RetType, class ClassType> 78 class Getter<RetType, ClassType, RetType (ClassType::*)(void)> { 79 public: 80 RetType operator()(ClassType *o) {return o->*getter();} 81 }; 82 //member variable getter 83 template <class RetType, class ClassType> 84 class Getter<RetType, ClassType, RetType (ClassType::*)> { 85 public: 86 RetType operator()(ClassType *o) {return o->*getter;} 87 }; 88 */ 89 90 #endif 91