G
g3rc4n
help me, i could of sworn this was working a while ago, reason i ask
for help is that i don't trust vc++ errors
i'm trying to write a generic observer-subject class where a fucntion
pointer with variable number of arguments are passed then executed of
each element
also if anyone can think of a way round the macro without code
duplication your welcome to comment on the use of a "evil" macro in c+
+
#include <vector>
#include <functional>
#include <iostream>
#define GENERIC_IMPL_ITERATE__ \
for(VI ITER__(subscribers_.begin()),END__(subscribers_.end()); \
ITER__!=END__; \
++ITER__)
template<class T>
struct generic_impl{
typedef std::vector<T*> vector_t;
typedef typename vector_t::iterator VI;
void on_eachh(void* fun){
typedef void(*fun_t)(T*);
GENERIC_IMPL_ITERATE__{
(reinterpret_cast<fun_t>(fun))(*ITER__);
}
}
template<typename FUN>
void on_each(FUN fun){
GENERIC_IMPL_ITERATE__{
fun(*ITER__);
}
}
template<typename FUN, typename A>
void on_each(FUN fun, A& a){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a);
}
}
template<typename FUN, typename A, typename B>
void on_each(FUN fun, A& a, B& b){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a,b);
}
}
void subscribe(T* t){
subscribers_.push_back(t);
}
private:
vector_t subscribers_;
};
#undef GENERIC_IMPL_ITERATE__
struct foo{
void zero(){}
void one(char){}
void two(char,char){}
};
int main(){
generic_impl<foo> impl;
impl.on_each(reinterpret_cast<void*>(&foo::zero));
impl.on_each(std::mem_fun(&foo::zero));
impl.on_each(std::mem_fun(&foo:ne),'a');
return(0);
}
for help is that i don't trust vc++ errors
i'm trying to write a generic observer-subject class where a fucntion
pointer with variable number of arguments are passed then executed of
each element
also if anyone can think of a way round the macro without code
duplication your welcome to comment on the use of a "evil" macro in c+
+
#include <vector>
#include <functional>
#include <iostream>
#define GENERIC_IMPL_ITERATE__ \
for(VI ITER__(subscribers_.begin()),END__(subscribers_.end()); \
ITER__!=END__; \
++ITER__)
template<class T>
struct generic_impl{
typedef std::vector<T*> vector_t;
typedef typename vector_t::iterator VI;
void on_eachh(void* fun){
typedef void(*fun_t)(T*);
GENERIC_IMPL_ITERATE__{
(reinterpret_cast<fun_t>(fun))(*ITER__);
}
}
template<typename FUN>
void on_each(FUN fun){
GENERIC_IMPL_ITERATE__{
fun(*ITER__);
}
}
template<typename FUN, typename A>
void on_each(FUN fun, A& a){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a);
}
}
template<typename FUN, typename A, typename B>
void on_each(FUN fun, A& a, B& b){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a,b);
}
}
void subscribe(T* t){
subscribers_.push_back(t);
}
private:
vector_t subscribers_;
};
#undef GENERIC_IMPL_ITERATE__
struct foo{
void zero(){}
void one(char){}
void two(char,char){}
};
int main(){
generic_impl<foo> impl;
impl.on_each(reinterpret_cast<void*>(&foo::zero));
impl.on_each(std::mem_fun(&foo::zero));
impl.on_each(std::mem_fun(&foo:ne),'a');
return(0);
}