00001 #ifndef TAG_FN_H
00002 #define TAG_FN_H
00003
00004 #include <functional>
00005 #include <iterator>
00006
00007 namespace std {
00011 template <class Arg1, class Arg2, class Result>
00012 struct binary_function<Arg1, Arg2&, Result> {
00013 typedef Arg1 first_argument_type;
00014 typedef Arg2 second_argument_type;
00015 typedef Result result_type;
00016 };
00017 }
00018
00019 namespace tag {
00020
00024
00025
00026 template <typename A, typename m>
00027 struct mem_data_ref_t : std::unary_function<A &, m &> {
00028 m A::*data;
00029 inline mem_data_ref_t( m A::*d ) : data(d) {};
00030 inline m & operator()(A & instance) const {
00031 return instance.*data;
00032 }
00033 };
00034
00035 template <typename A, typename m>
00036 struct const_mem_data_ref_t : std::unary_function<const A &, const m &> {
00037 const m A::*data;
00038 inline const_mem_data_ref_t( const m A::*d ) : data(d) {};
00039 inline const m & operator()(const A & instance) const {
00040 return instance.*data;
00041 }
00042 };
00043
00044 template <typename A, typename m>
00045 inline struct mem_data_ref_t<A,m> mem_data_ref( m A::*data ){
00046 return mem_data_ref_t<A,m>(data);
00047 }
00048
00049 template <typename A, typename m>
00050 inline struct const_mem_data_ref_t<A,m> const_mem_data_ref( const m A::*data ){
00051 return const_mem_data_ref_t<A,m>(data);
00052 }
00053
00054 template <typename A, typename m>
00055 struct mem_data_t : std::unary_function<A *, m &> {
00056 m A::*data;
00057 inline mem_data_t( m A::*d ) : data(d) {};
00058 inline m & operator()(A * instance) const {
00059 return instance->*data;
00060 }
00061 };
00062
00063 template <typename A, typename m>
00064 struct const_mem_data_t : std::unary_function<const A *, const m &> {
00065 const m A::*data;
00066 inline const_mem_data_t(const m A::*d ) : data(d) {};
00067 inline const m & operator()(const A * instance) const {
00068 return instance->*data;
00069 }
00070 };
00071
00072 template <typename A, typename m>
00073 inline struct mem_data_t<A,m> mem_data( m A::*data ){
00074 return mem_data_t<A,m>(data);
00075 }
00076
00077 template <typename A, typename m>
00078 inline struct const_mem_data_t<A,m> const_mem_data( const m A::*data ){
00079 return const_mem_data_t<A,m>(data);
00080 }
00081
00082 template <typename G, typename F>
00083 struct bind_t : std::unary_function<typename F::argument_type, typename G::result_type> {
00084 const F & f;
00085 const G & g;
00086 inline bind_t( const F & f_, const G & g_) :f(f_), g(g_) {};
00087 inline typename G::result_type operator()( typename F::argument_type a) const {
00088 return g(f(a));
00089 }
00090 };
00091
00092 template <typename G, typename F>
00093 inline struct bind_t<G,F> bind( const G & g, const F & f ){
00094 return bind_t<G,F>(f,g);
00095 }
00096
00098
00102
00103
00104 namespace Internal {
00105
00106 template <class T> struct make_const {
00107 typedef const T type;
00108 };
00109
00110 template <class T> struct make_const<T&> {
00111 typedef const T & type;
00112 };
00113
00114 template <class A, class B>
00115 struct forward_const {
00116 typedef B value_type;
00117 enum { CONST = 0 };
00118 };
00119
00120 template <class A, class B>
00121 struct forward_const<const A, B> {
00122 typedef typename make_const<B>::type value_type;
00123 enum { CONST = 1 };
00124 };
00125
00126 template <class A, class B>
00127 struct forward_const<A&, B> {
00128 typedef B value_type;
00129 enum { CONST = 2 };
00130 };
00131
00132 template <class A, class B>
00133 struct forward_const<const A&, B> {
00134 typedef typename make_const<B>::type value_type;
00135 enum { CONST = 3 };
00136 };
00137
00138 template <class A, class B>
00139 struct forward_const<A *, B> {
00140 typedef B value_type;
00141 enum { CONST = 4 };
00142 };
00143
00144 template <class A, class B>
00145 struct forward_const<const A *, B> {
00146 typedef typename make_const<B>::type value_type;
00147 enum { CONST = 5 };
00148 };
00149
00150 }
00151
00162 template <typename It, typename m>
00163 struct member_iterator_t {
00164
00165 typedef typename std::iterator_traits<It>::value_type ParentValue;
00166
00167
00168 typedef typename std::iterator_traits<It>::iterator_category iterator_category;
00169 typedef m value_type;
00170 typedef typename std::iterator_traits<It>::difference_type difference_type;
00171 typedef typename Internal::forward_const<typename std::iterator_traits<It>::pointer, m *>::value_type pointer;
00172 typedef typename Internal::forward_const<typename std::iterator_traits<It>::reference, m &>::value_type reference;
00173
00174 It iterator;
00175 m ParentValue::*data;
00176
00177 inline member_iterator_t( m ParentValue::*d ) : data(d) {};
00178 inline member_iterator_t( const It & it, m ParentValue::*d ) : data(d) { iterator = it; };
00179
00180 template <typename Other> inline member_iterator_t & operator=(const Other & other) {
00181 iterator = other;
00182 return *this;
00183 }
00184 inline member_iterator_t & operator=( const member_iterator_t & other){
00185 data = other.data;
00186 iterator = other.iterator;
00187 return *this;
00188 }
00189
00190 inline reference operator*(void) const {
00191 return (*iterator).*data;
00192 }
00193 inline reference operator->(void) const {
00194 return iterator->*data;
00195 }
00196 inline reference operator[](difference_type n) const {
00197 return iterator[n].*data;
00198 }
00199 inline member_iterator_t & operator++() {
00200 ++iterator;
00201 return *this;
00202 }
00203 inline member_iterator_t operator++(int) {
00204 member_iterator_t tmp(*this);
00205 iterator++;
00206 return tmp;
00207 }
00208 inline member_iterator_t & operator--() {
00209 --iterator;
00210 return *this;
00211 }
00212 inline member_iterator_t operator--(int) {
00213 member_iterator_t tmp(*this);
00214 iterator--;
00215 return tmp;
00216 }
00217 inline member_iterator_t & operator+=(difference_type n){
00218 iterator+=n;
00219 return *this;
00220 }
00221 inline member_iterator_t & operator-=(difference_type n){
00222 iterator-=n;
00223 return *this;
00224 }
00225 template <typename Other>
00226 inline difference_type operator-(const Other & other) const {
00227 return (iterator - other);
00228 }
00229 inline difference_type operator-(const member_iterator_t & other) const {
00230 return (iterator - other.iterator);
00231 }
00232 template <typename Other>
00233 inline bool operator==(const Other & other) const {
00234 return (iterator == other);
00235 }
00236 inline bool operator==(const member_iterator_t & other) const {
00237 return (iterator == other.iterator);
00238 }
00239 template <typename Other>
00240 inline bool operator!=(const Other & other) const {
00241 return (iterator != other);
00242 }
00243 inline bool operator!=(const member_iterator_t & other) const {
00244 return (iterator != other.iterator);
00245 }
00246 };
00247
00259 template <typename It, typename m>
00260 inline struct member_iterator_t<It, m> member_iterator( const It & it, m std::iterator_traits<It>::value_type::*d ){
00261 return member_iterator_t<It, m>(it, d);
00262 }
00263
00265
00266 }
00267
00268 #endif // TAG_FN_H