remove string_view.h

This commit is contained in:
qicosmos
2025-09-07 14:06:06 +08:00
parent 8f2c0a4fa0
commit 0737fefead
10 changed files with 53 additions and 1465 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.5)
project(rest_rpc)
include_directories(include)
+16 -15
View File
@@ -374,7 +374,7 @@ void test_callback() {
// set timeout 100ms
client.async_call<10000>(
"delay_echo",
[](const asio::error_code &ec, string_view data) {
[](const asio::error_code &ec, std::string_view data) {
if (ec) {
std::cout << ec.value() << " timeout" << std::endl;
return;
@@ -389,7 +389,7 @@ void test_callback() {
// zero means no timeout check, no param means using default timeout(5s)
client.async_call<0>(
"echo",
[](const asio::error_code &ec, string_view data) {
[](const asio::error_code &ec, std::string_view data) {
auto str = as<std::string>(data);
std::cout << "echo " << str << '\n';
},
@@ -400,13 +400,13 @@ void test_callback() {
}
void wait_for_notification(rpc_client &client) {
client.async_call<0>("sub",
[&client](const asio::error_code &ec, string_view data) {
auto str = as<std::string>(data);
std::cout << str << '\n';
client.async_call<0>(
"sub", [&client](const asio::error_code &ec, std::string_view data) {
auto str = as<std::string>(data);
std::cout << str << '\n';
wait_for_notification(client);
});
wait_for_notification(client);
});
}
void test_sub1() {
@@ -418,11 +418,12 @@ void test_sub1() {
return;
}
client.subscribe("key", [](string_view data) { std::cout << data << "\n"; });
client.subscribe("key",
[](std::string_view data) { std::cout << data << "\n"; });
client.subscribe(
"key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) {
[](std::string_view data) {
msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size());
std::cout << p.name << "\n";
@@ -431,7 +432,7 @@ void test_sub1() {
client.subscribe(
"key1",
"048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) { std::cout << data << "\n"; });
[](std::string_view data) { std::cout << data << "\n"; });
bool stop = false;
std::thread thd1([&client, &stop] {
@@ -494,7 +495,7 @@ void test_multiple_thread() {
for (size_t i = 0; i < 1000000; i++) {
client->async_call<0>(
"get_name",
[](const asio::error_code &ec, string_view data) {
[](const asio::error_code &ec, std::string_view data) {
if (ec) {
std::cout << ec.message() << '\n';
}
@@ -548,7 +549,7 @@ void test_threads() {
for (size_t i = 1000000; i < 2 * 1000000; i++) {
client.async_call(
"get_int",
[i](asio::error_code ec, string_view data) {
[i](asio::error_code ec, std::string_view data) {
if (ec) {
std::cout << ec.message() << '\n';
return;
@@ -618,7 +619,7 @@ void test_ssl() {
client.async_call(
"echo",
[](asio::error_code ec, string_view data) {
[](asio::error_code ec, std::string_view data) {
if (ec) {
std::cout << ec.message() << " " << data << "\n";
return;
@@ -643,7 +644,7 @@ void benchmark_test() {
for (size_t i = 0; i < 1000000; i++) {
client.async_call(
"echo",
[i](asio::error_code ec, string_view data) {
[i](asio::error_code ec, std::string_view data) {
if (ec) {
return;
}
+5 -11
View File
@@ -1,17 +1,11 @@
#pragma once
#include <tuple>
#if __cplusplus > 201402L
#include <string_view>
using string_view = std::string_view;
#else
#include "string_view.hpp"
using namespace nonstd;
#endif
#include <tuple>
#include "codec.h"
namespace rest_rpc {
inline bool has_error(string_view result) {
inline bool has_error(std::string_view result) {
if (result.empty()) {
return true;
}
@@ -22,20 +16,20 @@ inline bool has_error(string_view result) {
return std::get<0>(tp) != 0;
}
template <typename T> inline T get_result(string_view result) {
template <typename T> inline T get_result(std::string_view result) {
rpc_service::msgpack_codec codec;
auto tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size());
return std::get<1>(tp);
}
inline std::string get_error_msg(string_view result) {
inline std::string get_error_msg(std::string_view result) {
rpc_service::msgpack_codec codec;
auto tp =
codec.unpack<std::tuple<int, std::string>>(result.data(), result.size());
return std::get<1>(tp);
}
template <typename T> inline T as(string_view result) {
template <typename T> inline T as(std::string_view result) {
if (has_error(result)) {
throw std::logic_error(get_error_msg(result));
}
+1 -1
View File
@@ -209,7 +209,7 @@ private:
read_head();
if (req_type_ == request_type::req_res) {
route_result_t ret = router_.route<connection>(
func_id, nonstd::string_view{body_.data(), length},
func_id, std::string_view{body_.data(), length},
this->shared_from_this());
if (delay_) {
delay_ = false;
+5 -6
View File
@@ -5,10 +5,10 @@
#include "function_name.h"
#include "md5.hpp"
#include "meta_util.hpp"
#include "string_view.hpp"
#include "use_asio.hpp"
#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
namespace rest_rpc {
@@ -81,7 +81,7 @@ public:
}
template <typename T>
route_result_t route(uint32_t key, nonstd::string_view data,
route_result_t route(uint32_t key, std::string_view data,
std::weak_ptr<T> conn) {
route_result_t route_result{};
std::string result;
@@ -198,8 +198,7 @@ private:
template <bool is_pub, typename Function>
void register_nonmember_func(uint32_t key, Function f) {
this->map_invokers_[key] = [f](std::weak_ptr<connection> conn,
nonstd::string_view str,
std::string &result) {
std::string_view str, std::string &result) {
using args_tuple = typename function_traits<Function>::bare_tuple_type;
msgpack_codec codec;
try {
@@ -217,7 +216,7 @@ private:
template <bool is_pub, typename Function, typename Self>
void register_member_func(uint32_t key, const Function &f, Self *self) {
this->map_invokers_[key] = [f, self](std::weak_ptr<connection> conn,
nonstd::string_view str,
std::string_view str,
std::string &result) {
using args_tuple = typename function_traits<Function>::bare_tuple_type;
msgpack_codec codec;
@@ -235,7 +234,7 @@ private:
std::unordered_map<uint32_t,
std::function<void(std::weak_ptr<connection>,
nonstd::string_view, std::string &)>>
std::string_view, std::string &)>>
map_invokers_;
std::unordered_map<uint32_t, std::string> key2func_name_;
};
+10 -9
View File
@@ -26,7 +26,7 @@ enum class client_language_t {
class req_result {
public:
req_result() = default;
req_result(string_view data) : data_(data.data(), data.length()) {}
req_result(std::string_view data) : data_(data.data(), data.length()) {}
bool success() const { return !has_error(data_); }
template <typename T> T as() {
@@ -310,7 +310,7 @@ public:
template <size_t TIMEOUT = DEFAULT_TIMEOUT, typename... Args>
void async_call(std::string_view rpc_name,
std::function<void(asio::error_code, string_view)> cb,
std::function<void(asio::error_code, std::string_view)> cb,
Args &&...args) {
if (!has_connected_) {
if (cb)
@@ -623,7 +623,7 @@ private:
}
void call_back(uint64_t req_id, const asio::error_code &ec,
string_view data) {
std::string_view data) {
if (client_language_ == client_language_t::JAVA) {
// For Java client.
// TODO(qwang): Call java callback.
@@ -678,7 +678,7 @@ private:
}
}
void callback_sub(const asio::error_code &ec, string_view result) {
void callback_sub(const asio::error_code &ec, std::string_view result) {
rpc_service::msgpack_codec codec;
try {
auto tp = codec.unpack<std::tuple<int, std::string, std::string>>(
@@ -728,7 +728,7 @@ private:
public std::enable_shared_from_this<call_t> {
public:
call_t(asio::io_context &ios,
std::function<void(asio::error_code, string_view)> cb,
std::function<void(asio::error_code, std::string_view)> cb,
size_t timeout)
: timer_(ios), cb_(std::move(cb)), timeout_(timeout) {}
@@ -748,7 +748,7 @@ private:
});
}
void callback(asio::error_code ec, string_view data) { cb_(ec, data); }
void callback(asio::error_code ec, std::string_view data) { cb_(ec, data); }
bool has_timeout() const { return has_timeout_; }
@@ -763,7 +763,7 @@ private:
private:
asio::steady_timer timer_;
std::function<void(asio::error_code, string_view)> cb_;
std::function<void(asio::error_code, std::string_view)> cb_;
size_t timeout_;
bool has_timeout_ = false;
};
@@ -889,7 +889,7 @@ private:
struct client_message_type {
std::uint64_t req_id;
request_type req_type;
string_view content;
std::string_view content;
uint32_t func_id;
};
std::deque<client_message_type> outbox_;
@@ -912,7 +912,8 @@ private:
rpc_header header_;
std::unordered_map<std::string, std::function<void(string_view)>> sub_map_;
std::unordered_map<std::string, std::function<void(std::string_view)>>
sub_map_;
std::set<std::pair<std::string, std::string>> key_token_set_;
client_language_t client_language_ = client_language_t::CPP;
+4 -4
View File
@@ -61,8 +61,8 @@ public:
router_.register_handler<is_pub>(name, f, self);
}
void
set_error_callback(std::function<void(asio::error_code, string_view)> f) {
void set_error_callback(
std::function<void(asio::error_code, std::string_view)> f) {
err_cb_ = std::move(f);
}
@@ -174,7 +174,7 @@ private:
}
}
}
void error_callback(const asio::error_code &ec, string_view msg) {
void error_callback(const asio::error_code &ec, std::string_view msg) {
if (err_cb_) {
err_cb_(ec, msg);
}
@@ -269,7 +269,7 @@ private:
bool stop_check_ = false;
std::condition_variable cv_;
std::function<void(asio::error_code, string_view)> err_cb_;
std::function<void(asio::error_code, std::string_view)> err_cb_;
std::function<void(int64_t)> conn_timeout_callback_;
std::function<void(std::shared_ptr<connection>, std::string)>
on_net_err_callback_ = nullptr;
File diff suppressed because it is too large Load Diff
-6
View File
@@ -14,13 +14,7 @@ using tcp_socket = asio::ip::tcp::socket;
using ssl_socket = asio::ssl::stream<asio::ip::tcp::socket>;
#endif
#if __cplusplus > 201402L
#include <string_view>
using string_view = std::string_view;
#else
#include "string_view.hpp"
using namespace nonstd;
#endif
#ifdef CINATRA_ENABLE_SSL
#if __cplusplus > 201402L
+11 -11
View File
@@ -226,7 +226,7 @@ TEST_CASE("test_client_async_call") {
TEST_CASE("test_client_async_call_not_connect") {
rpc_client client("127.0.0.1", 9001);
client.async_call<>("get_person",
[](const asio::error_code &ec, string_view data) {
[](const asio::error_code &ec, std::string_view data) {
CHECK_EQ(ec, asio::error::not_connected);
CHECK_EQ(data, "not connected");
});
@@ -246,14 +246,14 @@ TEST_CASE("test_client_async_call_with_timeout") {
// zero means no timeout check, no param means using default timeout(5s)
client.async_call<0>(
"echo",
[](const asio::error_code &ec, string_view data) {
[](const asio::error_code &ec, std::string_view data) {
if (ec)
std::cout << "error code: " << ec << ", err msg: " << data << '\n';
},
test);
client.async_call<>(
"echo",
[&client](const asio::error_code &ec, string_view data) {
[&client](const asio::error_code &ec, std::string_view data) {
std::cout << "req id : " << client.reqest_id() << '\n';
if (ec)
std::cout << "error code: " << ec << ", err msg: " << data << '\n';
@@ -261,7 +261,7 @@ TEST_CASE("test_client_async_call_with_timeout") {
test);
client.async_call<>(
"get_person",
[&client](const asio::error_code &ec, string_view data) {
[&client](const asio::error_code &ec, std::string_view data) {
if (ec) {
std::cout << "error code: " << ec << ", err msg: " << data << '\n';
return;
@@ -306,7 +306,7 @@ TEST_CASE("test_client_subscribe") {
CHECK(r);
client.publish("key", "hello subscriber");
client.subscribe("key", [&stop](string_view data) {
client.subscribe("key", [&stop](std::string_view data) {
std::cout << data << "\n";
CHECK_EQ(data, "hello subscriber");
stop = true;
@@ -322,7 +322,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") {
server.publish(std::move(key), std::move(val));
});
bool stop = false;
server.set_error_callback([&stop](asio::error_code ec, string_view msg) {
server.set_error_callback([&stop](asio::error_code ec, std::string_view msg) {
std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- "
<< msg << std::endl;
CHECK_EQ(ec, asio::error::invalid_argument);
@@ -346,7 +346,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") {
std::cout << "line: " << __LINE__ << ", msg: " << ec.value() << " -- "
<< ec.message() << std::endl;
});
client.subscribe("key1", [&stop](string_view data) {
client.subscribe("key1", [&stop](std::string_view data) {
CHECK(data != "hello subscriber");
stop = true;
});
@@ -373,7 +373,7 @@ TEST_CASE("test_server_publish_encode_msg") {
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);
CHECK(r);
client.subscribe("person", [&stop](string_view data) {
client.subscribe("person", [&stop](std::string_view data) {
try {
msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size());
@@ -407,7 +407,7 @@ TEST_CASE("test_client_subscribe_by_token") {
CHECK(r);
client.subscribe(
"key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[&stop](string_view data) {
[&stop](std::string_view data) {
std::cout << data << "\n";
CHECK_EQ(data, "hello token subscriber");
stop = true;
@@ -441,7 +441,7 @@ TEST_CASE("test_client_publish_and_subscribe_by_token") {
bool r = client.connect("127.0.0.1", 9000);
CHECK(r);
client.publish_by_token("key", client_token, "hello token subscriber");
client.subscribe("key", client_token, [&stop](string_view data) {
client.subscribe("key", client_token, [&stop](std::string_view data) {
std::cout << data << "\n";
CHECK_EQ(data, "hello token subscriber");
stop = true;
@@ -499,7 +499,7 @@ TEST_CASE("test_server_duplicate_registration_key") {
try {
server.register_handler("delay_echo", delay_echo);
} catch (const std::exception &e) {
string_view ew{e.what()};
std::string_view ew{e.what()};
std::cerr << ew << '\n';
CHECK_EQ(ew, "duplicate registration key !");
}