2012-10-10 29 views
2

首先,我會說我不是最有能力的C++程序員,但我正在學習,並享受Thrift的力量。處理Apache Thrift列表/ map C++中的返回類型

我已經實現了Thrift服務,並帶有一些返回void,i32和list的基本函數。我正在使用由Django Web應用程序控制的Python客戶端進行RPC調用,並且它工作得很好。生成的代碼是非常簡單的,除了列表回報:

.thrift描述文件:

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簽名:

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描述中列出的字符串向量。

我想知道如果我的意圖是提供一個字符串向量修改的功能,然後Thrift將處理返回給我的客戶端,儘管函數返回void。

我可以找到絕對沒有資源或示例使用的節儉列表<> C++類型。任何指導將不勝感激。

回答

3

好的,我已經想通了。這真的很簡單。

void ListAllFlows(std::vector<std::string> & _return) 
{ 
    for(int x = 0; x < 5; x++) 
    { 
     _return.push_back(std::string("hi")); 
    } 
} 

然後Python的客戶端,因爲它看起來在.thrift文件只是將其稱爲:

result = client.ListAllFlows() 
print result # ['hi', 'hi', 'hi', 'hi', 'hi'] 
0

返回一個容器類直接工作,但我更詳細over here概述了一些缺點。