Call Wrapper Class

Discussion in 'C++' started by GCRhoads, Nov 21, 2007.

  1. GCRhoads

    GCRhoads Guest

    I have some templated functions and I want to write a call wrapper
    class for it. I
    also want to pass the function object from this class to some
    functions that will then call the function (forwarded through my class
    object to the real function). I've looked around for sources on how
    to do this but I can't find anything. How do you do this? I would
    also appreciate any references to good sources about this topic if you
    have any.

    Thanks for your time.
    GCRhoads, Nov 21, 2007
    #1
    1. Advertising

  2. GCRhoads

    werasm Guest

    On Nov 21, 9:58 pm, GCRhoads <> wrote:
    > I have some templated functions and I want to write a call wrapper
    > class for it. I
    > also want to pass the function object from this class to some
    > functions that will then call the function (forwarded through my class
    > object to the real function). I've looked around for sources on how
    > to do this but I can't find anything. How do you do this? I would
    > also appreciate any references to good sources about this topic if you
    > have any.
    >
    > Thanks for your time.


    Sounds like what you want to do already exists. boost::function
    allows the wrapping of various types of functions, and as far
    as I know it is part of tr1 (as I recall). I'm not sure what
    you want though (but that is certainly "Call wrappers").

    Your question is quite vague. Perhaps add some sourcecode as
    starting point.

    Werner
    werasm, Nov 21, 2007
    #2
    1. Advertising

  3. GCRhoads

    GCRhoads Guest

    On Nov 21, 5:44 pm, werasm <> wrote:
    > On Nov 21, 9:58 pm, GCRhoads <> wrote:
    >
    > > I have some templated functions and I want to write a call wrapper
    > > class for it. I
    > > also want to pass the function object from this class to some
    > > functions that will then call the function (forwarded through my class
    > > object to the real function). I've looked around for sources on how
    > > to do this but I can't find anything. How do you do this? I would
    > > also appreciate any references to good sources about this topic if you
    > > have any.

    >
    > > Thanks for your time.

    >
    > Sounds like what you want to do already exists. boost::function
    > allows the wrapping of various types of functions, and as far
    > as I know it is part of tr1 (as I recall). I'm not sure what
    > you want though (but that is certainly "Call wrappers").
    >
    > Your question is quite vague. Perhaps add some sourcecode as
    > starting point.
    >
    > Werner


    I have several types of templated function objects (the function call
    has the same signature)
    and I want a wrapper class to be able to pass any of them to other
    functions.

    For example, the templated function objects are like the following.

    template<typename TreeType>
    struct Eval1
    {
    double operator() (const TreeType &tree, ...) {....}
    };


    template<typename TreeType>
    struct Eval2
    {
    double operator() (const TreeType &tree, ...) {....}
    };

    The number of function objects like this will grow in the future. Now
    I want a wrapper class that can hold any of these function objects and
    pass them to other functions.

    So I want to complete the following class

    template<typename TreeType>
    class FunctObj
    {
    };

    so that I can do the following.

    Eval1<HierarchyTree> funct1;
    Eval2<EnhancedHierarchyTree> funct2;

    FunctObj FO; // object of the class I am trying to
    write.

    Now I want to be able to assign either funct1 or funct2 to FO and pass
    FO to a function.
    How do I write the class FunctObj so that I can do this?

    The code fragment would look like the following.

    switch( user_option )
    {
    case 1:
    FO = funct1;
    break;

    case 2:
    FO = funct2;
    break;

    case 3:
    //etc.
    }

    my_function( FO, .... );

    -- Glenn
    GCRhoads, Nov 23, 2007
    #3
  4. GCRhoads

    terminator Guest

    On Nov 23, 6:57 pm, GCRhoads <> wrote:
    > On Nov 21, 5:44 pm, werasm <> wrote:
    >
    >
    >
    >
    >
    > > On Nov 21, 9:58 pm, GCRhoads <> wrote:

    >
    > > > I have some templated functions and I want to write a call wrapper
    > > > class for it. I
    > > > also want to pass the function object from this class to some
    > > > functions that will then call the function (forwarded through my class
    > > > object to the real function). I've looked around for sources on how
    > > > to do this but I can't find anything. How do you do this? I would
    > > > also appreciate any references to good sources about this topic if you
    > > > have any.

    >
    > > > Thanks for your time.

    >
    > > Sounds like what you want to do already exists. boost::function
    > > allows the wrapping of various types of functions, and as far
    > > as I know it is part of tr1 (as I recall). I'm not sure what
    > > you want though (but that is certainly "Call wrappers").

    >
    > > Your question is quite vague. Perhaps add some sourcecode as
    > > starting point.

    >
    > > Werner

    >
    > I have several types of templated function objects (the function call
    > has the same signature)
    > and I want a wrapper class to be able to pass any of them to other
    > functions.
    >
    > For example, the templated function objects are like the following.
    >
    > template<typename TreeType>
    > struct Eval1
    > {
    > double operator() (const TreeType &tree, ...) {....}
    > };
    >
    > template<typename TreeType>
    > struct Eval2
    > {
    > double operator() (const TreeType &tree, ...) {....}
    > };
    >
    > The number of function objects like this will grow in the future. Now
    > I want a wrapper class that can hold any of these function objects and
    > pass them to other functions.
    >
    > So I want to complete the following class
    >
    > template<typename TreeType>
    > class FunctObj
    > {
    > };
    >
    > so that I can do the following.
    >
    > Eval1<HierarchyTree> funct1;
    > Eval2<EnhancedHierarchyTree> funct2;
    >
    > FunctObj FO; // object of the class I am trying to
    > write.
    >
    > Now I want to be able to assign either funct1 or funct2 to FO and pass
    > FO to a function.
    > How do I write the class FunctObj so that I can do this?
    >
    > The code fragment would look like the following.
    >
    > switch( user_option )
    > {
    > case 1:
    > FO = funct1;
    > break;
    >
    > case 2:
    > FO = funct2;
    > break;
    >
    > case 3:
    > //etc.
    > }
    >

    make FuncObj the abstract base to all other templates and use a
    pointer inside the switch block instead of an object.

    regards,
    FM.
    terminator, Nov 23, 2007
    #4
  5. GCRhoads

    GCRhoads Guest

    On Nov 23, 3:51 pm, terminator <> wrote:
    > On Nov 23, 6:57 pm, GCRhoads <> wrote:
    >
    > > On Nov 21, 5:44 pm, werasm <> wrote:

    >
    > > > On Nov 21, 9:58 pm, GCRhoads <> wrote:

    >
    > > > > I have some templated functions and I want to write a call wrapper
    > > > > class for it. I
    > > > > also want to pass the function object from this class to some
    > > > > functions that will then call the function (forwarded through my class
    > > > > object to the real function). I've looked around for sources on how
    > > > > to do this but I can't find anything. How do you do this? I would
    > > > > also appreciate any references to good sources about this topic if you
    > > > > have any.

    >
    > > > > Thanks for your time.

    >
    > > > Sounds like what you want to do already exists. boost::function
    > > > allows the wrapping of various types of functions, and as far
    > > > as I know it is part of tr1 (as I recall). I'm not sure what
    > > > you want though (but that is certainly "Call wrappers").

    >
    > > > Your question is quite vague. Perhaps add some sourcecode as
    > > > starting point.

    >
    > > > Werner

    >
    > > I have several types of templated function objects (the function call
    > > has the same signature)
    > > and I want a wrapper class to be able to pass any of them to other
    > > functions.

    >
    > > For example, the templated function objects are like the following.

    >
    > > template<typename TreeType>
    > > struct Eval1
    > > {
    > > double operator() (const TreeType &tree, ...) {....}
    > > };

    >
    > > template<typename TreeType>
    > > struct Eval2
    > > {
    > > double operator() (const TreeType &tree, ...) {....}
    > > };

    >
    > > The number of function objects like this will grow in the future. Now
    > > I want a wrapper class that can hold any of these function objects and
    > > pass them to other functions.

    >
    > > So I want to complete the following class

    >
    > > template<typename TreeType>
    > > class FunctObj
    > > {
    > > };

    >
    > > so that I can do the following.

    >
    > > Eval1<HierarchyTree> funct1;
    > > Eval2<EnhancedHierarchyTree> funct2;

    >
    > > FunctObj FO; // object of the class I am trying to
    > > write.

    >
    > > Now I want to be able to assign either funct1 or funct2 to FO and pass
    > > FO to a function.
    > > How do I write the class FunctObj so that I can do this?

    >
    > > The code fragment would look like the following.

    >
    > > switch( user_option )
    > > {
    > > case 1:
    > > FO = funct1;
    > > break;

    >
    > > case 2:
    > > FO = funct2;
    > > break;

    >
    > > case 3:
    > > //etc.
    > > }

    >
    > make FuncObj the abstract base to all other templates and use a
    > pointer inside the switch block instead of an object.
    >
    > regards,
    > FM.


    Thanks.

    Since all the templated function objects have two arguments I was able
    to get it to work using the STL.


    #include <functional>

    For the function I want to pass other functions into, I changed its
    signature to

    template<class BinaryFunc>
    void MyFunct( BinaryFunc F, .... )
    {
    ...
    = F( ... );
    ...
    }


    inside the switch, I call this passing the various templated function
    objects as the "BinaryFunc" object. There may be a prettier way but
    it does work.
    GCRhoads, Nov 23, 2007
    #5
  6. GCRhoads

    terminator Guest

    On Nov 24, 12:20 am, GCRhoads <> wrote:
    > On Nov 23, 3:51 pm, terminator <> wrote:
    >
    >
    >
    >
    >
    > > On Nov 23, 6:57 pm, GCRhoads <> wrote:

    >
    > > > On Nov 21, 5:44 pm, werasm <> wrote:

    >
    > > > > On Nov 21, 9:58 pm, GCRhoads <> wrote:

    >
    > > > > > I have some templated functions and I want to write a call wrapper
    > > > > > class for it. I
    > > > > > also want to pass the function object from this class to some
    > > > > > functions that will then call the function (forwarded through my class
    > > > > > object to the real function). I've looked around for sources on how
    > > > > > to do this but I can't find anything. How do you do this? I would
    > > > > > also appreciate any references to good sources about this topic if you
    > > > > > have any.

    >
    > > > > > Thanks for your time.

    >
    > > > > Sounds like what you want to do already exists. boost::function
    > > > > allows the wrapping of various types of functions, and as far
    > > > > as I know it is part of tr1 (as I recall). I'm not sure what
    > > > > you want though (but that is certainly "Call wrappers").

    >
    > > > > Your question is quite vague. Perhaps add some sourcecode as
    > > > > starting point.

    >
    > > > > Werner

    >
    > > > I have several types of templated function objects (the function call
    > > > has the same signature)
    > > > and I want a wrapper class to be able to pass any of them to other
    > > > functions.

    >
    > > > For example, the templated function objects are like the following.

    >
    > > > template<typename TreeType>
    > > > struct Eval1
    > > > {
    > > > double operator() (const TreeType &tree, ...) {....}
    > > > };

    >
    > > > template<typename TreeType>
    > > > struct Eval2
    > > > {
    > > > double operator() (const TreeType &tree, ...) {....}
    > > > };

    >
    > > > The number of function objects like this will grow in the future. Now
    > > > I want a wrapper class that can hold any of these function objects and
    > > > pass them to other functions.

    >
    > > > So I want to complete the following class

    >
    > > > template<typename TreeType>
    > > > class FunctObj
    > > > {
    > > > };

    >
    > > > so that I can do the following.

    >
    > > > Eval1<HierarchyTree> funct1;
    > > > Eval2<EnhancedHierarchyTree> funct2;

    >
    > > > FunctObj FO; // object of the class I am trying to
    > > > write.

    >
    > > > Now I want to be able to assign either funct1 or funct2 to FO and pass
    > > > FO to a function.
    > > > How do I write the class FunctObj so that I can do this?

    >
    > > > The code fragment would look like the following.

    >
    > > > switch( user_option )
    > > > {
    > > > case 1:
    > > > FO = funct1;
    > > > break;

    >
    > > > case 2:
    > > > FO = funct2;
    > > > break;

    >
    > > > case 3:
    > > > //etc.
    > > > }

    >
    > > make FuncObj the abstract base to all other templates and use a
    > > pointer inside the switch block instead of an object.

    >
    > > regards,
    > > FM.

    >
    > Thanks.
    >
    > Since all the templated function objects have two arguments I was able
    > to get it to work using the STL.
    >
    > #include <functional>
    >
    > For the function I want to pass other functions into, I changed its
    > signature to
    >
    > template<class BinaryFunc>
    > void MyFunct( BinaryFunc F, .... )
    > {
    > ...
    > = F( ... );
    > ...
    > }
    >


    in the absence of the switch statement this works fine you can use
    elseif construrct:

    if (user_opt = opt1)
    MyFunct(.....);
    else if (user_opt = opt2)
    MyFunct(.....);
    else if (user_opt = opt3)
    MyFunct(.....);
    ..
    ..
    ..
    else
    MyFunct(.....);

    regards,
    FM
    terminator, Nov 24, 2007
    #6
    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. Brendan Duffy
    Replies:
    0
    Views:
    3,986
    Brendan Duffy
    Jul 25, 2003
  2. Julian
    Replies:
    1
    Views:
    692
    Roland
    May 24, 2005
  3. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  4. est
    Replies:
    1
    Views:
    590
    Diez B. Roggisch
    Feb 16, 2008
  5. Chris Bordeman
    Replies:
    8
    Views:
    484
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
Loading...

Share This Page