Skip to content

Commit 6ed3744

Browse files
committed
post_on
1 parent 98185ea commit 6ed3744

8 files changed

+843
-0
lines changed

Diff for: .clang-format

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: true
7+
AlignConsecutiveDeclarations: true
8+
AlignEscapedNewlines: Right
9+
AlignOperands: Align
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: false
12+
AllowShortBlocksOnASingleLine: Never
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: InlineOnly
15+
AllowShortIfStatementsOnASingleLine: Never
16+
AllowShortLoopsOnASingleLine: false
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: All
19+
AlwaysBreakBeforeMultilineStrings: true
20+
AlwaysBreakTemplateDeclarations: Yes
21+
BinPackArguments: false
22+
##BinPackArguments: true
23+
BinPackParameters: false
24+
##BinPackParameters: false
25+
BraceWrapping:
26+
AfterClass: true
27+
AfterControlStatement: MultiLine
28+
AfterEnum: true
29+
AfterFunction: true
30+
AfterNamespace: false
31+
AfterObjCDeclaration: true
32+
AfterStruct: true
33+
AfterUnion: true
34+
AfterExternBlock: true
35+
BeforeCatch: true
36+
BeforeElse: true
37+
IndentBraces: false
38+
SplitEmptyFunction: true
39+
SplitEmptyRecord: true
40+
SplitEmptyNamespace: false
41+
BreakBeforeBinaryOperators: None
42+
BreakBeforeBraces: Allman
43+
BreakBeforeInheritanceComma: true
44+
BreakInheritanceList: BeforeColon
45+
BreakBeforeTernaryOperators: true
46+
BreakConstructorInitializersBeforeComma: true
47+
BreakConstructorInitializers: BeforeColon
48+
BreakAfterJavaFieldAnnotations: false
49+
BreakStringLiterals: true
50+
ColumnLimit: 80
51+
CommentPragmas: '^ IWYU pragma:'
52+
CompactNamespaces: true
53+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
54+
ConstructorInitializerIndentWidth: 0
55+
ContinuationIndentWidth: 4
56+
#Cpp11BracedListStyle: true
57+
Cpp11BracedListStyle: false
58+
DerivePointerAlignment: false
59+
DisableFormat: false
60+
ExperimentalAutoDetectBinPacking: false
61+
FixNamespaceComments: true
62+
ForEachMacros:
63+
- foreach
64+
- Q_FOREACH
65+
- BOOST_FOREACH
66+
IncludeBlocks: Regroup
67+
IncludeCategories:
68+
- Regex: '^<[^/]+/'
69+
Priority: 3
70+
- Regex: '^<[^/]+.hpp>'
71+
Priority: 2
72+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
73+
Priority: 4
74+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
75+
Priority: 6
76+
- Regex: '<[a-z0-9]+>'
77+
Priority: 7
78+
# - Regex: '.*'
79+
# Priority: 1
80+
IncludeIsMainRegex: '(Test)?$'
81+
IndentCaseLabels: false
82+
IndentPPDirectives: None
83+
IndentWidth: 4
84+
IndentWrappedFunctionNames: false
85+
JavaScriptQuotes: Leave
86+
JavaScriptWrapImports: true
87+
KeepEmptyLinesAtTheStartOfBlocks: false
88+
MacroBlockBegin: 'reenter\s(.*)'
89+
MacroBlockEnd: ''
90+
MaxEmptyLinesToKeep: 1
91+
NamespaceIndentation: None
92+
ObjCBinPackProtocolList: Auto
93+
ObjCBlockIndentWidth: 4
94+
ObjCSpaceAfterProperty: false
95+
ObjCSpaceBeforeProtocolList: true
96+
PenaltyBreakAssignment: 2
97+
PenaltyBreakBeforeFirstCallParameter: 19
98+
PenaltyBreakComment: 300
99+
PenaltyBreakFirstLessLess: 120
100+
PenaltyBreakString: 1000
101+
PenaltyBreakTemplateDeclaration: 10
102+
PenaltyExcessCharacter: 1000000
103+
PenaltyReturnTypeOnItsOwnLine: 60
104+
PointerAlignment: Right
105+
ReflowComments: true
106+
SortIncludes: true
107+
SortUsingDeclarations: true
108+
SpaceAfterCStyleCast: false
109+
SpaceAfterTemplateKeyword: true
110+
SpaceBeforeAssignmentOperators: true
111+
SpaceBeforeCpp11BracedList: true
112+
SpaceBeforeCtorInitializerColon: true
113+
SpaceBeforeInheritanceColon: true
114+
SpaceBeforeParens: ControlStatements
115+
SpaceBeforeRangeBasedForLoopColon: true
116+
SpaceInEmptyParentheses: false
117+
SpacesBeforeTrailingComments: 3
118+
SpacesInAngles: true
119+
SpacesInContainerLiterals: true
120+
SpacesInCStyleCastParentheses: false
121+
SpacesInParentheses: false
122+
SpacesInSquareBrackets: false
123+
Standard: c++20
124+
TabWidth: 4
125+
UseTab: Never
126+
...
127+

Diff for: CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(asio_experiments)
4+
set(CMAKE_CXX_STANDARD 20)
5+
if (MSVC)
6+
add_compile_options("/await:strict")
7+
endif()
8+
include(FetchContent)
9+
FetchContent_Declare(asio
10+
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
11+
GIT_TAG master
12+
GIT_SHALLOW Yes)
13+
14+
FetchContent_GetProperties(asio)
15+
if(NOT asio_POPULATED)
16+
FetchContent_Populate(asio)
17+
endif()
18+
19+
find_package(Threads REQUIRED)
20+
21+
add_library(asio_asio INTERFACE)
22+
target_include_directories(asio_asio INTERFACE "${asio_SOURCE_DIR}/asio/include")
23+
target_link_libraries(asio_asio INTERFACE Threads::Threads)
24+
add_library(asio::asio ALIAS asio_asio)
25+
26+
file(GLOB src_files CONFIGURE_DEPENDS "*.cpp")
27+
foreach(src IN LISTS src_files)
28+
get_filename_component(target_name "${src}" NAME_WLE)
29+
add_executable("${target_name}" "${src}")
30+
target_link_libraries("${target_name}" PUBLIC asio::asio)
31+
endforeach()

Diff for: append_prepend.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Copyright (c) 2021 Richard Hodges ([email protected])
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/madmongo1/blog-2021-10
8+
//
9+
10+
#include <asio.hpp>
11+
#include <asio/experimental/append.hpp>
12+
#include <asio/experimental/prepend.hpp>
13+
14+
#include <iostream>
15+
16+
struct object
17+
{
18+
};
19+
20+
struct my_handler
21+
{
22+
void operator()(std::error_code)
23+
{
24+
std::cout << "(error_code)\n";
25+
}
26+
27+
void operator()(std::error_code, std::size_t)
28+
{
29+
std::cout << "(error_code, size_t)\n";
30+
}
31+
32+
void operator()(std::error_code, std::size_t, std::string)
33+
{
34+
std::cout << "(error_code, size_t, string)\n";
35+
}
36+
37+
void operator()(std::error_code, std::string)
38+
{
39+
std::cout << "(error_code, string)\n";
40+
}
41+
42+
void operator()(object*, std::error_code, std::size_t, std::string)
43+
{
44+
std::cout << "(object*, error_code, size_t, string)\n";
45+
}
46+
47+
void operator()(object*, std::error_code, std::size_t)
48+
{
49+
std::cout << "(object*, error_code, size_t)\n";
50+
}
51+
52+
void operator()(object*, std::error_code, std::string)
53+
{
54+
std::cout << "(object*, error_code, string)\n";
55+
}
56+
57+
void operator()(object*, std::error_code)
58+
{
59+
std::cout << "(object*, error_code)\n";
60+
}
61+
};
62+
63+
int main()
64+
{
65+
using asio::experimental::append;
66+
using asio::experimental::prepend;
67+
68+
asio::io_context ioc;
69+
auto sock = asio::ip::tcp::socket(ioc);
70+
auto timer = asio::steady_timer(ioc);
71+
char buf[1024];
72+
73+
auto handler = my_handler();
74+
auto self = object();
75+
auto message = "Hello, World!";
76+
77+
sock.async_read_some(asio::buffer(buf), handler);
78+
sock.async_read_some(asio::buffer(buf), prepend(handler, &self));
79+
sock.async_read_some(asio::buffer(buf), append(prepend(handler, &self), message));
80+
81+
timer.async_wait(handler);
82+
timer.async_wait(append(handler, message));
83+
timer.async_wait(prepend(append(handler, message), &self));
84+
ioc.run();
85+
}

Diff for: as_single.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright (c) 2021 Richard Hodges ([email protected])
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/madmongo1/blog-2021-10
8+
//
9+
10+
#include <asio.hpp>
11+
#include <asio/experimental/as_single.hpp>
12+
13+
#include <iostream>
14+
15+
16+
int main()
17+
{
18+
asio::io_context ioc;
19+
auto sock = asio::ip::tcp::socket(ioc);
20+
auto timer = asio::steady_timer(ioc);
21+
char buf[1024];
22+
23+
sock.async_read_some(asio::buffer(buf), [](std::error_code, std::size_t){});
24+
sock.async_read_some(asio::buffer(buf), asio::experimental::as_single([](std::tuple<std::error_code, std::size_t>){}));
25+
26+
timer.async_wait([](std::error_code){});
27+
timer.async_wait(asio::experimental::as_single([](std::error_code){}));
28+
ioc.run();
29+
}

Diff for: debug/report_fatal.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Copyright (c) 2021 Richard Hodges ([email protected])
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/madmongo1/blog-2021-10
8+
//
9+
10+
#ifndef BLOG_OCT_2021_DEBUG_REPORT_FATAL_HPP
11+
#define BLOG_OCT_2021_DEBUG_REPORT_FATAL_HPP
12+
13+
#include <iostream>
14+
15+
namespace debug
16+
{
17+
template<class Context>
18+
struct fatal_reporter
19+
{
20+
Context context;
21+
22+
void operator()(std::exception_ptr ep, auto...args) const
23+
{
24+
std::cout << context << ": ";
25+
try {
26+
if (ep)
27+
std::rethrow_exception(ep);
28+
const char* sep = "";
29+
auto emit = [&sep](auto&& arg)
30+
{
31+
std::cout << sep << arg;
32+
sep = ", ";
33+
};
34+
(emit(args),...);
35+
}
36+
catch(std::exception& e)
37+
{
38+
std::cout << "exception: " << e.what() << "\n";
39+
}
40+
std::cout << "\n";
41+
}
42+
};
43+
44+
inline constexpr auto report_fatal(auto context)
45+
{
46+
return fatal_reporter<decltype(context)> { context };
47+
}
48+
49+
}
50+
51+
#endif // BLOG_OCT_2021_DEBUG_REPORT_FATAL_HPP

0 commit comments

Comments
 (0)