Return raw pointer as ClientJson response.

GitOrigin-RevId: 6b309681539b3ee9b2c2e87c57ed4cc8f94d798f
This commit is contained in:
levlam 2020-10-05 15:48:37 +03:00
parent 3fc140b4a2
commit 119fc9563a
4 changed files with 10 additions and 20 deletions

View File

@ -67,10 +67,10 @@ static std::string from_response(const td_api::Object &object, const string &ext
static TD_THREAD_LOCAL std::string *current_output;
static CSlice store_string(std::string str) {
static const char *store_string(std::string str) {
init_thread_local<std::string>(current_output);
*current_output = std::move(str);
return *current_output;
return current_output->c_str();
}
void ClientJson::send(Slice request) {
@ -83,10 +83,10 @@ void ClientJson::send(Slice request) {
client_.send(Client::Request{extra_id, std::move(parsed_request.first)});
}
CSlice ClientJson::receive(double timeout) {
const char *ClientJson::receive(double timeout) {
auto response = client_.receive(timeout);
if (!response.object) {
return {};
return nullptr;
}
std::string extra;
@ -101,7 +101,7 @@ CSlice ClientJson::receive(double timeout) {
return store_string(from_response(*response.object, extra));
}
CSlice ClientJson::execute(Slice request) {
const char *ClientJson::execute(Slice request) {
auto parsed_request = to_request(request);
return store_string(from_response(*Client::execute(Client::Request{0, std::move(parsed_request.first)}).object,
parsed_request.second));

View File

@ -22,9 +22,9 @@ class ClientJson final {
public:
void send(Slice request);
CSlice receive(double timeout);
const char *receive(double timeout);
static CSlice execute(Slice request);
static const char *execute(Slice request);
private:
Client client_;

View File

@ -27,19 +27,9 @@ void td_json_client_send(void *client, const char *request) {
}
const char *td_json_client_receive(void *client, double timeout) {
auto slice = static_cast<td::ClientJson *>(client)->receive(timeout);
if (slice.empty()) {
return nullptr;
} else {
return slice.c_str();
}
return static_cast<td::ClientJson *>(client)->receive(timeout);
}
const char *td_json_client_execute(void *client, const char *request) {
auto slice = td::ClientJson::execute(td::Slice(request == nullptr ? "" : request));
if (slice.empty()) {
return nullptr;
} else {
return slice.c_str();
}
return td::ClientJson::execute(td::Slice(request == nullptr ? "" : request));
}

View File

@ -84,7 +84,7 @@ TDJSON_EXPORT const char *td_json_client_receive(void *client, double timeout);
* in the same thread, so it can't be used after that.
* \param[in] client The client. Currently ignored for all requests, so NULL can be passed.
* \param[in] request JSON-serialized null-terminated request to TDLib.
* \return JSON-serialized null-terminated request response. May be NULL if the request can't be parsed.
* \return JSON-serialized null-terminated request response.
*/
TDJSON_EXPORT const char *td_json_client_execute(void *client, const char *request);