generic classes template function with function pointer and argumentnumber overloading (lol)

Discussion in 'C++' started by g3rc4n@gmail.com, Apr 18, 2009.

  1. Guest

    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::eek:ne),'a');
    return(0);
    }
    , Apr 18, 2009
    #1
    1. Advertising

  2. * :
    > 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__)


    Identifiers with double underscores are reserved to the implementation.

    Presumably you're using some names from your standard library implementation.

    Don't.


    > 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__{


    At this point there's no ITER__ or END__ in sight.

    Are you sure they're defined?


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, Apr 18, 2009
    #2
    1. Advertising

  3. Guest

    Re: generic classes template function with function pointer andargument number overloading (lol)

    On Apr 18, 5:04 am, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > 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__)

    >
    > Identifiers with double underscores are reserved to the implementation.
    >
    > Presumably you're using some names from your standard library implementation.
    >
    > Don't.
    >
    > > 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__{

    >
    > At this point there's no ITER__ or END__ in sight.
    >
    > Are you sure they're defined?
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
    > No ads, and there is some C++ stuff! :) Just going there is good. Linking
    > to it is even better! Thanks in advance!


    i thought it was only names that began with double underscores?
    , Apr 18, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weyoun the Dancing Bong

    LOL @ College course.

    Weyoun the Dancing Bong, Jan 20, 2004, in forum: HTML
    Replies:
    13
    Views:
    697
    Hywel Jenkins
    Jan 20, 2004
  2. Guest
    Replies:
    5
    Views:
    610
  3. SorceCode
    Replies:
    2
    Views:
    3,361
    Christian Janßen
    Jan 26, 2004
  4. Vijai Kalyan
    Replies:
    4
    Views:
    701
    Vijai Kalyan
    Nov 8, 2005
  5. Tuxedo
    Replies:
    11
    Views:
    167
    Tuxedo
    Sep 7, 2010
Loading...

Share This Page