function objects instead of function pointers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    function objects instead of function pointers?

    Hi all,

    I am implementing an object that is currently using function pointers
    for callbacks to let me do something on assignment:

    template <class T>
    class MyClass{
    private:
    typedef T& (* writer) (T& oldval, T& newval);
    writer onwrite;

    static T& defaultWrite(T& oldval, T& newval)
    {return oldval = newval;}

    public:
    MyClass() : onwrite(default Write) {}
    MyClass(writer w) : onwrite(w) {}

    T& operator = (T rhs) {return onwrite(value, rhs.value);}
    };

    This class works really well for me, but I want to speed it up. I
    would rather use function objects, instead of function pointers...
    something like:

    template <class T>
    class MyNewClass{
    private:
    unary_function< T&, T> onwrite;

    class defaultWrite
    {
    public:
    T operator () (const T& oldval, const T& newval) const
    { return oldval = newval;}
    };

    public:
    MyClass() : onwrite(default Write) {}
    MyClass(unary_f unction<T&, T> w) : onwrite(w) {}

    T& operator = (T rhs) {return onwrite(value, rhs.value);}
    };

    Something like this would be ideal. Of course, unary_function does
    not define the () operator, so I get that that onwrite(value,
    rhs.value) does not evaluate as a function.

    What should I do to properly store a function object?

    Thanks,
    Brian
  • Rolf Magnus

    #2
    Re: function objects instead of function pointers?

    Brian wrote:
    [color=blue]
    > Hi all,
    >
    > I am implementing an object that is currently using function pointers
    > for callbacks to let me do something on assignment:
    >
    > template <class T>
    > class MyClass{
    > private:
    > typedef T& (* writer) (T& oldval, T& newval);
    > writer onwrite;
    >
    > static T& defaultWrite(T& oldval, T& newval)
    > {return oldval = newval;}
    >
    > public:
    > MyClass() : onwrite(default Write) {}
    > MyClass(writer w) : onwrite(w) {}
    >
    > T& operator = (T rhs) {return onwrite(value, rhs.value);}
    > };
    >
    > This class works really well for me, but I want to speed it up. I
    > would rather use function objects, instead of function pointers...[/color]

    You mean you want to be able to choose the function at run time? I think
    then there is not much you can do to speed it up.
    [color=blue]
    > something like:
    >
    > template <class T>
    > class MyNewClass{
    > private:
    > unary_function< T&, T> onwrite;[/color]

    You have an instance of unary_function here, so the member functions are
    fixed at that point and can't be changed at run time. If OTOH, you only
    need to select it at object creation time, you can make the function
    object type a template parameter.
    [color=blue]
    >
    > class defaultWrite
    > {
    > public:
    > T operator () (const T& oldval, const T& newval) const
    > { return oldval = newval;}
    > };
    >
    > public:
    > MyClass() : onwrite(default Write) {}
    > MyClass(unary_f unction<T&, T> w) : onwrite(w) {}
    >
    > T& operator = (T rhs) {return onwrite(value, rhs.value);}
    > };
    >
    > Something like this would be ideal. Of course, unary_function does
    > not define the () operator, so I get that that onwrite(value,
    > rhs.value) does not evaluate as a function.
    >
    > What should I do to properly store a function object?[/color]

    Comment

    • Brian

      #3
      Re: function objects instead of function pointers?

      Rolf Magnus <ramagnus@t-online.de> wrote in message news:<bnp4gi$hj $07$1@news.t-online.com>...[color=blue]
      > Brian wrote:
      >[color=green]
      > > Hi all,
      > >
      > > I am implementing an object that is currently using function pointers
      > > for callbacks to let me do something on assignment:
      > >
      > > template <class T>
      > > class MyClass{
      > > private:
      > > typedef T& (* writer) (T& oldval, T& newval);
      > > writer onwrite;
      > >
      > > static T& defaultWrite(T& oldval, T& newval)
      > > {return oldval = newval;}
      > >
      > > public:
      > > MyClass() : onwrite(default Write) {}
      > > MyClass(writer w) : onwrite(w) {}
      > >
      > > T& operator = (T rhs) {return onwrite(value, rhs.value);}
      > > };
      > >
      > > This class works really well for me, but I want to speed it up. I
      > > would rather use function objects, instead of function pointers...[/color]
      >
      > You mean you want to be able to choose the function at run time? I think
      > then there is not much you can do to speed it up.
      >[color=green]
      > > something like:
      > >
      > > template <class T>
      > > class MyNewClass{
      > > private:
      > > unary_function< T&, T> onwrite;[/color]
      >
      > You have an instance of unary_function here, so the member functions are
      > fixed at that point and can't be changed at run time. If OTOH, you only
      > need to select it at object creation time, you can make the function
      > object type a template parameter.
      >[/color]

      Indeed. I am looking to do something like this, where the callback is
      set at object instance's creation, and is used throughout the object's
      life. I have tried doing it with templates, but I begin to get
      confused at that level. Any pointers?

      Thanks,
      B

      Comment

      • Rolf Magnus

        #4
        Re: function objects instead of function pointers?

        Brian wrote:
        [color=blue]
        > Rolf Magnus <ramagnus@t-online.de> wrote in message
        > news:<bnp4gi$hj $07$1@news.t-online.com>...[color=green]
        >> Brian wrote:
        >>[color=darkred]
        >> > Hi all,
        >> >
        >> > I am implementing an object that is currently using function
        >> > pointers for callbacks to let me do something on assignment:
        >> >
        >> > template <class T>
        >> > class MyClass{
        >> > private:
        >> > typedef T& (* writer) (T& oldval, T& newval);
        >> > writer onwrite;
        >> >
        >> > static T& defaultWrite(T& oldval, T& newval)
        >> > {return oldval = newval;}
        >> >
        >> > public:
        >> > MyClass() : onwrite(default Write) {}
        >> > MyClass(writer w) : onwrite(w) {}
        >> >
        >> > T& operator = (T rhs) {return onwrite(value, rhs.value);}
        >> > };
        >> >
        >> > This class works really well for me, but I want to speed it up. I
        >> > would rather use function objects, instead of function pointers...[/color]
        >>
        >> You mean you want to be able to choose the function at run time? I
        >> think then there is not much you can do to speed it up.
        >>[color=darkred]
        >> > something like:
        >> >
        >> > template <class T>
        >> > class MyNewClass{
        >> > private:
        >> > unary_function< T&, T> onwrite;[/color]
        >>
        >> You have an instance of unary_function here, so the member functions
        >> are fixed at that point and can't be changed at run time. If OTOH,
        >> you only need to select it at object creation time, you can make the
        >> function object type a template parameter.
        >>[/color]
        >
        > Indeed. I am looking to do something like this, where the callback is
        > set at object instance's creation, and is used throughout the object's
        > life. I have tried doing it with templates, but I begin to get
        > confused at that level. Any pointers?[/color]

        Try something like this:

        #include <functional>

        template <class T>
        class defaultWriter : public std::binary_fun ction<T, T, T>
        {
        public:
        // oldval cannot be const, since you couldn't assign to it then
        T& operator () (T& oldval, const T& newval) const
        {
        return oldval = newval;
        }
        };

        template <class T, class W = defaultWriter<T > >
        class MyNewClass
        {
        private:
        W onwrite;
        public:
        explicit MyNewClass(W writer = W()) : onwrite(writer) {}
        // ...
        };

        Comment

        • Brian

          #5
          Re: function objects instead of function pointers?

          Rolf Magnus <ramagnus@t-online.de> wrote in message news:<bnrntg$2v g$02$1@news.t-online.com>...[color=blue]
          > Brian wrote:
          >[color=green]
          > > Rolf Magnus <ramagnus@t-online.de> wrote in message
          > > news:<bnp4gi$hj $07$1@news.t-online.com>...[color=darkred]
          > >> Brian wrote:
          > >>
          > >> > Hi all,
          > >> >
          > >> > I am implementing an object that is currently using function
          > >> > pointers for callbacks to let me do something on assignment:
          > >> >
          > >> > template <class T>
          > >> > class MyClass{
          > >> > private:
          > >> > typedef T& (* writer) (T& oldval, T& newval);
          > >> > writer onwrite;
          > >> >
          > >> > static T& defaultWrite(T& oldval, T& newval)
          > >> > {return oldval = newval;}
          > >> >
          > >> > public:
          > >> > MyClass() : onwrite(default Write) {}
          > >> > MyClass(writer w) : onwrite(w) {}
          > >> >
          > >> > T& operator = (T rhs) {return onwrite(value, rhs.value);}
          > >> > };
          > >> >
          > >> > This class works really well for me, but I want to speed it up. I
          > >> > would rather use function objects, instead of function pointers...
          > >>
          > >> You mean you want to be able to choose the function at run time? I
          > >> think then there is not much you can do to speed it up.
          > >>
          > >> > something like:
          > >> >
          > >> > template <class T>
          > >> > class MyNewClass{
          > >> > private:
          > >> > unary_function< T&, T> onwrite;
          > >>
          > >> You have an instance of unary_function here, so the member functions
          > >> are fixed at that point and can't be changed at run time. If OTOH,
          > >> you only need to select it at object creation time, you can make the
          > >> function object type a template parameter.
          > >>[/color]
          > >
          > > Indeed. I am looking to do something like this, where the callback is
          > > set at object instance's creation, and is used throughout the object's
          > > life. I have tried doing it with templates, but I begin to get
          > > confused at that level. Any pointers?[/color]
          >
          > Try something like this:
          >
          > #include <functional>
          >
          > template <class T>
          > class defaultWriter : public std::binary_fun ction<T, T, T>
          > {
          > public:
          > // oldval cannot be const, since you couldn't assign to it then
          > T& operator () (T& oldval, const T& newval) const
          > {
          > return oldval = newval;
          > }
          > };
          >
          > template <class T, class W = defaultWriter<T > >
          > class MyNewClass
          > {
          > private:
          > W onwrite;
          > public:
          > explicit MyNewClass(W writer = W()) : onwrite(writer) {}
          > // ...
          > };[/color]


          BEAUTIFUL!!! That is exactly what I was trying to do. I added a
          reader "callback", and created a read-only version, and the function
          object version out-performs the function pointer version by a factor
          of 12. Very nice.

          Thanks for the pointers (or should I say, removal of pointers... hur
          hur hur),
          Brian

          Comment

          Working...