Using boost :: bind with boost function: getting a bound variable of type

Is there a way to get information about which parameters were limited by boost :: bind or should this be saved manually?

i.e:.

in .h

class MyClass
{
    void foo(int a);
    void foo2(double b);
    void bar();
    void execute();
    int _myint;
    double _mydouble;
}

in .cpp

MyClass::bar()
{
    vector<boost::function<void(void)> myVector;
    myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
    myVector.push_back(boost::bind(&MyClass::foo2, this, MyClass::_mydouble);
}
MyClass::execute(char* param)
{

    boost::function<void(void)> f  = myVector[0];
    //MAGIC goes here
    //somehow know that an int parameter was bound
    _myint = atoi(param);
    //--------------------------------------
    f();
}
+4
source share
4 answers

As far as I know, you cannot do what you want, because it std::functionthrows out (almost) all information about the type of the passed function pointer, function object, or lambda. It uses a method called the erasure type , and on the surface completely forgets what was passed into it if it is allowed with the arguments provided. So, after binding, you are out of luck, it seems.

However , you can provide this information yourself:

#include <functional>
#include <sstream>
#include <string>
#include <vector>

struct call_info{
  std::function<void(void)> func;
  std::string arg_type;
};

class MyClass{
  std::vector<call_info> _myVec;
  int _myInt;
  double _myDouble;

public:
  void foo1(int i);
  void foo2(double d);
  void bar();
  void execute(char* param);
};

void MyClass::bar(){
  call_info info;
  info.func = std::bind(&MyClass::foo1, this, &MyClass::_myInt);
  info.arg_type = "int";
  _myVector.push_back(info);
  info.func = std::bind(&MyClass::foo2, this, &MyClass::_myDouble);
  info.arg_type = "double";
  _myVector.push_back(info);
}

void MyClass::execute(char* param){
  call_info& info = _myVector[0];
  std::stringstream conv(param);

  if(info.arg_type == "int")
    conv >> _myInt;
  else if(info.arg_type == "double")
    conv >> _myDouble;
  info.func();
}

, , , ( , , ).

+2

, , Boost Spirit:

,

: /exec <functionName> <param1> <param2>

, , , :

struct Echo
{
    void WriteLine(const std::string& s) { std::cout << "WriteLine('"     << s << "');" << std::endl; }
    void WriteStr (const std::string& s) { std::cout << "Write(string: '" << s << "');" << std::endl; }
    void WriteInt (int i)                { std::cout << "Write(int: "     << i <<  ");" << std::endl; }
    void WriteDbl (double d)             { std::cout << "Write(double: "  << d <<  ");" << std::endl; }
    void NewLine  ()                     { std::cout << "NewLine();"                    << std::endl; }
} echoService;

struct Admin
{
    void Shutdown(const std::string& reason, int retval) 
    { 
        std::cout << "Shutdown(reason: '" << reason << "', retval: " << retval << ")" << std::endl;
        // exit(retval);
    }
} adminService;

(TL; DR? skip this)

, ""?

. , . , . . (, , () , , : ).

. Lex/yacc, Coco/R ++, ANTLR . Boost Spirit Qi.

, , , , :

parser = "/execute" > (
        (lit("WriteLine") > stringlit)
      | (lit("Write")    >> +(double_ | int_ | stringlit))
      | lit("NewLine")
      | (lit("Shutdown")  > (stringlit > -int_))

// stringlit is just a quoted string:    
stringlit = lexeme[ '"' >> *~char_('"') >> '"' ];

. , /execute Write

, echoService adminService, REPL :

void execute(const std::string& command)
{
    typedef std::string::const_iterator It;
    It f(command.begin()), l(command.end());

    if (!phrase_parse(f,l, "/execute" > (
            (lit("WriteLine")  
                > stringlit  [ bind(&Echo::WriteLine, ref(echoService), _1) ])
          | (lit("Write") >> +(
                  double_    [ bind(&Echo::WriteDbl,  ref(echoService), _1) ]
                | int_       [ bind(&Echo::WriteInt,  ref(echoService), _1) ]
                | stringlit  [ bind(&Echo::WriteStr,  ref(echoService), _1) ]
            ))
          | (lit("NewLine")  [ bind(&Echo::NewLine,   ref(echoService)) ])
          | (lit("Shutdown")  > (stringlit > (int_ | attr(0))) 
                             [ bind(&Admin::Shutdown, ref(adminService), _1, _2) ])
        ), space))
    {
        // handle error, see full code below
    }
}

, , :

int main()
{
    std::string command;
    while (std::getline(std::cin, command))
        execute(command);
}

, ?


github 1: https://gist.github.com/1314900

  • / ​​
  • std:: cin ( )
  • ( , )

, , Boost. g++ 4.6.1 ( ) Boost 1.47. (input.txt):

/execute WriteLine "bogus"
/execute Write     "here comes the answer: "
/execute Write     42
/execute Write     31415e-4
/execute Write     "that is the inverse of" 24 "and answers nothing"
/execute Shutdown  "Bye" 9
/execute Shutdown  "Test default value for retval"

WriteLine('bogus');
Write(string: 'here comes the answer: ');
Write(double: 42);
Write(double: 3.1415);
Write(string: 'that is the inverse of');
Write(double: 24);
Write(string: 'and answers nothing');
Shutdown(reason: 'Bye', retval: 9)
Shutdown(reason: 'Test default value for retval', retval: 0)
  • . exit(...), , retval (attr(0))
  • : 31415e-4 3.1415
  • : /execute Write echoService.Write(...)
  • : ( ). /

< > 1 , github :

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <fstream>

namespace qi  = boost::spirit::qi;
namespace phx = boost::phoenix;

///////////////////////////////////
// 'domain classes' (scriptables)
struct Echo
{
    void WriteLine(const std::string& s) { std::cout << "WriteLine('"     << s << "');" << std::endl; }
    void WriteStr (const std::string& s) { std::cout << "Write(string: '" << s << "');" << std::endl; }
    void WriteInt (int i)                { std::cout << "Write(int: "     << i <<  ");" << std::endl; }
    void WriteDbl (double d)             { std::cout << "Write(double: "  << d <<  ");" << std::endl; }
    void NewLine  ()                     { std::cout << "NewLine();"                    << std::endl; }
} echoService;

struct Admin
{
    void Shutdown(const std::string& reason, int retval) 
    { 
        std::cout << "Shutdown(reason: '" << reason << "', retval: " << retval << ")" << std::endl;
        // exit(retval);
    }
} adminService;

void execute(const std::string& command)
{
    typedef std::string::const_iterator It;
    It f(command.begin()), l(command.end());

    using namespace qi;
    using phx::bind;
    using phx::ref;

    rule<It, std::string(), space_type> stringlit = lexeme[ '"' >> *~char_('"') >> '"' ];

    try
    {
        if (!phrase_parse(f,l, "/execute" > (
                (lit("WriteLine")  
                    > stringlit  [ bind(&Echo::WriteLine, ref(echoService), _1) ])
              | (lit("Write") >> +(
                      double_    [ bind(&Echo::WriteDbl,  ref(echoService), _1) ] // the order matters
                    | int_       [ bind(&Echo::WriteInt,  ref(echoService), _1) ]
                    | stringlit  [ bind(&Echo::WriteStr,  ref(echoService), _1) ]
                ))
              | (lit("NewLine")  [ bind(&Echo::NewLine,   ref(echoService)) ])
              | (lit("Shutdown")  > (stringlit > (int_ | attr(0))) 
                                 [ bind(&Admin::Shutdown, ref(adminService), _1, _2) ])
            ), space))
        {
            if (f!=l) // allow whitespace only lines
                std::cerr << "** (error interpreting command: " << command << ")" << std::endl;
        }
    }
    catch (const expectation_failure<It>& e)
    {
        std::cerr << "** (unexpected input '" << std::string(e.first, std::min(e.first+10, e.last)) << "') " << std::endl;
    }

    if (f!=l)
        std::cerr << "** (warning: skipping unhandled input '" << std::string(f,l) << "')" << std::endl;
}

int main()
{
    std::ifstream ifs("input.txt");

    std::string command;
    while (std::getline(ifs/*std::cin*/, command))
        execute(command);
}

>

+6

boost::function typedef, .

template<typename Signature>  // Function type R (T1, T2, ..., TN)
class function : public functionN<R, T1, T2, ..., TN> {
public:
  // types
  typedef R  result_type;         
  typedef T1 argument_type;         // If N == 1
  typedef T1 first_argument_type;   // If N == 2
  typedef T2 second_argument_type;  // If N == 2
  typedef T1 arg1_type;           
  typedef T2 arg2_type;  

  // ...

  // static constants
  static const int arity = N;

boost:: function.

0

, , , . , (, , ), . :

, :

vector<boost::function<void(char*)> myParseVector;
myParseVector.push_back(boost::bind(&MyClass::parseInt, this, MyClass::_myint);
myParseVector.push_back(boost::bind(&MyClass::parseDouble, this, MyClass::_mydouble);

.. , , .

Or ... use type erase yourself with a user interface that supports parseand execute!

0
source

All Articles