clang-format

This commit is contained in:
2025-02-27 16:40:36 +08:00
parent 282b17d7ca
commit 599547e1ae
10 changed files with 347 additions and 103 deletions

198
.clang-format Normal file
View File

@@ -0,0 +1,198 @@
# # https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# # https://clang-format-configurator.site
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 80
AlignAfterOpenBracket: AlwaysBreak # Align, DontAlign, AlwaysBreak, BlockIndent
AlignArrayOfStructures: Right # Left, Right
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignEscapedNewlines: Right # DontAlign, Left, Right
AllowShortCaseLabelsOnASingleLine: true
AlignConsecutiveShortCaseStatements: # AllowShortCaseLabelsOnASingleLine: true
Enabled: false
BreakBeforeBinaryOperators: true
AlignOperands: AlignAfterOperator # Align, AlignAfterOperator
AlignTrailingComments:
Kind: Always # Leave, Always, Never
OverEmptyLines: 1
AllowShortBlocksOnASingleLine: Never # Never, Empty, Always
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None # None, InlineOnly, Empty, Inline, All
AllowShortIfStatementsOnASingleLine: Never # Never, WithoutElse, OnlyFirstIf, AllIfsAndElse
AllowShortLambdasOnASingleLine: None # None, Empty, Inline, All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: All # None, All, TopLevel, AllDefinitions, TopLevelDefinitions
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes # No, MultiLine, Yes
# AttributeMacros # TODO
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both # Both, None, Before, After
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: MultiLine # Never, MultiLine, Always
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
# BracedInitializerIndentWidth: 4
BreakAfterAttributes: Always # Always, Leave, Never
# BreakBeforeBinaryOperators # TODO
# BreakBeforeConceptDeclarations # TODO
# BreakBeforeInlineASMColon # TODO
BreakBeforeTernaryOperators: true
ConstructorInitializerIndentWidth: 2
BreakConstructorInitializers: BeforeComma # BeforeColon, BeforeComma, AfterColon
BreakInheritanceList: BeforeComma # BeforeColon, BeforeComma, AfterColon, AfterComma
BreakStringLiterals: true
# ColumnLimit # TODO
# CommentPragmas # TODO
CompactNamespaces: false
# ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
# DisableFormat: false
EmptyLineAfterAccessModifier: Never # Never, Leave, Always
EmptyLineBeforeAccessModifier: Always # Never, Leave, LogicalBlock, Always
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
# ForEachMacros:
# IfMacros:
IncludeBlocks: Preserve # Preserve, Merge, Regroup
# IncludeCategories: # TODO
# IncludeIsMainRegex
# IncludeIsMainSourceRegex
AccessModifierOffset: -2
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: NoIndent # AfterExternBlock, NoIndent, Indent
IndentGotoLabels: false
IndentPPDirectives: None # None, AfterHash, BeforeHash
IndentRequiresClause: true # C++
# IndentWrappedFunctionNames: false
InsertBraces: true
InsertNewlineAtEOF: true
# InsertTrailingCommas: #TODO
# works only for C++14 and C23
# IntegerLiteralSeparator:
# Binary: 4
# Decimal: 3
# Hex: 2
IntegerLiteralSeparator:
Binary: -1
Decimal: -1
Hex: -1
KeepEmptyLinesAtEOF: false
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature # Signature, OuterScope
# LineEnding:
# MacroBlockBegin:
# MacroBlockEnd:
# Macros:
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None # None, Inner, All
# NamespaceMacros:
PPIndentWidth: -1
PackConstructorInitializers: Never # Never, BinPack, CurrentLine, NextLine, NextLineOnly
# PenaltyBreakAssignment:
# PenaltyBreakBeforeFirstCallParameter:
# PenaltyBreakComment
# PenaltyBreakFirstLessLess
# PenaltyBreakOpenParenthesis
# PenaltyBreakString
# PenaltyBreakTemplateDeclaration
# PenaltyExcessCharacter
# PenaltyIndentedWhitespace
# PenaltyReturnTypeOnItsOwnLine
PointerAlignment: Left
QualifierAlignment: Left
# QualifierOrder: TODO
# RawStringFormats:
ReferenceAlignment: Left
ReflowComments: true
RemoveBracesLLVM: false
RemoveParentheses: ReturnStatement
RemoveSemicolon: true
# RequiresClausePositionStyle: # TODO, C++
# RequiresExpressionIndentation: # TODO, C++
SeparateDefinitionBlocks: Always
# ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInContainerLiterals: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
# SpacesInParensOptions:
SpacesInSquareBrackets: false
# StatementAttributeLikeMacros:
# StatementMacros:
# TypeNames:
# TypenameMacros:
# WhitespaceSensitiveMacros:

View File

@@ -7,14 +7,15 @@
#include "ipc.hpp"
namespace ipc
{
namespace ipc {
#define set_message(verbose, s, ...) { \
#define set_message(verbose, s, ...) \
{ \
m_message = fmt::format(s __VA_OPT__(, ) __VA_ARGS__); \
if (verbose < 0) { \
throw std::runtime_error(m_message); \
} else if (verbose <= m_verbose) { \
} \
else if (verbose <= m_verbose) { \
fmt::print(m_message); \
fmt::print("\n"); \
} \
@@ -36,9 +37,13 @@ start_external(const std::string& executable, bool is_server)
}
void
start_external(const std::string& executable, const std::string& ipc_name, bool is_server)
start_external(
const std::string& executable,
const std::string& ipc_name,
bool is_server)
{
auto command = fmt::format("{} {} {}", executable, ipc_name, is_server ? "1" : "0");
auto command
= fmt::format("{} {} {}", executable, ipc_name, is_server ? "1" : "0");
fmt::print("execute: {}\n", command);
auto thread_func = [command] {
@@ -67,17 +72,29 @@ Ipc::write(const void* buffer, size_t count, size_t size)
ssize_t bytes_sent = 0;
do {
bytes_current = _write(&bytes_buffer[bytes_sent], bytes_required - bytes_sent);
bytes_current
= _write(&bytes_buffer[bytes_sent], bytes_required - bytes_sent);
bytes_sent += bytes_current;
// set_message(2, "write {} -> {}/{}\n", bytes_current, bytes_sent, bytes_required);
// set_message(2, "write {} -> {}/{}\n", bytes_current, bytes_sent,
// bytes_required);
} while ((bytes_current > 0) && (bytes_sent != bytes_required));
if (bytes_sent != bytes_required) {
set_message(-1, "c : (-) write ({} * {}) = {} bytes error: only {} bytes are sent",
count, size, bytes_required, bytes_sent);
set_message(
-1,
"c : (-) write ({} * {}) = {} bytes error: only {} bytes are sent",
count,
size,
bytes_required,
bytes_sent);
}
set_message(1, "c : (+) write ({} * {}) = {} bytes", count, size, bytes_required);
set_message(
1,
"c : (+) write ({} * {}) = {} bytes",
count,
size,
bytes_required);
}
void
@@ -94,17 +111,35 @@ Ipc::read(void* buffer, size_t count, size_t size)
ssize_t bytes_received = 0;
do {
bytes_current = _read(&bytes_buffer[bytes_received], bytes_required - bytes_received);
bytes_current = _read(
&bytes_buffer[bytes_received],
bytes_required - bytes_received);
bytes_received += bytes_current;
set_message(2, "read {} -> {}/{}\n", bytes_current, bytes_received, bytes_required);
set_message(
2,
"read {} -> {}/{}\n",
bytes_current,
bytes_received,
bytes_required);
} while ((bytes_current > 0) && (bytes_received != bytes_required));
if (bytes_received != bytes_required) {
set_message(-1, "c : (-) read ({} * {}) = {} bytes error: only {} bytes are recieved",
count, size, bytes_required, bytes_received);
set_message(
-1,
"c : (-) read ({} * {}) = {} bytes error: only {} bytes are "
"recieved",
count,
size,
bytes_required,
bytes_received);
}
set_message(1, "c : (+) read ({} * {}) = {} bytes", size, count, bytes_required);
set_message(
1,
"c : (+) read ({} * {}) = {} bytes",
size,
count,
bytes_required);
}
void
@@ -123,4 +158,4 @@ Ipc::read(char* str)
read(str, 1, len);
}
}
} // namespace ipc

View File

@@ -9,8 +9,7 @@
#include <windows.h>
#endif
namespace ipc
{
namespace ipc {
void
init();
@@ -22,7 +21,10 @@ void
start_external(const std::string& executable, bool is_server);
void
start_external(const std::string& executable, const std::string& ipc_name, bool is_server);
start_external(
const std::string& executable,
const std::string& ipc_name,
bool is_server);
class Ipc
{
@@ -92,4 +94,4 @@ protected:
#endif
};
}
} // namespace ipc

View File

@@ -107,8 +107,16 @@ ipc_read(void* ipc, T* data, size_t count)
i->read(data, count);
}
#define _IPC_WRITE_FN(data_type) _IPC_WRITE_FN_NAME(data_type) { return ipc_write(ipc, data, count); }
#define _IPC_READ_FN(data_type) _IPC_READ_FN_NAME (data_type) { return ipc_read (ipc, data, count); }
#define _IPC_WRITE_FN(data_type) \
_IPC_WRITE_FN_NAME(data_type) \
{ \
return ipc_write(ipc, data, count); \
}
#define _IPC_READ_FN(data_type) \
_IPC_READ_FN_NAME(data_type) \
{ \
return ipc_read(ipc, data, count); \
}
_IPC_WRITE_FN(char);
_IPC_WRITE_FN(int);

View File

@@ -40,8 +40,10 @@ ipc_write_string(void* ipc, const char* str);
void
ipc_read_string(void* ipc, char* str);
#define _IPC_WRITE_FN_NAME(type) void ipc_write_ ## type (void* ipc, const type * data, size_t count)
#define _IPC_READ_FN_NAME(type) void ipc_read_ ## type (void* ipc, type * data, size_t count)
#define _IPC_WRITE_FN_NAME(type) \
void ipc_write_##type(void* ipc, const type* data, size_t count)
#define _IPC_READ_FN_NAME(type) \
void ipc_read_##type(void* ipc, type* data, size_t count)
_IPC_WRITE_FN_NAME(char);
_IPC_WRITE_FN_NAME(int);

View File

@@ -8,8 +8,7 @@
#include "ipc.hpp"
namespace ipc
{
namespace ipc {
std::string
default_name()
@@ -20,8 +19,7 @@ default_name()
void
make_fifo(const std::string& name, unsigned int mode)
{
if (mkfifo(name.c_str(), mode) < 0)
{
if (mkfifo(name.c_str(), mode) < 0) {
auto message = fmt::format("mkfifo '{}' : {}", name, strerror(errno));
throw std::runtime_error(message);
}
@@ -50,7 +48,8 @@ Ipc::Ipc(const std::string& name, bool is_server, int verbose)
if (is_server) {
m_reader_fd = fifo_open(name + "_c", O_RDONLY);
m_writer_fd = fifo_open(name + "_s", O_WRONLY);
} else {
}
else {
m_writer_fd = fifo_open(name + "_c", O_WRONLY);
m_reader_fd = fifo_open(name + "_s", O_RDONLY);
}
@@ -72,4 +71,4 @@ Ipc::_read(void* buffer, size_t bytes)
return ::read(m_reader_fd, buffer, bytes);
}
}
} // namespace ipc

View File

@@ -11,8 +11,7 @@
#define UNUSED(x) (void)(x)
namespace ipc
{
namespace ipc {
// https://stackoverflow.com/a/17387176
// Returns the last Win32 error, in string format.
@@ -30,14 +29,14 @@ getLastErrorAsString()
// Ask Win32 to give us the string version of that message ID.
// The parameters we pass in, tell Win32 to create the buffer that holds the
// message for us (because we don't yet know how long the message string will be).
// message for us (because we don't yet know how long the message string
// will be).
//
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea
//
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, // dwFlags
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS, // dwFlags
NULL, // lpSource
errorMessageID, // dwMessageId
@@ -83,7 +82,8 @@ Ipc::Ipc(const std::string& name, bool is_server, int verbose)
// fmt::print("full name = {}\n", full_name);
auto throw_failed = [](const std::string& name) {
auto message = fmt::format("{}() failed: {}", name, getLastErrorAsString());
auto message
= fmt::format("{}() failed: {}", name, getLastErrorAsString());
throw std::runtime_error(message);
};
@@ -186,4 +186,4 @@ Ipc::_read(void* buffer, size_t bytes)
return numBytesRead;
}
}
} // namespace ipc

View File

@@ -9,9 +9,9 @@
int
main(int /* argc */, char** /* argv */)
{
using ipc::Ipc;
using fmt::format;
using fmt::print;
using ipc::Ipc;
using namespace std::chrono_literals;
int verbose = 0;

View File

@@ -9,9 +9,9 @@
int
main(int /* argc */, char** /* argv */)
{
using ipc::Ipc;
using fmt::format;
using fmt::print;
using ipc::Ipc;
using namespace std::chrono_literals;
int verbose = 0;