Mapped function pointer with different parameters

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

    Mapped function pointer with different parameters

    consider these functions:
    void foo()
    {
    cout << "foo() called;
    }
    void sayhello( std::string& msg ){
    msg = "something" ;
    }
    int add( int x, int y)
    {
    return x + y;
    }

    typedef void (*fooFunc)();
    typedef std::map<std::s tring, fooFunc> Processing_Map;

    ...
    Processing_Map table;
    table["foo"] = foo;
    table["sayhello"] = ???
    table["add"]= ???

    how to map table["sayhello"] to sayhello function, considering it has
    a parameter
    also the "add"

    and how to actually pass a parameter value to those mapped functions

    --leaf

  • Ben Pope

    #2
    Re: Mapped function pointer with different parameters

    leaf wrote:[color=blue]
    > consider these functions:
    > void foo()
    > {
    > cout << "foo() called;
    > }
    > void sayhello( std::string& msg ){
    > msg = "something" ;
    > }
    > int add( int x, int y)
    > {
    > return x + y;
    > }
    >
    > typedef void (*fooFunc)();
    > typedef std::map<std::s tring, fooFunc> Processing_Map;
    >
    > ..
    > Processing_Map table;
    > table["foo"] = foo;
    > table["sayhello"] = ???
    > table["add"]= ???
    >
    > how to map table["sayhello"] to sayhello function, considering it has
    > a parameter
    > also the "add"
    >
    > and how to actually pass a parameter value to those mapped functions[/color]

    function pointers are typed. You can't.

    One way of doing it would be to wrap foo:
    foo(std::string & msg) {
    foo();
    }

    change your typedef:
    typdef void (*fooFunc)(std: :string&);

    That only provides you with limited ability to call heterogeneous
    function prototypes.

    A typical "solution" is:
    typedef void* (*fooFunc)(void *, void*);

    Which allows you to return and accept arbitrary data by completely
    removing type safety.

    The difficulty is in keeping track of what to pass to which function.
    If you know at the call site which type of data to pass, then just call
    the correct function (or at least a function through a typesafe function
    pointer).

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...

    Comment

    • leaf

      #3
      Re: Mapped function pointer with different parameters

      Ben,
      I use a struct to store function information.
      struct TFunction{
      int id; // function to call
      ULONG* params; // function parameters
      unsigned int nprams; // number of function parameters, ( just to
      keep track )
      }

      The TFuncion is the stuct containing information about which function
      to call and what and how many parameters to pass.
      This stuct is filled from the others side of the Application.

      ---
      leaf

      Comment

      • Ben Pope

        #4
        Re: Mapped function pointer with different parameters

        leaf wrote:[color=blue]
        > Ben,
        > I use a struct to store function information.
        > struct TFunction{
        > int id; // function to call
        > ULONG* params; // function parameters
        > unsigned int nprams; // number of function parameters, ( just to
        > keep track )
        > }
        >
        > The TFuncion is the stuct containing information about which function
        > to call and what and how many parameters to pass.
        > This stuct is filled from the others side of the Application.[/color]

        You'd probably be much better off with a type safe implementation.

        If you explain at a higher level what it is you're trying to achieve (as
        opposed to how you want to achieve it), then a better solution may be
        presented.

        Check out the command pattern, to see if it better suits your needs.

        Ben Pope
        --
        I'm not just a number. To many, I'm known as a string...

        Comment

        • leaf

          #5
          Re: Mapped function pointer with different parameters

          At this moment any implementation would do, just a while ago i read
          articles about command pattern specifically, Generalized functors.
          somehow i did understand how to use it, but i can't figure out the
          final resolution.

          This is the overall implementation of my program:
          =============== =============== =======
          All the functions are imported from a DLL ( much like a functions
          library )

          Based on TFunction structure, say passed to the function :
          Functionizer( TFunction fn );

          this 'Functionizer' call the right function as well as pass the
          parameters stored in fn.

          -----
          leaf

          Comment

          • Michiel.Salters@tomtom.com

            #6
            Re: Mapped function pointer with different parameters

            leaf wrote:[color=blue]
            > consider these functions:
            > void foo();
            > void sayhello( std::string& msg );
            > int add( int x, int y);
            >
            > typedef void (*fooFunc)();
            > typedef std::map<std::s tring, fooFunc> Processing_Map;
            > ..
            > Processing_Map table;
            > table["foo"] = foo;
            > table["sayhello"] = ???
            > table["add"]= ???[/color]
            [color=blue]
            > and how to actually pass a parameter value to those mapped functions[/color]

            "A parameter"? How many parameters do you pass to table[X] ? You can't
            tell at compile time, so the compiler forbids it.

            Typically you'd use something like this in a script language engine. In
            that
            case, you define a ArgumentList class. All functions take that, but
            foo(ArgumentLis t al) complains if (al.size() > 0)

            HTH,
            Michiel Salters

            Comment

            Working...