Loading [MathJax]/extensions/tex2jax.js

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Boost.Redis: example/cpp20_protobuf.cpp Source File
Loading...
Searching...
No Matches
cpp20_protobuf.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#include <boost/redis/connection.hpp>
8#include <boost/redis/resp3/serialization.hpp>
9
10#include <boost/asio/co_spawn.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/asio/detached.hpp>
13#include <boost/system/errc.hpp>
14
15#include <iostream>
16
17// See the definition in person.proto. This header is automatically
18// generated by CMakeLists.txt.
19#include "person.pb.h"
20
21#if defined(BOOST_ASIO_HAS_CO_AWAIT)
22
23namespace asio = boost::asio;
24namespace resp3 = boost::redis::resp3;
32
33// The protobuf type described in example/person.proto
34using tutorial::person;
35
36// Boost.Redis customization points (example/protobuf.hpp)
37namespace tutorial {
38
39// Below I am using a Boost.Redis to indicate a protobuf error, this
40// is ok for an example, users however might want to define their own
41// error codes.
42void boost_redis_to_bulk(std::string& to, person const& u)
43{
44 std::string tmp;
45 if (!u.SerializeToString(&tmp))
46 throw boost::system::system_error(boost::redis::error::invalid_data_type);
47
48 resp3::boost_redis_to_bulk(to, tmp);
49}
50
51void boost_redis_from_bulk(person& u, node_view const& node, boost::system::error_code& ec)
52{
53 std::string const tmp{node.value};
54 if (!u.ParseFromString(tmp))
56}
57
58} // namespace tutorial
59
60using tutorial::boost_redis_to_bulk;
61using tutorial::boost_redis_from_bulk;
62
63asio::awaitable<void> co_main(config cfg)
64{
65 auto ex = co_await asio::this_coro::executor;
66 auto conn = std::make_shared<connection>(ex);
67 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
68
69 person p;
70 p.set_name("Louis");
71 p.set_id(3);
72 p.set_email("No email yet.");
73
74 request req;
75 req.push("SET", "protobuf-key", p);
76 req.push("GET", "protobuf-key");
77
78 response<ignore_t, person> resp;
79
80 // Sends the request and receives the response.
81 co_await conn->async_exec(req, resp);
82 conn->cancel();
83
84 std::cout << "Name: " << std::get<1>(resp).value().name() << "\n"
85 << "Age: " << std::get<1>(resp).value().id() << "\n"
86 << "Email: " << std::get<1>(resp).value().email() << "\n";
87}
88
89#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:30
basic_node< std::string > node
A node in the response tree that owns its data.
Definition node.hpp:62
operation
Connection operations that can be cancelled.
Definition operation.hpp:19
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
@ invalid_data_type
Invalid RESP3 type.
Configure parameters used by the connection classes.
Definition config.hpp:30
A node in the response tree.
Definition node.hpp:28