2
0
mirror of https://github.com/boostorg/redis.git synced 2026-01-19 04:42:09 +00:00

Simplifications.

This commit is contained in:
Marcelo Zimbres
2022-01-04 02:28:20 +01:00
parent 5a91320a2d
commit 98ebd62d7c
4 changed files with 50 additions and 55 deletions

View File

@@ -4,7 +4,7 @@
<navindex>
<tab type="mainpage" visible="yes" title=""/>
<tab type="pages" visible="no" title="" intro=""/>
<tab type="modules" visible="yes" title="" intro=""/>
<tab type="modules" visible="yes" title="Reference" intro=""/>
<tab type="namespaces" visible="no" title="">
<tab type="namespacelist" visible="yes" title="" intro=""/>
<tab type="namespacemembers" visible="yes" title="" intro=""/>

View File

@@ -12,16 +12,15 @@
namespace aedis {
namespace resp3 {
/** \ingroup functions
* @{
*/
/** \brief Creates a void adapter
\ingroup functions
The adapter returned by this function ignores any data and is
useful to avoid wasting time with responses on which the user is
insterested in.
Example usage:
@code
co_await async_read(socket, buffer, adapt());
@endcode
@@ -31,10 +30,13 @@ auto adapt() noexcept
{ return response_traits<void>::adapt(); }
/** \brief Adapts user data to the resp3 parser.
\ingroup functions
For the types supported by this function see `response_traits`.
For example
Example usage:
@code
std::unordered_map<std::string, std::string> cont;
co_await async_read(socket, buffer, adapt(cont));
@@ -44,7 +46,5 @@ template<class T>
auto adapt(T& t) noexcept
{ return response_traits<T>::adapt(t); }
/*! @} */
} // resp3
} // aedis

View File

@@ -25,16 +25,7 @@
namespace aedis {
namespace resp3 {
namespace adapter {
namespace detail
{
struct adapter_ignore {
void
operator()(
type, std::size_t, std::size_t, char const*, std::size_t,
std::error_code&) { }
};
namespace detail {
template <class T>
typename std::enable_if<std::is_integral<T>::value, void>::type
@@ -73,17 +64,21 @@ void set_on_resp3_error(type t, std::error_code& ec)
}
}
/** A general pupose redis response class
A pre-order-view of the response tree.
*/
// Adapter that ignores responses.
struct ignore {
void
operator()(
type, std::size_t, std::size_t, char const*, std::size_t,
std::error_code&) { }
};
template <class Container>
class adapter_general {
class general {
private:
Container* result_;
public:
adapter_general(Container& c = nullptr): result_(&c) {}
general(Container& c = nullptr): result_(&c) {}
/** @brief Function called by the parser when new data has been processed.
*
@@ -104,17 +99,16 @@ public:
void
operator()(
type t,
std::size_t n,
std::size_t aggregate_size,
std::size_t depth,
char const* data,
std::size_t size,
std::error_code&)
{
result_->emplace_back(t, n, depth, std::string{data, size});
result_->emplace_back(t, aggregate_size, depth, std::string{data, size});
}
};
// Adapter for simple data types.
template <class Node>
class adapter_node {
private:
@@ -139,14 +133,14 @@ public:
}
};
// Adapter for simple data types.
// Adapter for RESP3 simple data types.
template <class T>
class adapter_simple {
class simple {
private:
T* result_;
public:
adapter_simple(T& t) : result_(&t) {}
simple(T& t) : result_(&t) {}
void
operator()(
@@ -176,12 +170,12 @@ public:
};
template <class T>
class adapter_optional_simple {
class simple_optional {
private:
std::optional<T>* result_;
public:
adapter_optional_simple(std::optional<T>& o) : result_(&o) {}
simple_optional(std::optional<T>& o) : result_(&o) {}
void
operator()(
@@ -216,16 +210,16 @@ public:
}
};
/* A response type that parses the response directly in a vector.
/* A std::vector adapter.
*/
template <class Container>
class adapter_vector {
class vector {
private:
int i_ = -1;
Container* result_;
public:
adapter_vector(Container& v) : result_{&v} {}
vector(Container& v) : result_{&v} {}
void
operator()(type t,
@@ -261,12 +255,12 @@ public:
};
template <class Container>
class adapter_list {
class list {
private:
Container* result_;
public:
adapter_list(Container& ref): result_(&ref) {}
list(Container& ref): result_(&ref) {}
void
operator()(type t,
@@ -299,13 +293,13 @@ public:
};
template <class Container>
class adapter_set {
class set {
private:
Container* result_;
Container::iterator hint_;
public:
adapter_set(Container& c)
set(Container& c)
: result_(&c)
, hint_(std::end(c))
{}
@@ -342,14 +336,14 @@ public:
};
template <class Container>
class adapter_map {
class map {
private:
Container* result_;
Container::iterator current_;
bool on_key_ = true;
public:
adapter_map(Container& c)
map(Container& c)
: result_(&c)
, current_(std::end(c))
{}

View File

@@ -21,12 +21,13 @@
namespace aedis {
namespace resp3 {
/** \brief Adapts C++ data structures to read operations.
/** \brief Adapts C++ built-in types to RESP3 read operations.
* \ingroup classes
*
* This class adapts C++ types to read operations. Users are advised
* to use `adapt()` function below for type deduction. The following
* types are supported.
* This class adapts C++ built-in types to RESP3 read operations.
* For type deduction see also `adapt()`.
*
* The following types are supported.
*
* 1. Integer data types e.g. `int`, `unsigned`, etc.
*
@@ -57,7 +58,7 @@ struct response_traits
using response_type = T;
/// The adapter type.
using adapter_type = adapter::detail::adapter_simple<response_type>;
using adapter_type = adapter::detail::simple<response_type>;
/// Returns an adapter for the reponse r
static auto adapt(response_type& r) noexcept { return adapter_type{r}; }
@@ -67,7 +68,7 @@ template <class T>
struct response_traits<std::optional<T>>
{
using response_type = std::optional<T>;
using adapter_type = adapter::detail::adapter_optional_simple<typename response_type::value_type>;
using adapter_type = adapter::detail::simple_optional<typename response_type::value_type>;
static auto adapt(response_type& i) noexcept { return adapter_type{i}; }
};
@@ -75,7 +76,7 @@ template <class T, class Allocator>
struct response_traits<std::vector<T, Allocator>>
{
using response_type = std::vector<T, Allocator>;
using adapter_type = adapter::detail::adapter_vector<response_type>;
using adapter_type = adapter::detail::vector<response_type>;
static auto adapt(response_type& v) noexcept { return adapter_type{v}; }
};
@@ -91,7 +92,7 @@ template <class Allocator>
struct response_traits<std::vector<node, Allocator>>
{
using response_type = std::vector<node, Allocator>;
using adapter_type = adapter::detail::adapter_general<response_type>;
using adapter_type = adapter::detail::general<response_type>;
static auto adapt(response_type& v) noexcept { return adapter_type{v}; }
};
@@ -99,7 +100,7 @@ template <class T, class Allocator>
struct response_traits<std::list<T, Allocator>>
{
using response_type = std::list<T, Allocator>;
using adapter_type = adapter::detail::adapter_list<response_type>;
using adapter_type = adapter::detail::list<response_type>;
static auto adapt(response_type& v) noexcept { return adapter_type{v}; }
};
@@ -107,7 +108,7 @@ template <class T, class Allocator>
struct response_traits<std::deque<T, Allocator>>
{
using response_type = std::deque<T, Allocator>;
using adapter_type = adapter::detail::adapter_list<response_type>;
using adapter_type = adapter::detail::list<response_type>;
static auto adapt(response_type& v) noexcept { return adapter_type{v}; }
};
@@ -115,7 +116,7 @@ template <class Key, class Compare, class Allocator>
struct response_traits<std::set<Key, Compare, Allocator>>
{
using response_type = std::set<Key, Compare, Allocator>;
using adapter_type = adapter::detail::adapter_set<response_type>;
using adapter_type = adapter::detail::set<response_type>;
static auto adapt(response_type& s) noexcept { return adapter_type{s}; }
};
@@ -123,7 +124,7 @@ template <class Key, class Hash, class KeyEqual, class Allocator>
struct response_traits<std::unordered_set<Key, Hash, KeyEqual, Allocator>>
{
using response_type = std::unordered_set<Key, Hash, KeyEqual, Allocator>;
using adapter_type = adapter::detail::adapter_set<response_type>;
using adapter_type = adapter::detail::set<response_type>;
static auto adapt(response_type& s) noexcept { return adapter_type{s}; }
};
@@ -131,7 +132,7 @@ template <class Key, class T, class Compare, class Allocator>
struct response_traits<std::map<Key, T, Compare, Allocator>>
{
using response_type = std::map<Key, T, Compare, Allocator>;
using adapter_type = adapter::detail::adapter_map<response_type>;
using adapter_type = adapter::detail::map<response_type>;
static auto adapt(response_type& s) noexcept { return adapter_type{s}; }
};
@@ -139,7 +140,7 @@ template <class Key, class Hash, class KeyEqual, class Allocator>
struct response_traits<std::unordered_map<Key, Hash, KeyEqual, Allocator>>
{
using response_type = std::unordered_map<Key, Hash, KeyEqual, Allocator>;
using adapter_type = adapter::detail::adapter_map<response_type>;
using adapter_type = adapter::detail::map<response_type>;
static auto adapt(response_type& s) noexcept { return adapter_type{s}; }
};
@@ -147,7 +148,7 @@ template <>
struct response_traits<void>
{
using response_type = void;
using adapter_type = adapter::detail::adapter_ignore;
using adapter_type = adapter::detail::ignore;
static auto adapt() noexcept { return adapter_type{}; }
};