joe*_*ekr 7 c++ reflection binding parsing function
我正在看以下问题:
我得到格式如下的字符串:
functionname_parameter1_parameter2_parameter3
otherfunctionname_parameter1_parameter2
.
.
.
Run Code Online (Sandbox Code Playgroud)
我想用给定的参数调用函数.所以假设我有一个功能测试:
void test(int x, float y, std::string z) {}
Run Code Online (Sandbox Code Playgroud)
我收到一条消息:
test_5_2.0_abc
Run Code Online (Sandbox Code Playgroud)
那么我想像这样自动调用函数测试:
test(5, 2.0, "abc");
Run Code Online (Sandbox Code Playgroud)
您是否有任何关于如何在C++中实现此目的的提示?
Xeo*_*Xeo 20
更新:已更新stream_function以修复评论中提到的参数评估订单问题@Nawaz,并且还删除了std::function以提高效率.请注意,评估顺序修复仅适用于Clang,因为GCC不遵循此处的标准.可以在此处找到GCC的示例,其中包含手动订单执行.
这通常不容易实现.我写了一个小包装类,std::function它从一个提取参数std::istream.这是使用C++ 11的示例:
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <functional>
#include <stdexcept>
#include <type_traits>
// for proper evaluation of the stream extraction to the arguments
template<class R>
struct invoker{
R result;
template<class F, class... Args>
invoker(F&& f, Args&&... args)
: result(f(std::forward<Args>(args)...)) {}
};
template<>
struct invoker<void>{
template<class F, class... Args>
invoker(F&& f, Args&&... args)
{ f(std::forward<Args>(args)...); }
};
template<class F, class Sig>
struct stream_function_;
template<class F, class R, class... Args>
struct stream_function_<F, R(Args...)>{
stream_function_(F f)
: _f(f) {}
void operator()(std::istream& args, std::string* out_opt) const{
call(args, out_opt, std::is_void<R>());
}
private:
template<class T>
static T get(std::istream& args){
T t; // must be default constructible
if(!(args >> t)){
args.clear();
throw std::invalid_argument("invalid argument to stream_function");
}
return t;
}
// void return
void call(std::istream& args, std::string*, std::true_type) const{
invoker<void>{_f, get<Args>(args)...};
}
// non-void return
void call(std::istream& args, std::string* out_opt, std::false_type) const{
if(!out_opt) // no return wanted, redirect
return call(args, nullptr, std::true_type());
std::stringstream conv;
if(!(conv << invoker<R>{_f, get<Args>(args)...}.result))
throw std::runtime_error("bad return in stream_function");
*out_opt = conv.str();
}
F _f;
};
template<class Sig, class F>
stream_function_<F, Sig> stream_function(F f){ return {f}; }
typedef std::function<void(std::istream&, std::string*)> func_type;
typedef std::map<std::string, func_type> dict_type;
void print(){
std::cout << "print()\n";
}
int add(int a, int b){
return a + b;
}
int sub(int a, int b){
return a - b;
}
int main(){
dict_type func_dict;
func_dict["print"] = stream_function<void()>(print);
func_dict["add"] = stream_function<int(int,int)>(add);
func_dict["sub"] = stream_function<int(int,int)>(sub);
for(;;){
std::cout << "Which function should be called?\n";
std::string tmp;
std::cin >> tmp;
auto it = func_dict.find(tmp);
if(it == func_dict.end()){
std::cout << "Invalid function '" << tmp << "'\n";
continue;
}
tmp.clear();
try{
it->second(std::cin, &tmp);
}catch(std::exception const& e){
std::cout << "Error: '" << e.what() << "'\n";
std::cin.ignore();
continue;
}
std::cout << "Result: " << (tmp.empty()? "none" : tmp) << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
在Clang 3.3下编译并按预期工作(小实例).
Which function should be called?
a
Invalid function 'a'
Which function should be called?
add
2
d
Error: 'invalid argument to stream_function'
Which function should be called?
add
2
3
Result: 5
Which function should be called?
add 2 6
Result: 8
Which function should be called?
add 2
6
Result: 8
Which function should be called?
sub 8 2
Result: 6
Run Code Online (Sandbox Code Playgroud)
再次将这个课程混在一起很有趣,希望你喜欢.请注意,您需要稍微修改代码以适用于您的示例,因为C++ IOstreams将空格作为分隔符,因此您需要使用空格替换消息中的所有下划线.应该很容易做到,之后只需std::istringstream从你的消息构建一个:
std::istringstream input(message_without_underscores);
// call and pass 'input'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4466 次 |
| 最近记录: |