diff --git a/include/dmn-async.hpp b/include/dmn-async.hpp index a87a23e4..79f2f396 100644 --- a/include/dmn-async.hpp +++ b/include/dmn-async.hpp @@ -183,8 +183,8 @@ class Dmn_Async { * @return shared_ptr Rendezvous object for task completion. */ template - auto addExecTaskWithWait(Callable &&func, Args &&...args) - -> std::shared_ptr; + auto addExecTaskWithWait(Callable &&func, + Args &&...args) -> std::shared_ptr; /** * @brief Schedule a callable task to run after the given duration has @@ -223,8 +223,8 @@ class Dmn_Async { template auto addExecTaskAfterWithWait(const std::chrono::duration &duration, - Callable &&func, Args &&...args) - -> std::shared_ptr; + Callable &&func, + Args &&...args) -> std::shared_ptr; /** * @brief Block until the async is empty and no task pending to be executed. diff --git a/include/dmn-blockingqueue-lf.hpp b/include/dmn-blockingqueue-lf.hpp index b912ef77..aa409751 100644 --- a/include/dmn-blockingqueue-lf.hpp +++ b/include/dmn-blockingqueue-lf.hpp @@ -252,9 +252,9 @@ class Dmn_BlockingQueue_Lf * @return An optional containing the data from the head of the queue, or * std::nullopt if the queue is empty and wait is false. */ - virtual auto popOptional(bool wait, - const Inflight_Guard_Ticket &inflightTicket) - -> std::optional; + virtual auto + popOptional(bool wait, + const Inflight_Guard_Ticket &inflightTicket) -> std::optional; /** * @brief Wrapper call to pushImpl to copy and enqueue the item into the @@ -459,8 +459,8 @@ auto dmn::Dmn_BlockingQueue_Lf::freeRetiredNodeList(Node *head) -> uint64_t { } template -auto Dmn_BlockingQueue_Lf::pop(size_t count, long timeout) - -> std::vector { +auto Dmn_BlockingQueue_Lf::pop(size_t count, + long timeout) -> std::vector { assert(count > 0); auto inflightTicket = this->enterInflightGate(); @@ -506,8 +506,8 @@ auto Dmn_BlockingQueue_Lf::popOptional(bool wait) -> std::optional { template auto Dmn_BlockingQueue_Lf::popOptional( - bool wait, const Inflight_Guard_Ticket &inflightTicket) - -> std::optional { + bool wait, + const Inflight_Guard_Ticket &inflightTicket) -> std::optional { std::optional res{}; do { diff --git a/include/dmn-blockingqueue-mt.hpp b/include/dmn-blockingqueue-mt.hpp index e0f1c50b..794e1f7b 100644 --- a/include/dmn-blockingqueue-mt.hpp +++ b/include/dmn-blockingqueue-mt.hpp @@ -343,8 +343,8 @@ template auto Dmn_BlockingQueue_Mt::waitForEmpty() -> uint64_t { } template -auto Dmn_BlockingQueue_Mt::pop(size_t count, long timeout) - -> std::vector { +auto Dmn_BlockingQueue_Mt::pop(size_t count, + long timeout) -> std::vector { std::vector ret{}; assert(count > 0); diff --git a/include/dmn-blockingqueue.hpp b/include/dmn-blockingqueue.hpp index eb9a5a80..bd962b73 100644 --- a/include/dmn-blockingqueue.hpp +++ b/include/dmn-blockingqueue.hpp @@ -190,8 +190,8 @@ template class Dmn_BlockingQueue { }; // class Dmn_BlockingQueue template -inline auto Dmn_BlockingQueue::pop(std::size_t count, long timeout) - -> std::vector { +inline auto Dmn_BlockingQueue::pop(std::size_t count, + long timeout) -> std::vector { return static_cast(this)->pop(count, timeout); } diff --git a/include/dmn-dmesg.hpp b/include/dmn-dmesg.hpp index 4dc2cf63..88f7bf0d 100644 --- a/include/dmn-dmesg.hpp +++ b/include/dmn-dmesg.hpp @@ -588,8 +588,8 @@ class Dmn_DMesg : public Dmn_Pub { * * @return last message of topic or nullptr if no message for such topic. */ - auto getTopicLastMessage(std::string_view topic) - -> std::optional; + auto + getTopicLastMessage(std::string_view topic) -> std::optional; /** * @brief Reset conflict state by posting last message of the topic. diff --git a/include/dmn-dmesgnet.hpp b/include/dmn-dmesgnet.hpp index 7123d817..5b267612 100644 --- a/include/dmn-dmesgnet.hpp +++ b/include/dmn-dmesgnet.hpp @@ -185,9 +185,9 @@ class Dmn_DMesgNet : public Dmn_DMesg { ///< acknowledgement. long long m_master_sync_pending_counter{}; ///< Cycles spent waiting for ///< master synchronisation. - struct timeval - m_last_remote_master_timestamp{}; ///< Timestamp of the last received - ///< master heartbeat. + struct timeval m_last_remote_master_timestamp { + }; ///< Timestamp of the last received + ///< master heartbeat. std::unordered_map m_topic_last_dmesgpb{}; ///< Last published message per topic. std::unordered_map> diff --git a/include/dmn-pipe.hpp b/include/dmn-pipe.hpp index 6709029e..ccac4119 100644 --- a/include/dmn-pipe.hpp +++ b/include/dmn-pipe.hpp @@ -171,8 +171,8 @@ class Dmn_Pipe : public Dmn_Io, private QueueType, private Dmn_Proc { * * @return The number of items read and processed. */ - auto readAndProcess(Dmn_Pipe::Task fn, size_t count = 1, long timeout = 0) - -> size_t; + auto readAndProcess(Dmn_Pipe::Task fn, size_t count = 1, + long timeout = 0) -> size_t; /** * @brief Write (copy) an item into the pipe. @@ -294,8 +294,8 @@ auto Dmn_Pipe::read() -> std::optional { } template -auto Dmn_Pipe::read(size_t count, long timeout) - -> std::vector { +auto Dmn_Pipe::read(size_t count, + long timeout) -> std::vector { std::vector dataList{}; readAndProcess([&dataList](T &&item) { dataList.push_back(std::move(item)); }, diff --git a/run-clang-format.sh b/scripts/run-tab-check-and-clang-format.sh similarity index 56% rename from run-clang-format.sh rename to scripts/run-tab-check-and-clang-format.sh index 043a5c52..7a214fa7 100755 --- a/run-clang-format.sh +++ b/scripts/run-tab-check-and-clang-format.sh @@ -1,17 +1,40 @@ #!/bin/sh # -# Copyright © 2023 Chee Bin HOH. All rights reserved. +# Copyright © 2023 - 2026 Chee Bin HOH. All rights reserved. # +ROOT_DIR="." + +for arg in "$@"; do + case "$arg" in + --root=*) + ROOT_DIR="${arg#--root=}" + ;; + *) + echo "Usage: $0 [--root=dir]" >&2 + exit 2 + ;; + esac +done + +if [ ! -d "$ROOT_DIR" ]; then + echo "Error: root directory '$ROOT_DIR' does not exist." >&2 + exit 2 +fi + +cd "$ROOT_DIR" || exit 2 + DIRS=". include src include/kafka src/kafka test" FILE_PATTERN='*.cpp *.hpp' -# extra space following newline is never intended to be checked in, so we trim it. +# Extra space following newline is never intended to be checked in, so we trim it. # # Tab at the beginning of lines are not consistent cross IDE, it is particular # annoying for source files saved in visual studio kind of IDE and reopen in # vi. +ROOT_DIR=`pwd` + echo "******** check for tab character..." has_invalid_tab="" @@ -26,32 +49,26 @@ for d in `echo ${DIRS}`; do fi done - cd - &>/dev/null + cd $ROOT_DIR done -if [ "$has_invalid_tab" == "yes" ]; then +if [ "$has_invalid_tab" = "yes" ]; then exit 1 fi # clang-format the source files -if which clang-format &>/dev/null; then +if which clang-format >/dev/null; then echo "******** perform clang-format..." for d in `echo ${DIRS}`; do cd ${d}; for f in `ls ${FILE_PATTERN} 2>/dev/null`; do - clang-format --style=LLVM ${f} > ${f}_tmp - if ! diff $f ${f}_tmp &>/dev/null; then - echo "- formatting ${f}..." - cp ${f}_tmp ${f} - fi - - rm ${f}_tmp + clang-format -i --style=LLVM ${f} done - cd - &>/dev/null + cd $ROOT_DIR done +else + echo "Error: clang-format is not found, skip it..." fi - - diff --git a/src/dmn-dmesg.cpp b/src/dmn-dmesg.cpp index c57ff5e0..e6719d3f 100644 --- a/src/dmn-dmesg.cpp +++ b/src/dmn-dmesg.cpp @@ -402,9 +402,8 @@ void Dmn_DMesg::Dmn_DMesgHandler::write(const dmn::DMesgPb &dmesgpb, * @param flags Additional WriteOptions (kForce, etc.). * @return true if write succeeded without conflict, false otherwise. */ -auto Dmn_DMesg::Dmn_DMesgHandler::writeAndCheckConflict(dmn::DMesgPb &&dmesgpb, - WriteFlags flags) - -> bool { +auto Dmn_DMesg::Dmn_DMesgHandler::writeAndCheckConflict( + dmn::DMesgPb &&dmesgpb, WriteFlags flags) -> bool { std::string topic = dmesgpb.topic(); flags.set(kBlock); @@ -421,9 +420,8 @@ auto Dmn_DMesg::Dmn_DMesgHandler::writeAndCheckConflict(dmn::DMesgPb &&dmesgpb, * @param flags Additional WriteOptions (kForce, etc.). * @return true if write succeeded without conflict, false otherwise. */ -auto Dmn_DMesg::Dmn_DMesgHandler::writeAndCheckConflict(dmn::DMesgPb &dmesgpb, - WriteFlags flags) - -> bool { +auto Dmn_DMesg::Dmn_DMesgHandler::writeAndCheckConflict( + dmn::DMesgPb &dmesgpb, WriteFlags flags) -> bool { std::string topic = dmesgpb.topic(); flags.set(kBlock); @@ -454,7 +452,7 @@ void Dmn_DMesg::Dmn_DMesgHandler::writeDMesgInternal(dmn::DMesgPb &dmesgpb, "handler needs to be reset"); } - struct timeval tval{}; + struct timeval tval {}; gettimeofday(&tval, nullptr); DMESG_PB_SET_MSG_TIMESTAMP_FROM_TV(dmesgpb, tval); diff --git a/src/dmn-dmesgnet.cpp b/src/dmn-dmesgnet.cpp index 6e604ab8..2e44bbcc 100644 --- a/src/dmn-dmesgnet.cpp +++ b/src/dmn-dmesgnet.cpp @@ -70,7 +70,7 @@ Dmn_DMesgNet::Dmn_DMesgNet(std::string_view name, m_output_handler{std::move(output_handler)} { // Initialize the DMesgNet state - struct timeval tval{}; + struct timeval tval {}; gettimeofday(&tval, nullptr); DMESG_PB_SET_MSG_TOPIC(this->m_sys, kDMesgSysIdentifier); @@ -143,7 +143,7 @@ Dmn_DMesgNet::~Dmn_DMesgNet() noexcept try { // we avoid use of m_sys_handler as we are to destroy it, so we // do not want to hold the object life up and have to wait for // asynchrononous action to send last heartbeat messge. - struct timeval tval{}; + struct timeval tval {}; gettimeofday(&tval, nullptr); DMESG_PB_SET_MSG_SOURCEIDENTIFIER(this->m_sys, this->m_name); @@ -400,7 +400,7 @@ void Dmn_DMesgNet::createTimerProc() { DMESG_PB_SYS_NODE_SET_STATE(self, dmn::DMesgStatePb::Ready); DMESG_PB_SYS_NODE_SET_MASTERIDENTIFIER(self, this->m_name); - struct timeval tval{}; + struct timeval tval {}; gettimeofday(&tval, nullptr); DMESG_PB_SYS_NODE_SET_UPDATEDTIMESTAMP_FROM_TV(self, tval); @@ -515,7 +515,7 @@ void Dmn_DMesgNet::reconciliateDMesgPbSys(const dmn::DMesgPb &dmesgpb_other) { auto other = dmesgpb_other.body().sys().self(); auto *self = this->m_sys.mutable_body()->mutable_sys()->mutable_self(); - struct timeval tval{}; + struct timeval tval {}; gettimeofday(&tval, nullptr); if (self->state() == dmn::DMesgStatePb::MasterPending && diff --git a/src/dmn-runtime.cpp b/src/dmn-runtime.cpp index c9af7418..1f36339c 100644 --- a/src/dmn-runtime.cpp +++ b/src/dmn-runtime.cpp @@ -48,7 +48,7 @@ auto Dmn_Runtime_Manager_Impl_create() -> Dmn_Runtime_Manager_Impl * { auto *impl = new Dmn_Runtime_Manager_Impl; #ifdef _POSIX_TIMERS - struct sigevent sev{}; + struct sigevent sev {}; // 1. Setup signal delivery sev.sigev_notify = SIGEV_SIGNAL; @@ -152,7 +152,7 @@ void Dmn_Runtime_Manager_Impl_setNextTimer(Dmn_Runtime_Manager_Impl *impl, assert(impl->m_timer_created); #ifdef _POSIX_TIMERS - struct itimerspec its{}; + struct itimerspec its {}; its.it_value.tv_sec = sec; its.it_value.tv_nsec = nsec; @@ -165,7 +165,7 @@ void Dmn_Runtime_Manager_Impl_setNextTimer(Dmn_Runtime_Manager_Impl *impl, std::system_category().message(errno)); } #else /* _POSIX_TIMERS */ - struct itimerval timer{}; + struct itimerval timer {}; timer.it_value.tv_sec = sec; timer.it_value.tv_usec = nsec / 1000; diff --git a/src/dmn-socket.cpp b/src/dmn-socket.cpp index 3f366ea4..e7037e2a 100644 --- a/src/dmn-socket.cpp +++ b/src/dmn-socket.cpp @@ -45,7 +45,7 @@ namespace dmn { Dmn_Socket::Dmn_Socket(std::string_view ip4, int port_no, bool write_only) : m_ip4{ip4}, m_port_no{port_no}, m_write_only{write_only} { constexpr int broadcast{1}; - struct sockaddr_in servaddr{}; + struct sockaddr_in servaddr {}; const int type{SOCK_DGRAM}; m_fd = socket(AF_INET, type, 0); @@ -111,7 +111,7 @@ void Dmn_Socket::write(const std::string &item) { * as a member value per object to avoid reconstructing it on every * write call. */ - struct sockaddr_in servaddr{}; + struct sockaddr_in servaddr {}; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(m_port_no); diff --git a/test/dmn-test-async-queue-lf.cpp b/test/dmn-test-async-queue-lf.cpp index ea12585e..706760ad 100644 --- a/test/dmn-test-async-queue-lf.cpp +++ b/test/dmn-test-async-queue-lf.cpp @@ -82,8 +82,8 @@ int main(int argc, char *argv[]) { waitHandler->wait(); EXPECT_TRUE(done); - struct sigaction sact{}; - struct itimerval timer{}; + struct sigaction sact {}; + struct itimerval timer {}; // Install timer_handler as the signal handler for SIGALRM sact.sa_handler = &timer_handler; diff --git a/test/dmn-test-async.cpp b/test/dmn-test-async.cpp index b068f961..25f7d4f9 100644 --- a/test/dmn-test-async.cpp +++ b/test/dmn-test-async.cpp @@ -79,8 +79,8 @@ int main(int argc, char *argv[]) { waitHandler->wait(); EXPECT_TRUE(done); - struct sigaction sact{}; - struct itimerval timer{}; + struct sigaction sact {}; + struct itimerval timer {}; // Install timer_handler as the signal handler for SIGALRM sact.sa_handler = &timer_handler;