C++ 23 -> C++ 20; std::expected -> Expected (#3)
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(redis_playground LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
|
||||||
|
|
||||||
project(redis_playground LANGUAGES CXX)
|
|
||||||
|
|
||||||
find_package(hiredis CONFIG REQUIRED)
|
find_package(hiredis CONFIG REQUIRED)
|
||||||
|
|
||||||
add_library(proj_warnings INTERFACE)
|
add_library(proj_warnings INTERFACE)
|
||||||
target_compile_options(proj_warnings INTERFACE -Wall -Werror -Wextra -Wpedantic)
|
target_compile_options(proj_warnings INTERFACE -Wall -Werror -Wextra -Wpedantic)
|
||||||
|
|
||||||
|
add_library(expected INTERFACE src/expected.h)
|
||||||
|
target_link_libraries(expected INTERFACE proj_warnings)
|
||||||
|
|
||||||
add_executable(main ./src/main.cc)
|
add_executable(main ./src/main.cc)
|
||||||
target_link_libraries(main PRIVATE proj_warnings hiredis::hiredis)
|
target_link_libraries(main PRIVATE proj_warnings expected hiredis::hiredis)
|
||||||
|
|||||||
18
src/main.cc
18
src/main.cc
@@ -1,6 +1,5 @@
|
|||||||
#include <hiredis/hiredis.h>
|
#include <hiredis/hiredis.h>
|
||||||
|
|
||||||
#include <expected>
|
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -12,6 +11,8 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "expected.h"
|
||||||
|
|
||||||
struct FieldVal {
|
struct FieldVal {
|
||||||
std::string field, val;
|
std::string field, val;
|
||||||
};
|
};
|
||||||
@@ -73,7 +74,7 @@ class RedisClient {
|
|||||||
if (ctx_) redisFree(ctx_);
|
if (ctx_) redisFree(ctx_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<void, std::string> xadd(const XaddReq& req) noexcept {
|
Expected<void> xadd(const XaddReq& req) noexcept {
|
||||||
// todo: safe guard against invalid requests
|
// todo: safe guard against invalid requests
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -84,15 +85,15 @@ class RedisClient {
|
|||||||
auto reply = submit_cmd(ss.str());
|
auto reply = submit_cmd(ss.str());
|
||||||
|
|
||||||
if (!reply) {
|
if (!reply) {
|
||||||
return std::unexpected(ctx_->errstr);
|
return Unexpected(ctx_->errstr);
|
||||||
} else if (reply->type == REDIS_REPLY_ERROR) {
|
} else if (reply->type == REDIS_REPLY_ERROR) {
|
||||||
return std::unexpected(reply->str ? reply->str : "unknown error");
|
return Unexpected(reply->str ? reply->str : "unknown error");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<XreadRes, std::string> xread(const XreadReq& req) noexcept {
|
Expected<XreadRes> xread(const XreadReq& req) noexcept {
|
||||||
// todo: safe guard against invalid requests
|
// todo: safe guard against invalid requests
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -105,14 +106,13 @@ class RedisClient {
|
|||||||
auto reply = submit_cmd(ss.str());
|
auto reply = submit_cmd(ss.str());
|
||||||
|
|
||||||
if (!reply) {
|
if (!reply) {
|
||||||
return std::unexpected(ctx_->errstr);
|
return Unexpected(ctx_->errstr);
|
||||||
} else if (reply->type == REDIS_REPLY_ERROR) {
|
} else if (reply->type == REDIS_REPLY_ERROR) {
|
||||||
return std::unexpected(reply->str ? reply->str : "unknown reply error");
|
return Unexpected(reply->str ? reply->str : "unknown reply error");
|
||||||
} else if (reply->type == REDIS_REPLY_NIL) {
|
} else if (reply->type == REDIS_REPLY_NIL) {
|
||||||
return {};
|
return {};
|
||||||
} else if (reply->type != REDIS_REPLY_ARRAY) {
|
} else if (reply->type != REDIS_REPLY_ARRAY) {
|
||||||
return std::unexpected(
|
return Unexpected(std::format("unexpected reply type {}", reply->type));
|
||||||
std::format("unexpected reply type {}", reply->type));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
XreadRes res;
|
XreadRes res;
|
||||||
|
|||||||
Reference in New Issue
Block a user