-
Notifications
You must be signed in to change notification settings - Fork 42
Add parse event init, node and done. #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected]) | ||
/* Copyright (c) 2018-2025 Marcelo Zimbres Silva ([email protected]) | ||
* | ||
* Distributed under the Boost Software License, Version 1.0. (See | ||
* accompanying file LICENSE.txt) | ||
|
@@ -34,24 +34,41 @@ namespace boost::redis { | |
*/ | ||
class any_adapter { | ||
public: | ||
using fn_type = std::function<void(std::size_t, resp3::node_view const&, system::error_code&)>; | ||
/** @brief Parse events that an adapter must support. | ||
*/ | ||
enum class parse_event | ||
{ | ||
/// Called before the parser starts processing data | ||
init, | ||
/// Called for each and every node of RESP3 data | ||
node, | ||
/// Called when done processing a complete RESP3 message | ||
done | ||
}; | ||
|
||
struct impl_t { | ||
fn_type adapt_fn; | ||
std::size_t supported_response_size; | ||
} impl_; | ||
/// The type erased implementation type. | ||
using impl_t = std::function<void(parse_event, resp3::node_view const&, system::error_code&)>; | ||
|
||
template <class T> | ||
static auto create_impl(T& resp) -> impl_t | ||
{ | ||
using namespace boost::redis::adapter; | ||
auto adapter = boost_redis_adapt(resp); | ||
std::size_t size = adapter.get_supported_response_size(); | ||
return {std::move(adapter), size}; | ||
return [adapter2 = boost_redis_adapt(resp)]( | ||
any_adapter::parse_event ev, | ||
resp3::node_view const& nd, | ||
system::error_code& ec) mutable { | ||
switch (ev) { | ||
case parse_event::init: adapter2.on_init(); break; | ||
case parse_event::node: adapter2.on_node(nd, ec); break; | ||
case parse_event::done: adapter2.on_done(); break; | ||
} | ||
}; | ||
} | ||
|
||
template <class Executor> | ||
friend class basic_connection; | ||
/// Contructs from a type erased adaper | ||
any_adapter(impl_t fn = [](parse_event, resp3::node_view const&, system::error_code&) { }) | ||
: impl_{std::move(fn)} | ||
{ } | ||
|
||
/** | ||
* @brief Constructor. | ||
|
@@ -67,6 +84,29 @@ class any_adapter { | |
explicit any_adapter(T& resp) | ||
: impl_(create_impl(resp)) | ||
{ } | ||
|
||
/// Calls the implementation with the arguments `impl_(parse_event::init, ...);` | ||
void on_init() | ||
{ | ||
system::error_code ec; | ||
impl_(parse_event::init, {}, ec); | ||
}; | ||
|
||
/// Calls the implementation with the arguments `impl_(parse_event::done, ...);` | ||
void on_done() | ||
{ | ||
system::error_code ec; | ||
impl_(parse_event::done, {}, ec); | ||
}; | ||
|
||
/// Calls the implementation with the arguments `impl_(parse_event::node, ...);` | ||
void on_node(resp3::node_view const& nd, system::error_code& ec) | ||
{ | ||
impl_(parse_event::node, nd, ec); | ||
}; | ||
|
||
private: | ||
impl_t impl_; | ||
}; | ||
|
||
} // namespace boost::redis | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.