
Maxim Yegorushkin <maxim.yegorushkin@gmail.com> writes:
Can it be done in a straightforward way?
#include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/lexical_cast.hpp> #include <string> #include <iostream>
struct caster { std::string s; template<class T> operator T() const { return boost::lexical_cast<T>(s); } caster(std::string const& s) : s(s) {} caster(char const* s) : s(s) {} };
void f(int i, double d) { std::cout << i << char(0xa) << d << char(0xa); // sorry, my back-slash key does not work }
int main() { boost::function<void(caster, caster)> g(f); g("123", "3.13"); }
That's pretty clever. So simple--I like it! (I'll try this out and see how it goes.)
My motivation for wanting this is to implement a "simple" command interpreter for a socket-based administrative interface. I pre-register a bunch of function objects, then whenever someone sends a command down the socket, it arrives as a string. The function object is looked up by name, and then the call-operator is invoked given the string arguments. Internally, the proper string-to-type converstions occur, and the boost::function is called with those values. (If the conversion fails, an exception would be perfectly reasonable.)
Don't you have to tokenize the input first? While doing so you might parse it as well.
Actually, the data would be arriving in a CORBA sequence of strings, which is similar to a vector. Thus, the args would already be "tokenized". Thanks again for the idea! -- Chris