处理Apache Thrift列表/ map C ++中的返回类型 [英] Handling Apache Thrift list/map Return Types in C++
问题描述
首先,我会说我不是最有能力的C ++程序员,但我正在学习,并享受Thrift的力量。
First off, I'll say I'm not the most competent C++ programmer, but I'm learning, and enjoying the power of Thrift.
我实现了一个Thrift服务,它包含一些返回void,i32和list的基本函数。我使用一个由Django Web应用程序控制的Python客户端来进行RPC调用,并且它工作得很好。生成的代码非常简单,除了列表返回:
I've implemented a Thrift Service with some basic functions that return void, i32, and list. I'm using a Python client controlled by a Django web app to make RPC calls and it works pretty well. The generated code is pretty straight forward, except for list returns:
.thrift描述文件:
.thrift description file:
namespace cpp Remote
enum N_PROTO {
N_TCP,
N_UDP,
N_ANY
}
service Rcon {
i32 ping()
i32 KillFlows()
i32 RestartDispatch()
i32 PrintActiveFlows()
i32 PrintActiveListeners(1:i32 proto)
list<string> ListAllFlows()
}
从Rcon.h生成的签名:
The generated signatures from Rcon.h:
int32_t ping();
int32_t KillFlows();
int32_t RestartDispatch();
int32_t PrintActiveFlows();
int32_t PrintActiveListeners(const int32_t proto);
int64_t ListenerBytesReceived(const int32_t id);
void ListAllFlows(std::vector<std::string> & _return);
正如你所看到的,ListAllFlows()函数生成一个字符串向量。我想我希望它返回一个向量的字符串,如.thrift描述中所述。
As you see, the ListAllFlows() function generated takes a reference to a vector of strings. I guess I expect it to return a vector of strings as laid out in the .thrift description.
我想知道如果我的意思是提供一个向量字符串修改然后Thrift将处理返回它到我的客户端尽管函数返回void。
I'm wondering if I am meant to provide the function a vector of strings to modify and then Thrift will handle returning it to my client despite the function returning void.
我可以找到绝对没有资源或示例用法Thrift列表< C ++。
I can find absolutely no resources or example usages of Thrift list<> types in C++. Any guidance would be appreciated.
推荐答案
好的,我想出来了。这很简单。
Ok, I've figured it out. It's pretty simple really.
void ListAllFlows(std::vector<std::string> & _return)
{
for(int x = 0; x < 5; x++)
{
_return.push_back(std::string("hi"));
}
}
然后Python客户端只是调用它.thrift文件:
Then the Python client just calls it as it looks in the .thrift file:
result = client.ListAllFlows()
print result # ['hi', 'hi', 'hi', 'hi', 'hi']
这篇关于处理Apache Thrift列表/ map C ++中的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!