Skip to content

Commit

Permalink
Allow short, inline lambdas to be on a single line
Browse files Browse the repository at this point in the history
This also requires setting BraceWrapping > BeforeLambdaBody to false to
have any reasonable looking style.
  • Loading branch information
trflynn89 committed Oct 14, 2021
1 parent 4af0c82 commit 8ffd932
Show file tree
Hide file tree
Showing 51 changed files with 360 additions and 635 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
Expand All @@ -38,7 +38,7 @@ BraceWrapping:
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
Expand Down
22 changes: 4 additions & 18 deletions bench/util/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ void Table<Args...>::print_row(std::ostream &stream, const Row &row) const
{
std::size_t index = 0;

auto print_value = [this, &stream, &index](const auto &value)
{
auto print_value = [this, &stream, &index](const auto &value) {
print_column_separator(stream, index == 0 ? s_border_style : fly::logger::Style::Default);

const auto style = fly::logger::Styler(s_data_style, s_data_color);
Expand All @@ -317,13 +316,7 @@ void Table<Args...>::print_row(std::ostream &stream, const Row &row) const
++index;
};

std::apply(
[&print_value](const auto &...e)
{
(print_value(e), ...);
},
row);

std::apply([&print_value](const auto &...e) { (print_value(e), ...); }, row);
print_column_separator(stream, s_border_style) << '\n';
}

Expand Down Expand Up @@ -355,8 +348,7 @@ auto Table<Args...>::column_widths_for_row(const Row &row) -> RowSizedArray<std:
RowSizedArray<std::size_t> widths;
std::size_t index = 0;

auto size_of_value = [&widths, &index](const auto &value)
{
auto size_of_value = [&widths, &index](const auto &value) {
using T = std::decay_t<decltype(value)>;

if constexpr (fly::StandardStringLike<T>)
Expand Down Expand Up @@ -389,13 +381,7 @@ auto Table<Args...>::column_widths_for_row(const Row &row) -> RowSizedArray<std:
++index;
};

std::apply(
[&size_of_value](const auto &...e)
{
(size_of_value(e), ...);
},
row);

std::apply([&size_of_value](const auto &...e) { (size_of_value(e), ...); }, row);
return widths;
}

Expand Down
3 changes: 1 addition & 2 deletions fly/coders/huffman/huffman_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ void HuffmanEncoder::insert_code(HuffmanCode &&code)
//==================================================================================================
void HuffmanEncoder::limit_code_lengths()
{
auto compute_kraft = [this](const HuffmanCode &code) -> code_type
{
auto compute_kraft = [this](const HuffmanCode &code) -> code_type {
return 1_u16 << (m_max_code_length - code.m_length);
};

Expand Down
6 changes: 2 additions & 4 deletions fly/config/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ bool ConfigManager::start()

std::weak_ptr<ConfigManager> weak_self = shared_from_this();

auto callback = [weak_self](std::filesystem::path, fly::path::PathEvent)
{
auto callback = [weak_self](std::filesystem::path, fly::path::PathEvent) {
if (auto self = weak_self.lock(); self)
{
auto task = [](std::shared_ptr<ConfigManager> nested_self)
{
auto task = [](std::shared_ptr<ConfigManager> nested_self) {
nested_self->update_config();
};

Expand Down
3 changes: 1 addition & 2 deletions fly/logger/detail/win/styler_proxy_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ void StylerProxyImpl::apply_value<WORD, fly::logger::Color>(
const fly::logger::Color &modifier)
{
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffers#character-attributes
auto apply_color = [&attributes, &modifier](bool red, bool green, bool blue)
{
auto apply_color = [&attributes, &modifier](bool red, bool green, bool blue) {
if (modifier.m_plane == fly::logger::Color::Foreground)
{
attributes = red ? (attributes | FOREGROUND_RED) : (attributes & ~FOREGROUND_RED);
Expand Down
3 changes: 1 addition & 2 deletions fly/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ void Logger::log(Level level, Trace &&trace, std::string &&message)
if (m_task_runner)
{
auto task = [level, trace = std::move(trace), message = std::move(message), now](
std::shared_ptr<Logger> self) mutable
{
std::shared_ptr<Logger> self) mutable {
if (!self->m_last_task_failed)
{
self->log_to_sink(level, std::move(trace), std::move(message), now);
Expand Down
3 changes: 1 addition & 2 deletions fly/net/ipv6_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ struct fly::Formatter<fly::net::IPv6Address>
fly::net::IPv6Address::address_type::value_type parts[s_address_size] {};
address.copy(parts);

auto join_segments = [&parts](std::size_t index) -> std::uint16_t
{
auto join_segments = [&parts](std::size_t index) -> std::uint16_t {
return (parts[index] << 8) | parts[index + 1];
};

Expand Down
3 changes: 1 addition & 2 deletions fly/net/socket/detail/nix/socket_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,7 @@ void select(

if (status > 0)
{
auto is_not_set = [](fd_set *set, socket_type handle)
{
auto is_not_set = [](fd_set *set, socket_type handle) {
return !FD_ISSET(handle, set);
};

Expand Down
3 changes: 1 addition & 2 deletions fly/net/socket/detail/win/socket_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,7 @@ void select(

if (status > 0)
{
auto is_not_set = [](fd_set *set, socket_type handle)
{
auto is_not_set = [](fd_set *set, socket_type handle) {
return !FD_ISSET(handle, set);
};

Expand Down
3 changes: 1 addition & 2 deletions fly/net/socket/listen_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ bool ListenSocket<EndpointType>::accept_async(AcceptCompletion &&callback)
{
service->notify_when_readable(
this->shared_from_this(),
[callback = std::move(callback)](std::shared_ptr<ListenSocket> self) mutable
{
[callback = std::move(callback)](std::shared_ptr<ListenSocket> self) mutable {
self->ready_to_accept(std::move(callback));
});

Expand Down
25 changes: 8 additions & 17 deletions fly/net/socket/socket_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ SocketService::~SocketService() noexcept
//==================================================================================================
void SocketService::remove_socket(socket_type handle)
{
auto task = [handle](std::shared_ptr<SocketService> self)
{
auto compare_handles = [handle](const Request &request)
{
auto task = [handle](std::shared_ptr<SocketService> self) {
auto compare_handles = [handle](const Request &request) {
return handle == request.m_handle;
};

Expand All @@ -65,8 +63,7 @@ void SocketService::remove_socket(socket_type handle)
//==================================================================================================
void SocketService::notify_when_writable(socket_type handle, Notification &&callback)
{
auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self)
{
auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self) {
const bool should_poll = self->m_write_requests.empty() && self->m_read_requests.empty();
self->m_write_requests.emplace_back(handle, std::move(callback));

Expand All @@ -83,8 +80,7 @@ void SocketService::notify_when_writable(socket_type handle, Notification &&call
//==================================================================================================
void SocketService::notify_when_readable(socket_type handle, Notification &&callback)
{
auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self)
{
auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self) {
const bool should_poll = self->m_write_requests.empty() && self->m_read_requests.empty();
self->m_read_requests.emplace_back(handle, std::move(callback));

Expand Down Expand Up @@ -115,15 +111,11 @@ void SocketService::poll()

detail::select(m_config->socket_io_wait_time(), writable, readable);

auto invoke = [](const std::set<socket_type> &ready, std::vector<Request> &pending)
{
auto invoke = [](const std::set<socket_type> &ready, std::vector<Request> &pending) {
for (socket_type handle : ready)
{
auto it = std::find_if(
pending.begin(),
pending.end(),
[handle](const Request &request)
{
auto it =
std::find_if(pending.begin(), pending.end(), [handle](const Request &request) {
return handle == request.m_handle;
});

Expand All @@ -137,8 +129,7 @@ void SocketService::poll()

if (!m_write_requests.empty() || !m_read_requests.empty())
{
auto task = [](std::shared_ptr<SocketService> self)
{
auto task = [](std::shared_ptr<SocketService> self) {
self->poll();
};

Expand Down
3 changes: 1 addition & 2 deletions fly/net/socket/socket_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ auto SocketService::wrap_callback(const std::shared_ptr<SocketType> &socket, Cal
std::weak_ptr<SocketType> weak_socket = socket;
CallbackHolder holder {std::forward<Callback>(callback)};

return [weak_socket = std::move(weak_socket), holder = std::move(holder)]() mutable
{
return [weak_socket = std::move(weak_socket), holder = std::move(holder)]() mutable {
if (std::shared_ptr<SocketType> strong_socket = weak_socket.lock(); strong_socket)
{
std::invoke(std::move(holder.m_callback), std::move(strong_socket));
Expand Down
15 changes: 5 additions & 10 deletions fly/net/socket/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ TcpSocket<EndpointType>::connect_async(const EndpointType &endpoint, ConnectComp
{
service->notify_when_writable(
this->shared_from_this(),
[callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable
{
[callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable {
std::invoke(std::move(callback), self->finish_connect());
});
}
Expand Down Expand Up @@ -263,8 +262,7 @@ bool TcpSocket<EndpointType>::send_async(std::string_view message, SendCompletio
{
service->notify_when_writable(
this->shared_from_this(),
[message, callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable
{
[message, callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable {
self->ready_to_send(message, std::move(callback), 0, message.size());
});

Expand Down Expand Up @@ -302,8 +300,7 @@ bool TcpSocket<EndpointType>::receive_async(ReceiveCompletion &&callback)
{
service->notify_when_readable(
this->shared_from_this(),
[callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable
{
[callback = std::move(callback)](std::shared_ptr<TcpSocket> self) mutable {
self->ready_to_receive(std::move(callback), std::string());
});

Expand Down Expand Up @@ -339,8 +336,7 @@ void TcpSocket<EndpointType>::ready_to_send(
socket_service()->notify_when_writable(
this->shared_from_this(),
[message, callback = std::move(callback), bytes_sent, total_bytes](
std::shared_ptr<TcpSocket> self) mutable
{
std::shared_ptr<TcpSocket> self) mutable {
self->ready_to_send(message, std::move(callback), bytes_sent, total_bytes);
});
}
Expand Down Expand Up @@ -374,8 +370,7 @@ void TcpSocket<EndpointType>::ready_to_receive(ReceiveCompletion &&callback, std
socket_service()->notify_when_readable(
this->shared_from_this(),
[callback = std::move(callback),
received = std::move(received)](std::shared_ptr<TcpSocket> self) mutable
{
received = std::move(received)](std::shared_ptr<TcpSocket> self) mutable {
self->ready_to_receive(std::move(callback), std::move(received));
});
}
Expand Down
12 changes: 4 additions & 8 deletions fly/net/socket/udp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ bool UdpSocket<EndpointType>::send_async(
service->notify_when_writable(
this->shared_from_this(),
[endpoint, message, callback = std::move(callback)](
std::shared_ptr<UdpSocket> self) mutable
{
std::shared_ptr<UdpSocket> self) mutable {
self->ready_to_send(endpoint, message, std::move(callback), 0, message.size());
});

Expand Down Expand Up @@ -174,8 +173,7 @@ bool UdpSocket<EndpointType>::receive_async(ReceiveCompletion &&callback)
{
service->notify_when_readable(
this->shared_from_this(),
[callback = std::move(callback)](std::shared_ptr<UdpSocket> self) mutable
{
[callback = std::move(callback)](std::shared_ptr<UdpSocket> self) mutable {
self->ready_to_receive(std::move(callback), std::string());
});

Expand Down Expand Up @@ -213,8 +211,7 @@ void UdpSocket<EndpointType>::ready_to_send(
socket_service()->notify_when_writable(
this->shared_from_this(),
[endpoint, message, callback = std::move(callback), bytes_sent, total_bytes](
std::shared_ptr<UdpSocket> self) mutable
{
std::shared_ptr<UdpSocket> self) mutable {
self->ready_to_send(
endpoint,
message,
Expand Down Expand Up @@ -255,8 +252,7 @@ void UdpSocket<EndpointType>::ready_to_receive(ReceiveCompletion &&callback, std
socket_service()->notify_when_readable(
this->shared_from_this(),
[callback = std::move(callback),
received = std::move(received)](std::shared_ptr<UdpSocket> self) mutable
{
received = std::move(received)](std::shared_ptr<UdpSocket> self) mutable {
self->ready_to_receive(std::move(callback), std::move(received));
});
}
Expand Down
3 changes: 1 addition & 2 deletions fly/parser/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ fly::json_string_type JsonParser::consume_value()
{
fly::json_string_type value;

auto keep_parsing = [this](Token token) -> bool
{
auto keep_parsing = [this](Token token) -> bool {
switch (token)
{
case Token::Comma:
Expand Down
3 changes: 1 addition & 2 deletions fly/path/mac/path_monitor_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ ino_t get_inode_id(const std::filesystem::path &path)
auto path_it = std::find_if(
m_path_info.begin(),
m_path_info.end(),
[&inode_id](const PathInfoMap::value_type &value) -> bool
{
[&inode_id](const PathInfoMap::value_type &value) -> bool {
const auto *info = static_cast<PathInfoImpl *>(value.second.get());
return info->m_inode_id == inode_id;
});
Expand Down
3 changes: 1 addition & 2 deletions fly/path/nix/path_monitor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ void PathMonitorImpl::handle_event(const inotify_event *event) const
auto path_it = std::find_if(
m_path_info.begin(),
m_path_info.end(),
[&event](const PathInfoMap::value_type &value) -> bool
{
[&event](const PathInfoMap::value_type &value) -> bool {
const auto *info = static_cast<PathInfoImpl *>(value.second.get());
return info->m_watch_descriptor == event->wd;
});
Expand Down
3 changes: 1 addition & 2 deletions fly/path/path_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ bool PathMonitor::poll_paths_later()
return false;
}

auto task = [](std::shared_ptr<PathMonitor> self)
{
auto task = [](std::shared_ptr<PathMonitor> self) {
self->poll(self->m_config->poll_interval());
self->poll_paths_later();
};
Expand Down
3 changes: 1 addition & 2 deletions fly/path/path_monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ struct fly::Formatter<fly::path::PathEvent>
template <typename FormatContext>
void format(fly::path::PathEvent event, FormatContext &context)
{
auto append = [&context](std::string_view value)
{
auto append = [&context](std::string_view value) {
for (auto ch : value)
{
context.out()++ = ch;
Expand Down
3 changes: 1 addition & 2 deletions fly/system/system_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ bool SystemMonitor::poll_system_later()
return false;
}

auto task = [](std::shared_ptr<SystemMonitor> self)
{
auto task = [](std::shared_ptr<SystemMonitor> self) {
self->update_system_cpu_count();
self->update_system_cpu_usage();
self->update_process_cpu_usage();
Expand Down
Loading

0 comments on commit 8ffd932

Please sign in to comment.