Skip to content

Commit 663e9ac

Browse files
committed
Simplifications.
1 parent c0aa435 commit 663e9ac

23 files changed

+94
-136
lines changed

CMakeLists.txt

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,21 @@ find_package(OpenSSL REQUIRED)
5555
enable_testing()
5656
include_directories(include)
5757

58+
#=======================================================================
59+
60+
add_library(boost_redis_src STATIC examples/boost_redis.cpp)
61+
target_compile_features(boost_redis_src PUBLIC cxx_std_20)
62+
if (MSVC)
63+
target_compile_options(boost_redis_src PRIVATE /bigobj)
64+
target_compile_definitions(boost_redis_src PRIVATE _WIN32_WINNT=0x0601)
65+
endif()
66+
5867
# Main function for the examples.
5968
#=======================================================================
6069

61-
add_library(test_common STATIC
62-
tests/common.cpp
63-
)
64-
target_link_libraries(test_common PUBLIC OpenSSL::Crypto OpenSSL::SSL)
70+
add_library(test_common STATIC)
71+
target_sources(test_common PUBLIC tests/common.cpp)
72+
target_link_libraries(test_common PUBLIC OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
6573
target_compile_features(test_common PUBLIC cxx_std_17)
6674
if (MSVC)
6775
target_compile_options(test_common PRIVATE /bigobj)
@@ -70,22 +78,19 @@ endif()
7078

7179
#=======================================================================
7280

73-
add_library(common STATIC
74-
examples/start.cpp
75-
examples/main.cpp
76-
examples/boost_redis.cpp
77-
)
78-
target_compile_features(common PUBLIC cxx_std_20)
81+
add_library(examples_common STATIC examples/main.cpp)
82+
target_compile_features(examples_common PUBLIC cxx_std_20)
83+
target_link_libraries(examples_common PRIVATE boost_redis_src)
7984
if (MSVC)
80-
target_compile_options(common PRIVATE /bigobj)
81-
target_compile_definitions(common PRIVATE _WIN32_WINNT=0x0601)
85+
target_compile_options(examples_common PRIVATE /bigobj)
86+
target_compile_definitions(examples_common PRIVATE _WIN32_WINNT=0x0601)
8287
endif()
8388

8489
# Executables
8590
#=======================================================================
8691

8792
add_executable(cpp20_intro examples/cpp20_intro.cpp)
88-
target_link_libraries(cpp20_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
93+
target_link_libraries(cpp20_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
8994
target_compile_features(cpp20_intro PUBLIC cxx_std_20)
9095
add_test(cpp20_intro cpp20_intro)
9196
if (MSVC)
@@ -94,7 +99,7 @@ if (MSVC)
9499
endif()
95100

96101
add_executable(cpp20_streams examples/cpp20_streams.cpp)
97-
target_link_libraries(cpp20_streams PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
102+
target_link_libraries(cpp20_streams PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
98103
target_compile_features(cpp20_streams PUBLIC cxx_std_20)
99104
if (MSVC)
100105
target_compile_options(cpp20_streams PRIVATE /bigobj)
@@ -120,12 +125,12 @@ endif()
120125
if (NOT MSVC)
121126
add_executable(cpp20_chat_room examples/cpp20_chat_room.cpp)
122127
target_compile_features(cpp20_chat_room PUBLIC cxx_std_20)
123-
target_link_libraries(cpp20_chat_room PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
128+
target_link_libraries(cpp20_chat_room PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
124129
endif()
125130

126131
add_executable(cpp20_containers examples/cpp20_containers.cpp)
127132
target_compile_features(cpp20_containers PUBLIC cxx_std_20)
128-
target_link_libraries(cpp20_containers PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
133+
target_link_libraries(cpp20_containers PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
129134
add_test(cpp20_containers cpp20_containers)
130135
if (MSVC)
131136
target_compile_options(cpp20_containers PRIVATE /bigobj)
@@ -135,12 +140,12 @@ endif()
135140
if (NOT MSVC)
136141
add_executable(cpp20_echo_server examples/cpp20_echo_server.cpp)
137142
target_compile_features(cpp20_echo_server PUBLIC cxx_std_20)
138-
target_link_libraries(cpp20_echo_server PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
143+
target_link_libraries(cpp20_echo_server PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
139144
endif()
140145

141146
add_executable(cpp20_resolve_with_sentinel examples/cpp20_resolve_with_sentinel.cpp)
142147
target_compile_features(cpp20_resolve_with_sentinel PUBLIC cxx_std_20)
143-
target_link_libraries(cpp20_resolve_with_sentinel PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
148+
target_link_libraries(cpp20_resolve_with_sentinel PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
144149
#add_test(cpp20_resolve_with_sentinel cpp20_resolve_with_sentinel)
145150
if (MSVC)
146151
target_compile_options(cpp20_resolve_with_sentinel PRIVATE /bigobj)
@@ -149,7 +154,7 @@ endif()
149154

150155
add_executable(cpp20_json examples/cpp20_json.cpp)
151156
target_compile_features(cpp20_json PUBLIC cxx_std_20)
152-
target_link_libraries(cpp20_json PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
157+
target_link_libraries(cpp20_json PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
153158
add_test(cpp20_json cpp20_json)
154159
if (MSVC)
155160
target_compile_options(cpp20_json PRIVATE /bigobj)
@@ -160,7 +165,7 @@ if (Protobuf_FOUND)
160165
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS examples/person.proto)
161166
add_executable(cpp20_protobuf examples/cpp20_protobuf.cpp ${PROTO_SRCS} ${PROTO_HDRS})
162167
target_compile_features(cpp20_protobuf PUBLIC cxx_std_20)
163-
target_link_libraries(cpp20_protobuf PRIVATE OpenSSL::Crypto OpenSSL::SSL common ${Protobuf_LIBRARIES})
168+
target_link_libraries(cpp20_protobuf PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common ${Protobuf_LIBRARIES})
164169
target_include_directories(cpp20_protobuf PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
165170
add_test(cpp20_protobuf cpp20_protobuf)
166171
if (MSVC)
@@ -171,7 +176,7 @@ endif()
171176

172177
add_executable(cpp20_subscriber examples/cpp20_subscriber.cpp)
173178
target_compile_features(cpp20_subscriber PUBLIC cxx_std_20)
174-
target_link_libraries(cpp20_subscriber PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
179+
target_link_libraries(cpp20_subscriber PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
175180
if (MSVC)
176181
target_compile_options(cpp20_subscriber PRIVATE /bigobj)
177182
target_compile_definitions(cpp20_subscriber PRIVATE _WIN32_WINNT=0x0601)
@@ -180,7 +185,7 @@ endif()
180185
add_executable(cpp20_intro_tls examples/cpp20_intro_tls.cpp)
181186
target_compile_features(cpp20_intro_tls PUBLIC cxx_std_20)
182187
add_test(cpp20_intro_tls cpp20_intro_tls)
183-
target_link_libraries(cpp20_intro_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
188+
target_link_libraries(cpp20_intro_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
184189
if (MSVC)
185190
target_compile_options(cpp20_intro_tls PRIVATE /bigobj)
186191
target_compile_definitions(cpp20_intro_tls PRIVATE _WIN32_WINNT=0x0601)
@@ -189,7 +194,7 @@ endif()
189194
add_executable(cpp20_low_level_async tests/cpp20_low_level_async.cpp)
190195
target_compile_features(cpp20_low_level_async PUBLIC cxx_std_20)
191196
add_test(cpp20_low_level_async cpp20_low_level_async)
192-
target_link_libraries(cpp20_low_level_async PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
197+
target_link_libraries(cpp20_low_level_async PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
193198
if (MSVC)
194199
target_compile_options(cpp20_low_level_async PRIVATE /bigobj)
195200
target_compile_definitions(cpp20_low_level_async PRIVATE _WIN32_WINNT=0x0601)
@@ -211,6 +216,7 @@ endif()
211216

212217
add_executable(cpp17_low_level_sync tests/cpp17_low_level_sync.cpp)
213218
target_compile_features(cpp17_low_level_sync PUBLIC cxx_std_17)
219+
target_link_libraries(cpp17_low_level_sync PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
214220
add_test(cpp17_low_level_sync cpp17_low_level_sync)
215221
if (MSVC)
216222
target_compile_options(cpp17_low_level_sync PRIVATE /bigobj)
@@ -255,7 +261,7 @@ endif()
255261

256262
add_executable(test_conn_reconnect tests/conn_reconnect.cpp)
257263
target_compile_features(test_conn_reconnect PUBLIC cxx_std_20)
258-
target_link_libraries(test_conn_reconnect PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
264+
target_link_libraries(test_conn_reconnect PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
259265
add_test(test_conn_reconnect test_conn_reconnect)
260266
if (MSVC)
261267
target_compile_options(test_conn_reconnect PRIVATE /bigobj)
@@ -265,14 +271,15 @@ endif()
265271
add_executable(test_conn_tls tests/conn_tls.cpp)
266272
add_test(test_conn_tls test_conn_tls)
267273
target_compile_features(test_conn_tls PUBLIC cxx_std_17)
268-
target_link_libraries(test_conn_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL)
274+
target_link_libraries(test_conn_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
269275
if (MSVC)
270276
target_compile_options(test_conn_tls PRIVATE /bigobj)
271277
target_compile_definitions(test_conn_tls PRIVATE _WIN32_WINNT=0x0601)
272278
endif()
273279

274280
add_executable(test_low_level tests/low_level.cpp)
275281
target_compile_features(test_low_level PUBLIC cxx_std_17)
282+
target_link_libraries(test_low_level PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
276283
add_test(test_low_level test_low_level)
277284
if (MSVC)
278285
target_compile_options(test_low_level PRIVATE /bigobj)
@@ -282,15 +289,15 @@ endif()
282289
add_executable(test_conn_run_cancel tests/conn_run_cancel.cpp)
283290
target_compile_features(test_conn_run_cancel PUBLIC cxx_std_20)
284291
add_test(test_conn_run_cancel test_conn_run_cancel)
285-
target_link_libraries(test_conn_run_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL)
292+
target_link_libraries(test_conn_run_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
286293
if (MSVC)
287294
target_compile_options(test_conn_run_cancel PRIVATE /bigobj)
288295
target_compile_definitions(test_conn_run_cancel PRIVATE _WIN32_WINNT=0x0601)
289296
endif()
290297

291298
add_executable(test_conn_exec_cancel tests/conn_exec_cancel.cpp)
292299
target_compile_features(test_conn_exec_cancel PUBLIC cxx_std_20)
293-
target_link_libraries(test_conn_exec_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
300+
target_link_libraries(test_conn_exec_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
294301
add_test(test_conn_exec_cancel test_conn_exec_cancel)
295302
if (MSVC)
296303
target_compile_options(test_conn_exec_cancel PRIVATE /bigobj)
@@ -299,7 +306,7 @@ endif()
299306

300307
add_executable(test_conn_exec_cancel2 tests/conn_exec_cancel2.cpp)
301308
target_compile_features(test_conn_exec_cancel2 PUBLIC cxx_std_20)
302-
target_link_libraries(test_conn_exec_cancel2 PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
309+
target_link_libraries(test_conn_exec_cancel2 PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
303310
add_test(test_conn_exec_cancel2 test_conn_exec_cancel2)
304311
if (MSVC)
305312
target_compile_options(test_conn_exec_cancel2 PRIVATE /bigobj)
@@ -308,7 +315,7 @@ endif()
308315

309316
add_executable(test_conn_exec_error tests/conn_exec_error.cpp)
310317
target_compile_features(test_conn_exec_error PUBLIC cxx_std_17)
311-
target_link_libraries(test_conn_exec_error PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
318+
target_link_libraries(test_conn_exec_error PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
312319
add_test(test_conn_exec_error test_conn_exec_error)
313320
if (MSVC)
314321
target_compile_options(test_conn_exec_error PRIVATE /bigobj)
@@ -317,7 +324,7 @@ endif()
317324

318325
add_executable(test_conn_echo_stress tests/conn_echo_stress.cpp)
319326
target_compile_features(test_conn_echo_stress PUBLIC cxx_std_20)
320-
target_link_libraries(test_conn_echo_stress PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
327+
target_link_libraries(test_conn_echo_stress PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
321328
add_test(test_conn_echo_stress test_conn_echo_stress)
322329
if (MSVC)
323330
target_compile_options(test_conn_echo_stress PRIVATE /bigobj)
@@ -326,6 +333,7 @@ endif()
326333

327334
add_executable(test_request tests/request.cpp)
328335
target_compile_features(test_request PUBLIC cxx_std_17)
336+
target_link_libraries(test_request PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
329337
add_test(test_request test_request)
330338
if (MSVC)
331339
target_compile_options(test_request PRIVATE /bigobj)
@@ -334,7 +342,7 @@ endif()
334342

335343
add_executable(test_issue_50 tests/issue_50.cpp)
336344
target_compile_features(test_issue_50 PUBLIC cxx_std_20)
337-
target_link_libraries(test_issue_50 PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
345+
target_link_libraries(test_issue_50 PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
338346
add_test(test_issue_50 test_issue_50)
339347
if (MSVC)
340348
target_compile_options(test_issue_50 PRIVATE /bigobj)
@@ -343,7 +351,7 @@ endif()
343351

344352
add_executable(test_conn_check_health tests/conn_check_health.cpp)
345353
target_compile_features(test_conn_check_health PUBLIC cxx_std_17)
346-
target_link_libraries(test_conn_check_health PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
354+
target_link_libraries(test_conn_check_health PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
347355
add_test(test_conn_check_health test_conn_check_health)
348356
if (MSVC)
349357
target_compile_options(test_conn_check_health PRIVATE /bigobj)
@@ -352,7 +360,7 @@ endif()
352360

353361
add_executable(test_run tests/run.cpp)
354362
target_compile_features(test_run PUBLIC cxx_std_17)
355-
target_link_libraries(test_run PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
363+
target_link_libraries(test_run PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
356364
add_test(test_run test_run)
357365
if (MSVC)
358366
target_compile_options(test_run PRIVATE /bigobj)

examples/main.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,44 @@
44
* accompanying file LICENSE.txt)
55
*/
66

7-
#include "start.hpp"
87
#include <boost/redis/config.hpp>
9-
#include <boost/asio/awaitable.hpp>
10-
#include <string>
8+
#include <boost/asio/co_spawn.hpp>
9+
#include <boost/asio/use_awaitable.hpp>
10+
#include <boost/asio/io_context.hpp>
1111
#include <iostream>
1212

13-
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
14-
13+
namespace net = boost::asio;
1514
using boost::redis::config;
1615

17-
extern boost::asio::awaitable<void> co_main(config);
16+
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17+
18+
extern net::awaitable<void> co_main(config);
1819

1920
auto main(int argc, char * argv[]) -> int
2021
{
21-
config cfg;
22-
23-
if (argc == 3) {
24-
cfg.addr.host = argv[1];
25-
cfg.addr.port = argv[2];
22+
try {
23+
config cfg;
24+
25+
if (argc == 3) {
26+
cfg.addr.host = argv[1];
27+
cfg.addr.port = argv[2];
28+
}
29+
30+
net::io_context ioc;
31+
net::co_spawn(ioc, std::move(co_main(cfg)), [](std::exception_ptr p) {
32+
if (p)
33+
std::rethrow_exception(p);
34+
});
35+
ioc.run();
36+
37+
} catch (std::exception const& e) {
38+
std::cerr << "(main) " << e.what() << std::endl;
39+
return 1;
2640
}
27-
28-
return start(co_main(cfg));
2941
}
3042

3143
#else // defined(BOOST_ASIO_HAS_CO_AWAIT)
3244

33-
#include <iostream>
34-
3545
auto main() -> int
3646
{
3747
std::cout << "Requires coroutine support." << std::endl;

examples/start.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/start.hpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)