From 932ca51baefa3a10ff2fd5e273d7185929084209 Mon Sep 17 00:00:00 2001 From: dekken Date: Sun, 19 Nov 2023 13:48:43 +0100 Subject: [PATCH] formatting/updates/etc --- .clang-format | 3 + .github/workflows/build_nix.yml | 6 +- .github/workflows/build_osx.yml | 6 +- .sublime-project | 17 ++ .sublime-project.sublime-workspace | 3 + .travis.yml | 38 ---- appveyor.yml | 19 -- inc/mkn/kul/all.hpp | 45 ++--- inc/mkn/kul/alloc.hpp | 8 +- inc/mkn/kul/asio/log.hpp | 39 ++-- inc/mkn/kul/assert.hpp | 2 +- inc/mkn/kul/bon.hpp | 20 +- inc/mkn/kul/byte.hpp | 14 +- inc/mkn/kul/cli.hpp | 89 ++++----- inc/mkn/kul/dbg.hpp | 22 +-- inc/mkn/kul/env.hpp | 8 +- inc/mkn/kul/except.hpp | 50 +++-- inc/mkn/kul/for.hpp | 66 ++++++- inc/mkn/kul/hash.hpp | 2 +- inc/mkn/kul/io.hpp | 74 +++---- inc/mkn/kul/log.hpp | 73 ++++--- inc/mkn/kul/map.hpp | 60 +++--- inc/mkn/kul/math.hpp | 29 +-- inc/mkn/kul/math/blas.hpp | 16 +- inc/mkn/kul/math/noblas.hpp | 8 +- inc/mkn/kul/math/noop.hpp | 42 ++-- inc/mkn/kul/os.hpp | 116 +++++------ inc/mkn/kul/os/bsd/proc.os.hpp | 4 +- inc/mkn/kul/os/nix/proc.os.hpp | 6 +- inc/mkn/kul/os/nixish/env.hpp | 10 +- inc/mkn/kul/os/nixish/ipc.hpp | 19 +- inc/mkn/kul/os/nixish/os.bot.hpp | 18 +- inc/mkn/kul/os/nixish/os.top.hpp | 2 +- inc/mkn/kul/os/nixish/proc.hpp | 12 +- inc/mkn/kul/os/nixish/signal.hpp | 18 +- inc/mkn/kul/os/nixish/src/os/dir/dirs.ipp | 4 +- inc/mkn/kul/os/nixish/src/os/dir/files.ipp | 10 +- inc/mkn/kul/os/nixish/src/proc/child.ipp | 4 +- inc/mkn/kul/os/nixish/src/proc/run.ipp | 3 +- inc/mkn/kul/os/nixish/src/signal/handler.ipp | 14 +- .../kul/os/nixish/src/signal/stacktrace.ipp | 5 +- inc/mkn/kul/os/nixish/sys.hpp | 68 +++---- inc/mkn/kul/os/nixish/threads.os.hpp | 17 +- inc/mkn/kul/os/threads.hpp | 12 +- inc/mkn/kul/os/win/cli.os.hpp | 2 +- inc/mkn/kul/os/win/env.hpp | 12 +- inc/mkn/kul/os/win/ipc.hpp | 30 +-- inc/mkn/kul/os/win/os.bot.hpp | 18 +- inc/mkn/kul/os/win/os.top.hpp | 4 +- inc/mkn/kul/os/win/proc.hpp | 18 +- inc/mkn/kul/os/win/signal.hpp | 32 +-- inc/mkn/kul/os/win/src/cli/hidden.cpp | 2 +- inc/mkn/kul/os/win/src/proc/run.cpp | 22 ++- inc/mkn/kul/os/win/src/signal/se_handler.cpp | 8 +- inc/mkn/kul/os/win/sys.hpp | 22 +-- inc/mkn/kul/os/win/threads.os.hpp | 15 +- inc/mkn/kul/proc.hpp | 84 ++++---- inc/mkn/kul/scm.hpp | 49 ++--- inc/mkn/kul/scm/man.hpp | 8 +- inc/mkn/kul/serial/cli.arg.end.hpp | 4 +- inc/mkn/kul/sort.hpp | 178 +++++++++++++++++ inc/mkn/kul/span.hpp | 3 +- inc/mkn/kul/string.hpp | 82 ++++---- inc/mkn/kul/sys.hpp | 3 +- inc/mkn/kul/threads.hpp | 141 ++++++------- inc/mkn/kul/time.hpp | 8 +- inc/mkn/kul/tuple.hpp | 2 +- inc/mkn/kul/wstring.hpp | 18 +- inc/mkn/kul/yaml.hpp | 55 +++--- mkn.yaml | 12 +- {test => res/bench}/bench.cpp | 16 +- res/bench/sort.cpp | 69 +++++++ src/os/win/proc.cpp | 2 +- src/os/win/signal.cpp | 2 +- {test => tst}/test.cpp | 10 +- {test => tst}/test/cli.ipp | 0 {test => tst}/test/except.ipp | 0 {test => tst}/test/io.ipp | 0 {test => tst}/test/math.ipp | 0 {test => tst}/test/os.ipp | 0 {test => tst}/test/proc.ipp | 0 tst/test/sort.ipp | 185 ++++++++++++++++++ {test => tst}/test/span.ipp | 0 {test => tst}/test/string.ipp | 8 +- {test => tst}/usage.cpp | 59 +++--- 85 files changed, 1364 insertions(+), 920 deletions(-) create mode 100644 .sublime-project create mode 100644 .sublime-project.sublime-workspace delete mode 100644 .travis.yml delete mode 100644 appveyor.yml create mode 100644 inc/mkn/kul/sort.hpp rename {test => res/bench}/bench.cpp (89%) create mode 100644 res/bench/sort.cpp rename {test => tst}/test.cpp (92%) rename {test => tst}/test/cli.ipp (100%) rename {test => tst}/test/except.ipp (100%) rename {test => tst}/test/io.ipp (100%) rename {test => tst}/test/math.ipp (100%) rename {test => tst}/test/os.ipp (100%) rename {test => tst}/test/proc.ipp (100%) create mode 100644 tst/test/sort.ipp rename {test => tst}/test/span.ipp (100%) rename {test => tst}/test/string.ipp (96%) rename {test => tst}/usage.cpp (87%) diff --git a/.clang-format b/.clang-format index bbd1790..4835500 100644 --- a/.clang-format +++ b/.clang-format @@ -2,3 +2,6 @@ IndentWidth: 2 BasedOnStyle: 'google' ColumnLimit: 100 SortIncludes: false +UseTab: Never +DerivePointerAlignment: false +PointerAlignment: Left diff --git a/.github/workflows/build_nix.yml b/.github/workflows/build_nix.yml index 0b472ad..900eaa1 100644 --- a/.github/workflows/build_nix.yml +++ b/.github/workflows/build_nix.yml @@ -14,6 +14,6 @@ jobs: - name: "Build/Test" # contains slash so use quotes otherwise UB run: | - wget https://github.com/mkn/mkn/releases/download/latest/mkn_nix - chmod +x mkn_nix - KLOG=3 ./mkn_nix clean build run -dtOp test -a "-std=c++17 -fPIC" + curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_nix + chmod +x mkn + KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC" diff --git a/.github/workflows/build_osx.yml b/.github/workflows/build_osx.yml index 6a28617..641d1f4 100644 --- a/.github/workflows/build_osx.yml +++ b/.github/workflows/build_osx.yml @@ -14,6 +14,6 @@ jobs: - name: "Build/Test" # contains slash so use quotes otherwise UB run: | - wget https://github.com/mkn/mkn/releases/download/latest/mkn_osx - chmod +x mkn_osx - KLOG=3 ./mkn_osx clean build run -dtOp test -a "-std=c++17 -fPIC" + curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_osx + chmod +x mkn + KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC" diff --git a/.sublime-project b/.sublime-project new file mode 100644 index 0000000..45091a2 --- /dev/null +++ b/.sublime-project @@ -0,0 +1,17 @@ +{ + "folders": + [ + { + "path": "." + } + ], + "settings": + { + "ClangFormat": + { + "binary": "clang-format", + "format_on_save": true, + "style": "file" + }, + } +} diff --git a/.sublime-project.sublime-workspace b/.sublime-project.sublime-workspace new file mode 100644 index 0000000..5a07207 --- /dev/null +++ b/.sublime-project.sublime-workspace @@ -0,0 +1,3 @@ +{ + "project": ".sublime-project" +} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fb23a7f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,38 +0,0 @@ - -git: - depth: 3 -matrix: - include: - - os: linux - dist: bionic - sudo: required - compiler: gcc - addons: - apt: - sources: - - sourceline: 'ppa:ubuntu-toolchain-r/test' - packages: - - g++-8 - - libatlas-base-dev - - libblas-dev - env: - - MATRIX_EVAL="CC=gcc-8 && CXX=g++-8" - - os: osx - sudo: required - osx_image: xcode11 - compiler: clang - -script: - - | - export MKN_LIB_LINK_LIB="1" KUL_GIT_CO="--depth 3" KLOG="3" - git clone https://github.com/Dekken/maiken -b master --depth 1 maiken - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then - make bsd -C maiken CXX=clang++ && cp maiken/mkn . - ./mkn build -dtKa "-O2 -std=c++17" -l "-pthread -ldl" -p test run - fi - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - eval "${MATRIX_EVAL}" - export CC CXX - make nix -C maiken && cp maiken/mkn . - ./mkn build -dtKa "-D_MKN_KUL_USE_CBLAS -O2 -std=c++17 -fPIC" -l "-pthread -ldl -lblas" -p test run - fi diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index bcc4415..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,19 +0,0 @@ -version: 0.1.{build} -image: Visual Studio 2019 -init: git config --global core.autocrlf input -clone_depth: 1 -hosts: - queue-server: 127.0.0.1 - db.server.com: 127.0.0.2 -platform: x64 -configuration: Release -cache: - - C:\ProgramData\chocolatey\bin -> appveyor.yml - - C:\ProgramData\chocolatey\lib -> appveyor.yml -build_script: - - SET PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\Hostx64\x64;C:\Program Files\Git\usr\bin;%PATH% - - call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" - - curl -Lo mkn.exe https://github.com/Dekken/maiken/raw/binaries/win10_x64/mkn.exe - - SET MKN_CL_PREFERRED=1 - - rm "C:\Program Files\Git\usr\bin\link.exe" # messes with msvc link.exe - - bash -c "PATH=\"$PWD:/c/Python36-x64/:/c/Program Files/Git/usr/bin:$PATH\"; ./res/ci/appveyor.sh" diff --git a/inc/mkn/kul/all.hpp b/inc/mkn/kul/all.hpp index 24acd29..84788a6 100644 --- a/inc/mkn/kul/all.hpp +++ b/inc/mkn/kul/all.hpp @@ -36,46 +36,31 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include "mkn/kul/span.hpp" + namespace mkn { namespace kul { -#define PRINT_LINE() std::cout << __LINE__ << std::endl - -namespace func { -template -using check = void; -} - -template -struct is_span_like : std::false_type {}; - -template -struct is_span_like().data())>, - func::check().size())>> : std::true_type {}; - -template -auto constexpr is_span_like_v = is_span_like::value; - template -void for_each(Container& container, Function && function) { +void for_each(Container& container, Function&& function) { std::for_each(std::begin(container), std::end(container), function); } template -bool any_of(Container const& container, Function && function) { +bool any_of(Container const& container, Function&& function) { return std::any_of(std::begin(container), std::end(container), function); } template -bool any_of(Container && container, Function && function) { +bool any_of(Container&& container, Function&& function) { return std::any_of(std::begin(container), std::end(container), function); } template -bool all_of(Container const& container, Function && function) { +bool all_of(Container const& container, Function&& function) { return std::all_of(std::begin(container), std::end(container), function); } template -bool all_of(Container && container, Function && function) { +bool all_of(Container&& container, Function&& function) { return all_of(container, function); } @@ -90,7 +75,7 @@ bool compare_to(T const& t, OP const& op, Args const&... args) { return ((op(args, t)) && ...); } template -bool compare_to(T && t, OP && op, Args &&... args) { +bool compare_to(T&& t, OP&& op, Args&&... args) { return compare_to(t, op, args...); } @@ -100,7 +85,7 @@ bool compare_to(T const& t, OP const& op, std::tuple const& tu return std::apply([&](auto const&... args) { return compare_to(t, op, args...); }, tuple); } template -bool compare_to(T && t, OP && op, std::tuple && tuple) { +bool compare_to(T&& t, OP&& op, std::tuple&& tuple) { return compare_to(t, op, tuple); } @@ -114,7 +99,7 @@ class All { return are(t, std::equal_to{}); } template - bool operator==(T && t) { + bool operator==(T&& t) { return are(t, std::equal_to{}); } @@ -123,7 +108,7 @@ class All { return are(t, std::not_equal_to{}); } template - bool operator!=(T && t) { + bool operator!=(T&& t) { return are(t, std::not_equal_to{}); } @@ -132,7 +117,7 @@ class All { return are(t, std::greater{}); } template - bool operator>(T && t) { + bool operator>(T&& t) { return are(t, std::greater{}); } @@ -141,7 +126,7 @@ class All { return are(t, std::less{}); } template - bool operator<(T && t) { + bool operator<(T&& t) { return are(t, std::less{}); } @@ -150,7 +135,7 @@ class All { return are(t, std::greater_equal{}); } template - bool operator>=(T && t) { + bool operator>=(T&& t) { return are(t, std::greater_equal{}); } @@ -159,7 +144,7 @@ class All { return are(t, std::less_equal{}); } template - bool operator<=(T && t) { + bool operator<=(T&& t) { return are(t, std::less_equal{}); } diff --git a/inc/mkn/kul/alloc.hpp b/inc/mkn/kul/alloc.hpp index a397b42..7257f59 100644 --- a/inc/mkn/kul/alloc.hpp +++ b/inc/mkn/kul/alloc.hpp @@ -39,6 +39,7 @@ namespace mkn::kul { template class AlignedAllocator { using This = AlignedAllocator; + public: using pointer = T*; using reference = T&; @@ -71,13 +72,10 @@ class AlignedAllocator { deallocate(p); } - - bool operator!=(This const& that) const { - return !(*this == that); - } + bool operator!=(This const& that) const { return !(*this == that); } bool operator==(This const& /*that*/) const { - return true; // stateless + return true; // stateless } }; diff --git a/inc/mkn/kul/asio/log.hpp b/inc/mkn/kul/asio/log.hpp index d9ff0ef..2ab6723 100644 --- a/inc/mkn/kul/asio/log.hpp +++ b/inc/mkn/kul/asio/log.hpp @@ -41,7 +41,8 @@ namespace log { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; } // namespace log @@ -51,20 +52,20 @@ class Logger : public mkn::kul::Logger { private: mkn::kul::ChroncurrentThreadPool<> ctp; - std::function defE, defO; + std::function defE, defO; public: Logger() : ctp(1, 1), - defE([&](std::string const &s) { mkn::kul::Logger::err(s); }), - defO([&](std::string const &s) { mkn::kul::Logger::out(s); }) {} - void err(std::string const &s) override { + defE([&](std::string const& s) { mkn::kul::Logger::err(s); }), + defO([&](std::string const& s) { mkn::kul::Logger::out(s); }) {} + void err(std::string const& s) override { if (e) ctp.async(std::bind(e, s)); else ctp.async(std::bind(defE, s)); } - void out(std::string const &s) override { + void out(std::string const& s) override { if (o) ctp.async(std::bind(o, s)); else @@ -77,7 +78,7 @@ class LogMan : public mkn::kul::ALogMan { LogMan() : ALogMan(new mkn::kul::asio::Logger()) {} public: - static LogMan &INSTANCE() { + static LogMan& INSTANCE() { static LogMan instance; return instance; }; @@ -86,13 +87,13 @@ class LogMan : public mkn::kul::ALogMan { class Message { protected: std::stringstream ss; - const mkn::kul::log::mode &m; + const mkn::kul::log::mode& m; - Message(const mkn::kul::log::mode &_m) : m(_m) {} + Message(const mkn::kul::log::mode& _m) : m(_m) {} public: template - Message &operator<<(const T &s) { + Message& operator<<(const T& s) { ss << s; return *this; } @@ -100,15 +101,15 @@ class Message { class LogMessage : public Message { public: ~LogMessage() { LogMan::INSTANCE().log(f, fn, l, m, ss.str()); } - LogMessage(char const *_f, char const *_fn, uint16_t const &_l, const mkn::kul::log::mode &_m) + LogMessage(char const* _f, char const* _fn, uint16_t const& _l, const mkn::kul::log::mode& _m) : Message(_m), f(_f), fn(_fn), l(_l) {} private: char const *f, *fn; - uint16_t const &l; + uint16_t const& l; }; struct OutMessage : public Message { - OutMessage(const mkn::kul::log::mode &_m = mkn::kul::log::mode::NON) : Message(_m) {} + OutMessage(const mkn::kul::log::mode& _m = mkn::kul::log::mode::NON) : Message(_m) {} ~OutMessage() { LogMan::INSTANCE().out(m, ss.str()); } }; struct ErrMessage : public Message { @@ -116,10 +117,14 @@ struct ErrMessage : public Message { ~ErrMessage() { LogMan::INSTANCE().err(ss.str()); } }; -#define KASIO_LOG_INF mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::INF) -#define KASIO_LOG_ERR mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::ERR) -#define KASIO_LOG_DBG mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::DBG) -#define KASIO_LOG_TRC mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::TRC) +#define KASIO_LOG_INF \ + mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::INF) +#define KASIO_LOG_ERR \ + mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::ERR) +#define KASIO_LOG_DBG \ + mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::DBG) +#define KASIO_LOG_TRC \ + mkn::kul::asio::LogMessage(__FILE__, __func__, __LINE__, mkn::kul::log::mode::TRC) #define KASIO_LOG(sev) KLOG_##sev #define KASIO_OUT_NON mkn::kul::asio::OutMessage() diff --git a/inc/mkn/kul/assert.hpp b/inc/mkn/kul/assert.hpp index f814636..b7239db 100644 --- a/inc/mkn/kul/assert.hpp +++ b/inc/mkn/kul/assert.hpp @@ -40,7 +40,7 @@ namespace kul { struct Assert { template Assert(T t) { - (void) t; + (void)t; #if !defined(NDEBUG) if (!(t)) { auto tid = mkn::kul::this_thread::id(); diff --git a/inc/mkn/kul/bon.hpp b/inc/mkn/kul/bon.hpp index c6a9f59..b221044 100644 --- a/inc/mkn/kul/bon.hpp +++ b/inc/mkn/kul/bon.hpp @@ -39,14 +39,14 @@ namespace kul { namespace bon { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, const size_t &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, const size_t& l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; enum class TYPE { OBJ = 0, STR, INT }; class ob { private: - static void BUILD(std::stringstream *tree, size_t size, ob &o) { + static void BUILD(std::stringstream* tree, size_t size, ob& o) { o.v = tree[0].str(); if (size == 1) return; if (size == 2) @@ -56,7 +56,7 @@ class ob { BUILD(tree, size - 1, o.c.back()); } } - static ob BUILD(std::stringstream *tree, size_t size) { + static ob BUILD(std::stringstream* tree, size_t size) { ob o; BUILD(tree, size, o); return o; @@ -65,13 +65,13 @@ class ob { public: std::string v, a; std::vector c; - static ob BUILD(std::vector &tree) { return BUILD(tree.data(), tree.size()); } + static ob BUILD(std::vector& tree) { return BUILD(tree.data(), tree.size()); } std::string to_string() const { std::stringstream ss; ss << v; - std::function recurse = [&](const ob &o) { + std::function recurse = [&](const ob& o) { if (o.c.empty()) ss << " : " << o.a; - for (const auto &oc : o.c) recurse(oc); + for (const auto& oc : o.c) recurse(oc); }; recurse(*this); return ss.str(); @@ -88,7 +88,7 @@ class ob { if (keys.empty()) return node; return get_with_keys_for_node(node[keys.back()], 1); }; - std::function recurse = [&](const ob &o) { + std::function recurse = [&](const ob& o) { if (o.a.empty() && o.v.empty() && o.c.empty()) return; if (o.a.empty()) { @@ -101,7 +101,7 @@ class ob { auto val = get_with_keys(); auto vals = mkn::kul::String::ESC_SPLIT(o.a, ','); if (vals.empty()) val = o.a; - for (const auto &v : vals) { + for (const auto& v : vals) { auto p = mkn::kul::String::ESC_SPLIT(v, ':'); if (p.size() == 2) { mkn::kul::String::TRIM(p); @@ -110,7 +110,7 @@ class ob { KEXCEPTION("FAIL"); } } - for (const auto &oc : o.c) recurse(oc); + for (const auto& oc : o.c) recurse(oc); }; recurse(*this); YAML::Emitter out; @@ -144,7 +144,7 @@ YAML::Node from(std::string s) { } if (tri > 1) c_check(); std::vector nodes; - for (const auto &o : obs) nodes.emplace_back(o.to_yaml()); + for (const auto& o : obs) nodes.emplace_back(o.to_yaml()); return YAML::Node(nodes); } diff --git a/inc/mkn/kul/byte.hpp b/inc/mkn/kul/byte.hpp index 34ba9fc..fdd4d9c 100644 --- a/inc/mkn/kul/byte.hpp +++ b/inc/mkn/kul/byte.hpp @@ -39,28 +39,28 @@ namespace byte { inline bool isBigEndian() { const int one = 1; - return (*(char *)&one) == 0; + return (*(char*)&one) == 0; } class LittleEndian { public: static uint16_t UINT16(uint16_t i) { uint16_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { return (i << 8) | (i >> 8); } return i; } static int16_t INT16(int16_t i) { int16_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { return (i << 8) | ((i >> 8) & 0xFF); } return i; } static uint32_t UINT32(uint32_t i) { uint32_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { i = ((i << 8) & 0xFF00FF00) | ((i >> 8) & 0xFF00FF); return (i << 16) | (i >> 16); } @@ -68,7 +68,7 @@ class LittleEndian { } static int32_t INT32(int32_t i) { int32_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { i = ((i << 8) & 0xFF00FF00) | ((i >> 8) & 0xFF00FF); return (i << 16) | ((i >> 16) & 0xFFFF); } @@ -76,7 +76,7 @@ class LittleEndian { } static uint64_t UINT64(uint64_t i) { uint64_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { i = ((i << 8) & 0xFF00FF00FF00FF00ULL) | ((i >> 8) & 0x00FF00FF00FF00FFULL); i = ((i << 16) & 0xFFFF0000FFFF0000ULL) | ((i >> 16) & 0x0000FFFF0000FFFFULL); return (i << 32) | (i >> 32); @@ -85,7 +85,7 @@ class LittleEndian { } static int64_t INT64(int64_t i) { int64_t n = 42; - if (*(char *)&n != 42) { + if (*(char*)&n != 42) { i = ((i << 8) & 0xFF00FF00FF00FF00ULL) | ((i >> 8) & 0x00FF00FF00FF00FFULL); i = ((i << 16) & 0xFFFF0000FFFF0000ULL) | ((i >> 16) & 0x0000FFFF0000FFFFULL); return (i << 32) | ((i >> 32) & 0xFFFFFFFFULL); diff --git a/inc/mkn/kul/cli.hpp b/inc/mkn/kul/cli.hpp index 44d9d05..2f6f225 100644 --- a/inc/mkn/kul/cli.hpp +++ b/inc/mkn/kul/cli.hpp @@ -47,7 +47,7 @@ namespace mkn { namespace kul { namespace cli { -inline const std::string receive(std::string const &t = "") { +inline const std::string receive(std::string const& t = "") { if (!t.empty()) std::cout << t << std::endl; std::string s; std::getline(std::cin, s); @@ -56,22 +56,23 @@ inline const std::string receive(std::string const &t = "") { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class ArgNotFoundException : public Exception { public: - ArgNotFoundException(char const *f, uint16_t const &l, std::string const &s) + ArgNotFoundException(char const* f, uint16_t const& l, std::string const& s) : Exception(f, l, s) {} }; class Cmd { private: - char const *c; + char const* c; public: - Cmd(char const *_c) : c(_c) {} - char const *command() const { return c; } + Cmd(char const* _c) : c(_c) {} + char const* command() const { return c; } }; // deprecated : use mkn::kul::env::Var::Mode @@ -81,9 +82,9 @@ enum EnvVarMode { APPE = 0, PREP, REPL }; class EnvVar { public: EnvVar(const std::string _n, const std::string _v, const EnvVarMode _m) : n(_n), v(_v), m(_m) {} - EnvVar(const EnvVar &e) : n(e.n), v(e.v), m(e.m) {} - char const *name() const { return n.c_str(); } - char const *value() const { return v.c_str(); } + EnvVar(const EnvVar& e) : n(e.n), v(e.v), m(e.m) {} + char const* name() const { return n.c_str(); } + char const* value() const { return v.c_str(); } EnvVarMode mode() const { return m; } const std::string toString() const { std::string var(value()); @@ -98,7 +99,7 @@ class EnvVar { } return var; } - EnvVar &operator=(EnvVar &&e) { + EnvVar& operator=(EnvVar&& e) { std::swap(m, e.m); std::swap(n, e.n); std::swap(v, e.v); @@ -117,16 +118,16 @@ class Arg : public Cmd { static constexpr char INVALID_CHAR = ' '; public: - Arg(const char _d, char const *dd, ArgType _t, bool m = false) : Cmd(dd), man(m), d(_d), t(_t) {} - Arg(const char _d, char const *dd, bool m = false) : Cmd(dd), man(m), d(_d) {} + Arg(const char _d, char const* dd, ArgType _t, bool m = false) : Cmd(dd), man(m), d(_d), t(_t) {} + Arg(const char _d, char const* dd, bool m = false) : Cmd(dd), man(m), d(_d) {} - Arg(char const *dd, ArgType _t, bool m = false) : Cmd(dd), man(m), t(_t) {} - Arg(char const *dd, bool m = false) : Cmd(dd), man(m) {} + Arg(char const* dd, ArgType _t, bool m = false) : Cmd(dd), man(m), t(_t) {} + Arg(char const* dd, bool m = false) : Cmd(dd), man(m) {} bool mandatory() const { return man; } char dash() const { return d; } - char const *dashdash() const { return command(); } - const ArgType &type() const { return t; } + char const* dashdash() const { return command(); } + const ArgType& type() const { return t; } private: bool man; @@ -137,52 +138,52 @@ class Arg : public Cmd { class Args { public: Args() {} - Args(const std::vector &_cmds, const std::vector &_args) : cmds(_cmds), args(_args) {} + Args(const std::vector& _cmds, const std::vector& _args) : cmds(_cmds), args(_args) {} - void arg(const Arg &a) { args.push_back(a); } - void cmd(const Cmd &c) { cmds.push_back(c); } - const Cmd &commands(char const *c) const { - for (const Cmd &cmd : cmds) + void arg(const Arg& a) { args.push_back(a); } + void cmd(const Cmd& c) { cmds.push_back(c); } + const Cmd& commands(char const* c) const { + for (const Cmd& cmd : cmds) if (strcmp(cmd.command(), c) == 0) return cmd; KEXCEPT(ArgNotFoundException, "No command " + std::string(c) + " found"); } - const Arg &dashes(const char c) const { - for (const Arg &a : arguments()) + const Arg& dashes(const char c) const { + for (const Arg& a : arguments()) if (a.dash() == c) return a; KEXCEPT(ArgNotFoundException, "No argument " + std::string(1, c) + " found"); } - const Arg &doubleDashes(char const *c) const { - for (const Arg &a : arguments()) + const Arg& doubleDashes(char const* c) const { + for (const Arg& a : arguments()) if (strcmp(a.command(), c) == 0) return a; KEXCEPT(ArgNotFoundException, "No argument " + std::string(c) + " found"); } - const std::vector &commands() const { return cmds; } - const std::vector &arguments() const { return args; } - std::string const &get(std::string const &s) const { + const std::vector& commands() const { return cmds; } + const std::vector& arguments() const { return args; } + std::string const& get(std::string const& s) const { if (has(s)) return vals.at(s); KEXCEPT(ArgNotFoundException, "No value " + s + " found"); } bool empty() const { return vals.size() == 0; } - bool has(std::string const &s) const { return vals.count(s); } + bool has(std::string const& s) const { return vals.count(s); } size_t size() const { return vals.size(); } - bool erase(std::string const &key) { return vals.erase(key); } - void process(uint16_t const &argc, char *argv[], uint16_t first = 1) + bool erase(std::string const& key) { return vals.erase(key); } + void process(uint16_t const& argc, char* argv[], uint16_t first = 1) KTHROW(ArgNotFoundException) { - for (const Arg &a1 : arguments()) - for (const Arg &a2 : arguments()) { + for (const Arg& a1 : arguments()) + for (const Arg& a2 : arguments()) { if (&a1 == &a2) continue; if ((a1.dash() != ' ') && (a1.dash() == a2.dash() || strcmp(a1.dashdash(), a2.dashdash()) == 0)) KEXCEPT(Exception, "Duplicate argument detected"); } - for (const Cmd &c1 : commands()) - for (const Cmd &c2 : commands()) { + for (const Cmd& c1 : commands()) + for (const Cmd& c2 : commands()) { if (&c1 == &c2) continue; if (strcmp(c1.command(), c2.command()) == 0) KEXCEPT(Exception, "Duplicate argument detected"); } - Arg *arg = 0; + Arg* arg = 0; uint16_t valExpected = 0; std::string valExpectedFor, c, t; @@ -205,13 +206,13 @@ class Args { c = c.substr(c.find("--") + 2); valExpectedFor = c; if (c.find("=") == std::string::npos) { - arg = const_cast(&doubleDashes(c.c_str())); + arg = const_cast(&doubleDashes(c.c_str())); valExpected = arg->type(); if (!valExpected) vals[arg->dashdash()] = ""; continue; } valExpectedFor = c.substr(0, c.find("=")); - arg = const_cast(&doubleDashes(valExpectedFor.c_str())); + arg = const_cast(&doubleDashes(valExpectedFor.c_str())); valExpected = arg->type(); if (valExpected == 0) KEXCEPT(Exception, "Found = when no value is expected for arg " + valExpectedFor); @@ -223,13 +224,13 @@ class Args { if (c.find("=") != std::string::npos) { if (c.substr(0, c.find("=")).size() > 1) KEXCEPT(Exception, "Cannot mix flag and non-flag arguments"); - arg = const_cast(&dashes(c.at(0))); + arg = const_cast(&dashes(c.at(0))); vals[arg->dashdash()] = c.substr(c.find("=") + 1); valExpected = 0; } else if (c.length() > 1) { std::string a = c; for (size_t i = 0; i < c.length(); i++) { - arg = const_cast(&dashes(a.at(0))); + arg = const_cast(&dashes(a.at(0))); if (i + 1 == c.length()) valExpected = arg->type(); else if (arg->type() == ArgType::STRING) @@ -238,7 +239,7 @@ class Args { if (a.length() > 1) a = a.substr(1); } } else { - arg = const_cast(&dashes(c.at(0))); + arg = const_cast(&dashes(c.at(0))); valExpected = arg->type(); vals[arg->dashdash()] = ""; } @@ -253,7 +254,7 @@ class Args { } if (valExpected == 1) KEXCEPT(Exception, "Value expected for argument: \"" + valExpectedFor + "\""); - for (const Arg &a : args) + for (const Arg& a : args) if (a.mandatory()) get(a.dashdash()); } #if defined(_MKN_WITH_IO_CEREAL_) @@ -268,9 +269,9 @@ class Args { #include "mkn/kul/serial/cli.arg.end.hpp" }; -inline void asArgs(std::string const &cmd, std::vector &args); +inline void asArgs(std::string const& cmd, std::vector& args); -inline std::vector asArgs(std::string const &cmd) { +inline std::vector asArgs(std::string const& cmd) { std::vector args; asArgs(cmd, args); return args; diff --git a/inc/mkn/kul/dbg.hpp b/inc/mkn/kul/dbg.hpp index 03fe007..37a2caa 100644 --- a/inc/mkn/kul/dbg.hpp +++ b/inc/mkn/kul/dbg.hpp @@ -43,14 +43,14 @@ class StackTrace { public: StackTrace() = delete; - StackTrace(const StackTrace &) = delete; - StackTrace &operator=(const StackTrace &) = delete; - StackTrace &operator=(const StackTrace &&) = delete; - StackTrace(std::string const &s) { _stk.emplace_back(s); } - StackTrace(const StackTrace &&that) { + StackTrace(const StackTrace&) = delete; + StackTrace& operator=(const StackTrace&) = delete; + StackTrace& operator=(const StackTrace&&) = delete; + StackTrace(std::string const& s) { _stk.emplace_back(s); } + StackTrace(const StackTrace&& that) { if (this != &that) this->_stk = that._stk; } - const std::vector &stack() const { return _stk; } + const std::vector& stack() const { return _stk; } }; #if !defined(NDEBUG) || defined(KUL_FORCE_TRACE) @@ -71,16 +71,16 @@ class StackTrace { #endif // KUL_DBG_FUNC_ENTER #ifndef KUL_DBG_FUNC_ON_ENTER -#define KUL_DBG_FUNC_ON_ENTER \ +#define KUL_DBG_FUNC_ON_ENTER \ KOUT(TRC) << mkn::kul::LogMan::INSTANCE().str(m_fi, m_fu, m_li, mkn::kul::log::mode::TRC, "", \ - "[%M]: %T - %D : %F:%L fn(%N)") \ + "[%M]: %T - %D : %F:%L fn(%N)") \ << " - ENTERED"; #endif // KUL_DBG_FUNC_ON_ENTER #ifndef KUL_DBG_FUNC_ON_EXIT -#define KUL_DBG_FUNC_ON_EXIT \ +#define KUL_DBG_FUNC_ON_EXIT \ KOUT(TRC) << mkn::kul::LogMan::INSTANCE().str(m_fi, m_fu, m_li, mkn::kul::log::mode::TRC, "", \ - "[%M]: %T - %D : %F:%L fn(%N)") \ + "[%M]: %T - %D : %F:%L fn(%N)") \ << " - Function time: " << (mkn::kul::Now::MICROS() - m_start) << " μs"; #endif // KUL_DBG_FUNC_ON_EXIT @@ -103,7 +103,7 @@ class FunctionScope { #endif // defined(KUL_FORCE_TRACE) || !defined(NDEBUG) public: #if defined(KUL_FORCE_TRACE) || !defined(NDEBUG) - FunctionScope(char const *fi, char const *fu, uint16_t const &li) + FunctionScope(char const* fi, char const* fu, uint16_t const& li) : m_start(mkn::kul::Now::MICROS()), m_li(li), m_fi(fi), m_fu(fu) { KUL_DBG_FUNC_ON_ENTER } diff --git a/inc/mkn/kul/env.hpp b/inc/mkn/kul/env.hpp index 49f4a01..9342e36 100644 --- a/inc/mkn/kul/env.hpp +++ b/inc/mkn/kul/env.hpp @@ -48,9 +48,9 @@ class Var { enum Mode { APPE = 0, PREP, REPL }; Var(const std::string _n, const std::string _v, const Mode _m) : n(_n), v(_v), m(_m) {} - Var(const Var &e) : n(e.n), v(e.v), m(e.m) {} - char const *name() const { return n.c_str(); } - char const *value() const { return v.c_str(); } + Var(const Var& e) : n(e.n), v(e.v), m(e.m) {} + char const* name() const { return n.c_str(); } + char const* value() const { return v.c_str(); } Mode mode() const { return m; } const std::string toString() const { std::string var(value()); @@ -65,7 +65,7 @@ class Var { } return var; } - Var &operator=(Var &&e) { + Var& operator=(Var&& e) { std::swap(m, e.m); std::swap(n, e.n); std::swap(v, e.v); diff --git a/inc/mkn/kul/except.hpp b/inc/mkn/kul/except.hpp index a0638ff..a8c5ddd 100644 --- a/inc/mkn/kul/except.hpp +++ b/inc/mkn/kul/except.hpp @@ -60,11 +60,11 @@ namespace kul { class Exception : public std::runtime_error { public: - Exception(char const *f, uint16_t const &l, std::string const &s = "") + Exception(char const* f, uint16_t const& l, std::string const& s = "") : std::runtime_error(s), _f(f), _l(l), _ep(std::current_exception()), err(s) {} - Exception(Exception const &e) + Exception(Exception const& e) : std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {} - Exception(Exception const &&e) + Exception(Exception const&& e) : std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {} virtual ~Exception() KNOTHROW {} @@ -74,20 +74,20 @@ class Exception : public std::runtime_error { return ss.str(); } - char const *what() const noexcept override { return err.c_str(); } + char const* what() const noexcept override { return err.c_str(); } std::string str() const noexcept { return err; } - char const *file() const { return _f; } - uint16_t const &line() const { return _l; } - const std::exception_ptr &cause() const { return _ep; } + char const* file() const { return _f; } + uint16_t const& line() const { return _l; } + const std::exception_ptr& cause() const { return _ep; } const std::string stack() const { std::stringstream ss; if (_ep) { try { std::rethrow_exception(_ep); - } catch (const mkn::kul::Exception &e) { + } catch (const mkn::kul::Exception& e) { ss << e.stack() << std::endl; - } catch (const std::exception &e) { + } catch (const std::exception& e) { ss << e.what() << std::endl; } catch (...) { ss << "UNKNOWN EXCEPTION TYPE" << std::endl; @@ -97,7 +97,7 @@ class Exception : public std::runtime_error { return ss.str(); } template - Exception &operator<<(const T &s) { + Exception& operator<<(const T& s) { std::stringstream msg; msg << s; err += msg.str(); @@ -105,46 +105,44 @@ class Exception : public std::runtime_error { } protected: - char const *_f; + char const* _f; const uint16_t _l; const std::exception_ptr _ep; std::string err; - Exception &operator=(Exception &e) = delete; - Exception &operator=(Exception &&e) = delete; - Exception &operator=(const Exception &e) = delete; - Exception &operator=(const Exception &&e) = delete; + Exception& operator=(Exception& e) = delete; + Exception& operator=(Exception&& e) = delete; + Exception& operator=(const Exception& e) = delete; + Exception& operator=(const Exception&& e) = delete; - friend std::ostream &operator<<(std::ostream &, const Exception &); + friend std::ostream& operator<<(std::ostream&, const Exception&); }; -inline std::ostream &operator<<(std::ostream &s, const Exception &e) { +inline std::ostream& operator<<(std::ostream& s, const Exception& e) { std::cout << __FILE__ << " " << __LINE__ << " " << e.str(); return s << e.str(); } class Exit : public Exception { public: - Exit(char const *f, uint16_t const &l, std::string const &s, uint16_t const &e) + Exit(char const* f, uint16_t const& l, std::string const& s, uint16_t const& e) : Exception(f, l, s), _e(e) {} - Exit(const Exit &e) : Exception(e), _e(e._e) {} + Exit(const Exit& e) : Exception(e), _e(e._e) {} - uint16_t const &code() const { return _e; } + uint16_t const& code() const { return _e; } private: const uint16_t _e; - Exit &operator=(Exit &e) = delete; - Exit &operator=(Exit &&e) = delete; - Exit &operator=(const Exit &e) = delete; - Exit &operator=(const Exit &&e) = delete; + Exit& operator=(Exit& e) = delete; + Exit& operator=(Exit&& e) = delete; + Exit& operator=(const Exit& e) = delete; + Exit& operator=(const Exit&& e) = delete; }; } // namespace kul } // namespace mkn - - #define KEXCEPT(e, m) throw e(__FILE__, __LINE__, m) #define KEXCEPTSTR(e) throw e(__FILE__, __LINE__, "") #define KEXCEPTION(m) throw Exception(__FILE__, __LINE__, m) diff --git a/inc/mkn/kul/for.hpp b/inc/mkn/kul/for.hpp index 5913317..ba9aa3f 100644 --- a/inc/mkn/kul/for.hpp +++ b/inc/mkn/kul/for.hpp @@ -36,10 +36,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace mkn { namespace kul { -template +template struct Apply { - template - constexpr auto operator()(){ return std::integral_constant{}; } + template + constexpr auto operator()() { + return std::integral_constant{}; + } }; template @@ -55,17 +57,61 @@ constexpr auto apply_N(Apply&& f) { template constexpr void for_N(Fn&& fn) { -/* - for_N<2>([](auto ic) { - constexpr auto i = ic(); - // ... - }); -*/ + /* + for_N<2>([](auto ic) { + constexpr auto i = ic(); + // ... + }); + */ + + std::apply([&](auto... ics) { (fn(ics), ...); }, apply_N(Apply{})); +} - std::apply([&](auto ... ics) { (fn(ics), ...);}, apply_N(Apply{})); +template +auto generate(F&& f, std::size_t from, std::size_t to) { + using value_type = std::decay_t>; + std::vector v; + std::size_t count = to - from; + if (count > 0) v.reserve(count); + for (std::size_t i = from; i < to; ++i) v.emplace_back(f(i)); + return v; } +template +auto generate(F&& f, std::size_t count) { + return generate(std::forward(f), 0, count); +} +template +auto generate(F&& f, Container& container) { + using T = typename Container::value_type; + using value_type = std::decay_t>; + std::vector v1; + if (container.size() > 0) v1.reserve(container.size()); + for (auto& v : container) v1.emplace_back(f(v)); + return v1; +} + +template +auto constexpr generate_array__(F& f, std::array& arr) { + return f(arr[Idx]); +} + +template +auto constexpr generate_array_(F& f, std::array& arr, + std::integer_sequence) { + return std::array{generate_array__(f, arr)...}; +} + +template +auto constexpr generate(F&& f, std::array& arr) { + return generate_array_(f, arr, std::make_integer_sequence{}); +} + +template +auto generate(F&& f, Container&& v) { + return generate(std::forward(f), v); +} } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/hash.hpp b/inc/mkn/kul/hash.hpp index 2995d05..47fd38a 100644 --- a/inc/mkn/kul/hash.hpp +++ b/inc/mkn/kul/hash.hpp @@ -44,7 +44,7 @@ namespace hash { class SHA { public: - static void _256(std::string const &str, std::string &ret) { + static void _256(std::string const& str, std::string& ret) { unsigned char h[SHA256_DIGEST_LENGTH]; SHA256_CTX sha; SHA256_Init(&sha); diff --git a/inc/mkn/kul/io.hpp b/inc/mkn/kul/io.hpp index 4e23cb3..b762f33 100644 --- a/inc/mkn/kul/io.hpp +++ b/inc/mkn/kul/io.hpp @@ -49,30 +49,30 @@ namespace io { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, const size_t &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, const size_t& l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; class AReader { public: - std::ifstream const &buffer() const { return f; } - std::ifstream &buffer() { return f; } + std::ifstream const& buffer() const { return f; } + std::ifstream& buffer() { return f; } - AReader(char const *const path, std::ios_base::openmode const flags) : f(path, flags) { + AReader(char const* const path, std::ios_base::openmode const flags) : f(path, flags) { if (!f) KEXCEPT(Exception, "FileException : file \"" + std::string(path) + "\" not found"); } virtual ~AReader() {} - virtual char const *readLine() = 0; - virtual size_t read(char *c, const size_t &l) = 0; - virtual void seek(const size_t &l) = 0; - static void seek(std::ifstream &_f, const size_t &_l) { _f.seekg(_l); } + virtual char const* readLine() = 0; + virtual size_t read(char* c, const size_t& l) = 0; + virtual void seek(const size_t& l) = 0; + static void seek(std::ifstream& _f, const size_t& _l) { _f.seekg(_l); } protected: - char const *readLine(std::ifstream &_f) { + char const* readLine(std::ifstream& _f) { s1.clear(); if (_f.good()) { std::stringstream ss; std::istream::sentry sen(_f, true); - std::streambuf *sb = _f.rdbuf(); + std::streambuf* sb = _f.rdbuf(); for (;;) { int c = sb->sbumpc(); switch (c) { @@ -92,7 +92,7 @@ class AReader { } return 0; } - size_t read(char *c, std::ifstream &_f, const size_t &l) { + size_t read(char* c, std::ifstream& _f, const size_t& l) { s1.clear(); if (_f.good()) { std::vector v; @@ -112,46 +112,46 @@ class AReader { }; class Reader : public AReader { public: - Reader(char const *c) : AReader(c, std::ios::in) {} - Reader(File const &c) : Reader(c.full().c_str()) {} + Reader(char const* c) : AReader(c, std::ios::in) {} + Reader(File const& c) : Reader(c.full().c_str()) {} ~Reader() { f.close(); } - char const *readLine() { return AReader::readLine(f); } - size_t read(char *c, const size_t &s) { return AReader::read(c, f, s); } - void seek(const size_t &l) { AReader::seek(f, l); } + char const* readLine() { return AReader::readLine(f); } + size_t read(char* c, const size_t& s) { return AReader::read(c, f, s); } + void seek(const size_t& l) { AReader::seek(f, l); } }; class BinaryReader : public AReader { private: public: - BinaryReader(char const *c) : AReader(c, std::ios::in | std::ios::binary) { + BinaryReader(char const* c) : AReader(c, std::ios::in | std::ios::binary) { f.exceptions(std::ifstream::badbit | std::ifstream::failbit); } - BinaryReader(File const &c) : BinaryReader(c.full().c_str()) {} + BinaryReader(File const& c) : BinaryReader(c.full().c_str()) {} ~BinaryReader() { f.close(); } - char const *readLine() { return AReader::readLine(f); } + char const* readLine() { return AReader::readLine(f); } #ifdef _WIN32 - size_t read(char *c, const size_t &s) { return AReader::read(c, f, s); } + size_t read(char* c, const size_t& s) { return AReader::read(c, f, s); } #else - size_t read(char *c, const size_t &s) { + size_t read(char* c, const size_t& s) { size_t red = 0; try { red = f.readsome(c, s); KLOG(INF) << red; - } catch (const std::ios_base::failure &e) { + } catch (const std::ios_base::failure& e) { KLOG(ERR) << e.what(); } return red; } - size_t read(uint8_t *c, const size_t &s) { + size_t read(uint8_t* c, const size_t& s) { size_t red = 0; try { - red = f.readsome((char *)c, s); - } catch (const std::ios_base::failure &e) { + red = f.readsome((char*)c, s); + } catch (const std::ios_base::failure& e) { KLOG(ERR) << e.what(); } return red; } #endif - void seek(const size_t &s) { AReader::seek(f, s); } + void seek(const size_t& s) { AReader::seek(f, s); } }; class AWriter { @@ -161,32 +161,32 @@ class AWriter { } void close() { f.close(); } - AWriter &write(char const *c, bool nl = false) { + AWriter& write(char const* c, bool nl = false) { if (nl) f << c << mkn::kul::os::EOL(); else f << c; return *this; } - AWriter &write(char const *c, size_t len) { + AWriter& write(char const* c, size_t len) { f.write(c, len); return *this; } - AWriter &write(const uint8_t *c, size_t len) { - f.write((char const *)(c), len); + AWriter& write(const uint8_t* c, size_t len) { + f.write((char const*)(c), len); return *this; } template - AWriter &operator<<(const T &s) { + AWriter& operator<<(const T& s) { f << s; return *this; } - AWriter &operator<<(std::ostream &(*os)(std::ostream &)) { + AWriter& operator<<(std::ostream& (*os)(std::ostream&)) { (void)os; f << std::flush; return *this; } - AWriter &flush() { + AWriter& flush() { f << std::flush; return *this; } @@ -196,24 +196,24 @@ class AWriter { }; class Writer : public AWriter { public: - Writer(char const *c, bool a = 0) { + Writer(char const* c, bool a = 0) { if (a) f.open(c, std::ios::out | std::ios::app); else f.open(c, std::ios::out); if (!f) KEXCEPT(Exception, "FileException : file \"" + std::string(c) + "\" not found"); } - Writer(File const &c, bool a = 0) : Writer(c.full().c_str(), a) {} + Writer(File const& c, bool a = 0) : Writer(c.full().c_str(), a) {} ~Writer() {} }; class BinaryWriter : public AWriter { public: - BinaryWriter(char const *c) { + BinaryWriter(char const* c) { f.open(c, std::ios::out | std::ios::binary); if (!f) KEXCEPT(Exception, "FileException : file \"" + std::string(c) + "\" not found"); f.unsetf(std::ios_base::skipws); } - BinaryWriter(File const &c) : BinaryWriter(c.full().c_str()) {} + BinaryWriter(File const& c) : BinaryWriter(c.full().c_str()) {} ~BinaryWriter() { f << std::flush; f.close(); diff --git a/inc/mkn/kul/log.hpp b/inc/mkn/kul/log.hpp index 2852f66..afccd92 100644 --- a/inc/mkn/kul/log.hpp +++ b/inc/mkn/kul/log.hpp @@ -55,7 +55,8 @@ enum mode { OFF = -1, NON = 0, INF, ERR, DBG, OTH, TRC }; class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; } // namespace log @@ -64,8 +65,8 @@ class Logger { friend class ALogMan; protected: - std::function e, o; - const std::string modeTxt(const log::mode &m) const { + std::function e, o; + const std::string modeTxt(const log::mode& m) const { std::string s("NON"); if (m == 1) s = "INF"; @@ -82,8 +83,8 @@ class Logger { public: virtual ~Logger() {} - void str(char const *f, char const *fn, uint16_t const &l, std::string const &s, - const log::mode &m, std::string &str) { + void str(char const* f, char const* fn, uint16_t const& l, std::string const& s, + const log::mode& m, std::string& str) { mkn::kul::String::REPLACE(str, "%M", modeTxt(m)); mkn::kul::String::REPLACE(str, "%T", mkn::kul::this_thread::id()); mkn::kul::String::REPLACE(str, "%D", mkn::kul::DateTime::NOW(__MKN_KUL_LOG_TIME_FRMT__)); @@ -92,33 +93,33 @@ class Logger { mkn::kul::String::REPLACE(str, "%L", std::to_string(l)); mkn::kul::String::REPLACE(str, "%S", s); } - virtual void err(std::string const &s) { + virtual void err(std::string const& s) { if (e) e(s); else fprintf(stderr, "%s", s.c_str()); } - virtual void out(std::string const &s) { + virtual void out(std::string const& s) { if (o) o(s); else printf("%s", s.c_str()); } - void log(char const *f, char const *fn, uint16_t const &l, std::string const &s, - const log::mode &m) { + void log(char const* f, char const* fn, uint16_t const& l, std::string const& s, + const log::mode& m) { std::string st(__MKN_KUL_LOG_FRMT__); str(f, fn, l, s, m, st); out(st + mkn::kul::os::EOL()); } - void setOut(std::function _o) { this->o = _o; } - void setErr(std::function _e) { this->e = _e; } + void setOut(std::function _o) { this->o = _o; } + void setErr(std::function _e) { this->e = _e; } }; class ALogMan { protected: log::mode m; mutable std::unique_ptr logger; - ALogMan(Logger *_logger) : m(mkn::kul::log::mode::NON), logger(_logger) { + ALogMan(Logger* _logger) : m(mkn::kul::log::mode::NON), logger(_logger) { std::string s(mkn::kul::env::GET("KLOG")); if (s.size()) { mkn::kul::String::TRIM(s); @@ -145,26 +146,26 @@ class ALogMan { public: virtual ~ALogMan() {} - void setMode(const log::mode &m1) { m = m1; } + void setMode(const log::mode& m1) { m = m1; } bool inf() { return m >= log::INF; } bool err() { return m >= log::ERR; } bool dbg() { return m >= log::DBG; } - void log(char const *f, char const *fn, uint16_t const &l, const log::mode &_m, - std::string const &s) { + void log(char const* f, char const* fn, uint16_t const& l, const log::mode& _m, + std::string const& s) { if (this->m >= _m) logger->log(f, fn, l, s, _m); } - void out(const log::mode &_m, std::string const &s) { + void out(const log::mode& _m, std::string const& s) { if (this->m >= _m) logger->out(s + mkn::kul::os::EOL()); } - void err(std::string const &s) { logger->err(s + mkn::kul::os::EOL()); } - std::string str(char const *f, char const *fn, uint16_t const &l, const log::mode &_m, - std::string const &s = "", const std::string fmt = __MKN_KUL_LOG_FRMT__) { + void err(std::string const& s) { logger->err(s + mkn::kul::os::EOL()); } + std::string str(char const* f, char const* fn, uint16_t const& l, const log::mode& _m, + std::string const& s = "", const std::string fmt = __MKN_KUL_LOG_FRMT__) { std::string st(fmt); logger->str(f, fn, l, s, _m, st); return st; } - void setOut(std::function o) { logger->setOut(o); } - void setErr(std::function e) { logger->setErr(e); } + void setOut(std::function o) { logger->setOut(o); } + void setErr(std::function e) { logger->setErr(e); } }; class LogMan : public ALogMan { @@ -172,7 +173,7 @@ class LogMan : public ALogMan { LogMan() : ALogMan(new Logger()) {} public: - static LogMan &INSTANCE() { + static LogMan& INSTANCE() { static LogMan instance; return instance; }; @@ -181,54 +182,52 @@ class LogMan : public ALogMan { class Message { protected: std::stringstream ss; - const log::mode &m; + const log::mode& m; - Message(const log::mode &_m) : m(_m) { - ss.precision(22); - } + Message(const log::mode& _m) : m(_m) { ss.precision(22); } public: template - Message &operator<<(const T &s) { + Message& operator<<(const T& s) { ss << s; return *this; } }; class LogMessage : public Message { public: - LogMessage(char const *_f, char const *_fn, uint16_t const &_l, const log::mode &_m) + LogMessage(char const* _f, char const* _fn, uint16_t const& _l, const log::mode& _m) : Message(_m), f(_f), fn(_fn), l(_l) {} ~LogMessage() { LogMan::INSTANCE().log(f, fn, l, m, ss.str()); } private: char const *f, *fn; - uint16_t const &l; + uint16_t const& l; }; class DBgMessage : public Message { public: ~DBgMessage() { KUL_DEBUG_DO(LogMan::INSTANCE().log(f, fn, l, m, ss.str())); } #if !defined(NDEBUG) - DBgMessage(char const *_f, char const *_fn, uint16_t const &_l, const log::mode &_m) + DBgMessage(char const* _f, char const* _fn, uint16_t const& _l, const log::mode& _m) : Message(_m), f(_f), fn(_fn), l(_l) {} #else DBgMessage() : Message(mkn::kul::log::mode::NON) {} #endif template - DBgMessage &operator<<([[maybe_unused]] const T &s) { + DBgMessage& operator<<([[maybe_unused]] const T& s) { KUL_DEBUG_DO(ss << s;) return *this; } private: - KUL_DEBUG_DO(char const *f, *fn;) - KUL_DEBUG_DO(uint16_t const &l;) + KUL_DEBUG_DO(char const* f, *fn;) + KUL_DEBUG_DO(uint16_t const& l;) }; class OutMessage : public Message { public: ~OutMessage() { LogMan::INSTANCE().out(m, ss.str()); } - OutMessage(const log::mode &_m = mkn::kul::log::mode::NON) : Message(_m) {} + OutMessage(const log::mode& _m = mkn::kul::log::mode::NON) : Message(_m) {} }; class ErrMessage : public Message { public: @@ -238,9 +237,9 @@ class ErrMessage : public Message { class DBoMessage : public Message { public: ~DBoMessage() { KUL_DEBUG_DO(LogMan::INSTANCE().out(m, ss.str())); } - DBoMessage(const log::mode &_m = mkn::kul::log::mode::NON) : Message(_m) {} + DBoMessage(const log::mode& _m = mkn::kul::log::mode::NON) : Message(_m) {} template - DBoMessage &operator<<([[maybe_unused]] const T &s) { + DBoMessage& operator<<([[maybe_unused]] const T& s) { KUL_DEBUG_DO(ss << s;) return *this; } @@ -276,6 +275,6 @@ class DBoMessage : public Message { #define KERR mkn::kul::ErrMessage() -#endif //!defined(_MKN_KUL_DISABLE_KLOG_DEF_) +#endif //! defined(_MKN_KUL_DISABLE_KLOG_DEF_) #endif /* _MKN_KUL_LOG_HPP_ */ diff --git a/inc/mkn/kul/map.hpp b/inc/mkn/kul/map.hpp index eb00c38..d4acbd1 100644 --- a/inc/mkn/kul/map.hpp +++ b/inc/mkn/kul/map.hpp @@ -59,15 +59,15 @@ namespace hash { template class Map : public std::unordered_map { public: - Map &insert(const K &k, const V &v) { + Map& insert(const K& k, const V& v) { this->insert(std::make_pair(k, v)); return *this; } - Map &insert(const std::pair &pair) { + Map& insert(const std::pair& pair) { std::unordered_map::insert(pair); return *this; } - void setDeletedKey(const K &key) { (void)key; } + void setDeletedKey(const K& key) { (void)key; } }; namespace map { @@ -99,7 +99,7 @@ namespace kul { struct StdStringComparator { public: - bool operator()(std::string const &s1, std::string const &s2) const { + bool operator()(std::string const& s1, std::string const& s2) const { return (s1.compare(s2) == 0); } }; @@ -116,19 +116,19 @@ class Set : public google::sparse_hash_set { typedef typename Hash::iterator iterator; typedef typename Hash::const_iterator const_iterator; - void setEmptyKey(const key_type &key) { Hash::set_empty_key(key); } - void setDeletedKey(const key_type &key) { Hash::set_deleted_key(key); } + void setEmptyKey(const key_type& key) { Hash::set_empty_key(key); } + void setDeletedKey(const key_type& key) { Hash::set_deleted_key(key); } void clear() { Hash::clear(); } std::pair insert(const T obj) { return Hash::insert(obj); } - T &operator[](const key_type &key) { return Hash::operator[](key); } - size_type erase(const key_type &key) { return Hash::erase(key); } + T& operator[](const key_type& key) { return Hash::operator[](key); } + size_type erase(const key_type& key) { return Hash::erase(key); } iterator begin() { return Hash::begin(); } iterator end() { return Hash::end(); } const_iterator begin() const { return Hash::begin(); } const_iterator end() const { return Hash::end(); } size_type size() const { return Hash::size(); } const key_type getDeletedKey() const { return Hash::deleted_key(); } - size_type count(const key_type &key) const { return Hash::count(key); } + size_type count(const key_type& key) const { return Hash::count(key); } }; namespace set { @@ -145,21 +145,21 @@ class Map : public google::sparse_hash_map { typedef typename map::iterator iterator; typedef typename map::const_iterator const_iterator; - void setDeletedKey(const key_type &key) { map::set_deleted_key(key); } - void setEmptyKey(const key_type &key) { map::set_empty_key(key); } + void setDeletedKey(const key_type& key) { map::set_deleted_key(key); } + void setEmptyKey(const key_type& key) { map::set_empty_key(key); } void clear() { map::clear(); } - std::pair insert(const std::pair &obj) { return map::insert(obj); } + std::pair insert(const std::pair& obj) { return map::insert(obj); } std::pair insert(const K k, V v) { return insert(std::pair(k, v)); } - V &operator[](const key_type &key) { return map::operator[](key); } - size_type erase(const key_type &key) { return map::erase(key); } + V& operator[](const key_type& key) { return map::operator[](key); } + size_type erase(const key_type& key) { return map::erase(key); } iterator begin() { return map::begin(); } iterator end() { return map::end(); } - iterator find(const key_type &key) { return map::find(key); } + iterator find(const key_type& key) { return map::find(key); } size_type size() const { return map::size(); } const_iterator begin() const { return map::begin(); } const_iterator end() const { return map::end(); } - const_iterator find(const key_type &key) const { return map::find(key); } - size_type count(const key_type &key) const { return map::count(key); } + const_iterator find(const key_type& key) const { return map::find(key); } + size_type count(const key_type& key) const { return map::count(key); } const key_type getDeletedKey() const { return map::deleted_key(); } }; @@ -184,19 +184,19 @@ class Set : public google::dense_hash_set { typedef typename Hash::iterator iterator; typedef typename Hash::const_iterator const_iterator; - void setEmptyKey(const key_type &key) { Hash::set_empty_key(key); } + void setEmptyKey(const key_type& key) { Hash::set_empty_key(key); } void clear() { Hash::clear(); } - void setDeletedKey(const key_type &key) { Hash::set_deleted_key(key); } + void setDeletedKey(const key_type& key) { Hash::set_deleted_key(key); } std::pair insert(const T obj) { return Hash::insert(obj); } - T &operator[](const key_type &key) { return Hash::operator[](key); } - size_type erase(const key_type &key) { return Hash::erase(key); } + T& operator[](const key_type& key) { return Hash::operator[](key); } + size_type erase(const key_type& key) { return Hash::erase(key); } iterator begin() { return Hash::begin(); } iterator end() { return Hash::end(); } const_iterator begin() const { return Hash::begin(); } const_iterator end() const { return Hash::end(); } size_type size() const { return Hash::size(); } const key_type getDeletedKey() const { return Hash::deleted_key(); } - size_type count(const key_type &key) const { return Hash::count(key); } + size_type count(const key_type& key) const { return Hash::count(key); } }; namespace set { @@ -213,22 +213,22 @@ class Map : public google::dense_hash_map { typedef typename map::iterator iterator; typedef typename map::const_iterator const_iterator; - void setDeletedKey(const key_type &key) { map::set_deleted_key(key); } - void setEmptyKey(const key_type &key) { map::set_empty_key(key); } + void setDeletedKey(const key_type& key) { map::set_deleted_key(key); } + void setEmptyKey(const key_type& key) { map::set_empty_key(key); } void clear() { map::clear(); } - std::pair insert(const std::pair &obj) { return map::insert(obj); } + std::pair insert(const std::pair& obj) { return map::insert(obj); } std::pair insert(const K k, V v) { return insert(std::pair(k, v)); } - V &operator[](const key_type &key) { return map::operator[](key); } - size_type erase(const key_type &key) { return map::erase(key); } + V& operator[](const key_type& key) { return map::operator[](key); } + size_type erase(const key_type& key) { return map::erase(key); } iterator begin() { return map::begin(); } iterator end() { return map::end(); } - iterator find(const key_type &key) { return map::find(key); } + iterator find(const key_type& key) { return map::find(key); } size_type size() const { return map::size(); } const_iterator begin() const { return map::begin(); } const_iterator end() const { return map::end(); } - const_iterator find(const key_type &key) const { return map::find(key); } + const_iterator find(const key_type& key) const { return map::find(key); } const key_type getDeletedKey() const { return map::deleted_key(); } - size_type count(const key_type &key) const { return map::count(key); } + size_type count(const key_type& key) const { return map::count(key); } }; namespace map { diff --git a/inc/mkn/kul/math.hpp b/inc/mkn/kul/math.hpp index 9a4e3e0..95f3cc2 100644 --- a/inc/mkn/kul/math.hpp +++ b/inc/mkn/kul/math.hpp @@ -43,49 +43,40 @@ namespace math { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) + Exception(char const* f, uint16_t const& l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; template -T abs(const T &f) { +T abs(const T& f) { return f < 0 ? f * -1 : f; } template -T pow(const float &f, const int16_t &e = 2) { +T pow(const float& f, const int16_t& e = 2) { T r = f < 0 ? -1 : 1; for (uint16_t i = 0; i < abs(e); i++) r *= f; return e < 0 ? 1 / r : r; } template -T root(const float &f, const int16_t &r = 2, uint16_t const &it = 6, T g = 0) { +T root(const float& f, const int16_t& r = 2, uint16_t const& it = 6, T g = 0) { if (r < 1) KEXCEPT(Exception, "Invalid root quotient, must be greater than abs(1)"); if (g == 0) g = r >= f || r + r >= f ? 1 : (int)f / (r + r); for (uint16_t i = 0; i < it; i++) g = ((float)1 / r) * (((r - 1) * g) + (f / pow(g, r - 1))); return g; } - - -template -Multiplies product(Container const& container, Multiplies mul = 1) -{ - return std::accumulate(container.begin(), container.end(), mul, std::multiplies()); +template +Multiplies product(Container const& container, Multiplies mul = 1) { + return std::accumulate(container.begin(), container.end(), mul, std::multiplies()); } - -template -Return sum(Container const& container, Return r = 0) -{ - return std::accumulate(container.begin(), container.end(), r); +template +Return sum(Container const& container, Return r = 0) { + return std::accumulate(container.begin(), container.end(), r); } - - - - } // namespace math } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/math/blas.hpp b/inc/mkn/kul/math/blas.hpp index 9d49508..5c57441 100644 --- a/inc/mkn/kul/math/blas.hpp +++ b/inc/mkn/kul/math/blas.hpp @@ -7,12 +7,12 @@ namespace kul { namespace math { template static inline typename std::enable_if::value>::type mult_incr( - const uint64_t n, const K alpha, Y const *x, T *y) { + const uint64_t n, const K alpha, Y const* x, T* y) { cblas_saxpy(n, alpha, x, 1, y, 1); } template static inline typename std::enable_if::value>::type mult_incr( - const uint64_t n, const K alpha, Y const *x, T *y) { + const uint64_t n, const K alpha, Y const* x, T* y) { cblas_daxpy(n, alpha, x, 1, y, 1); } @@ -21,7 +21,7 @@ static inline typename std::enable_if::value && !std::is_same>::value && !std::is_same>::value, K>::type -dot(const size_t n, T const *x, K const *y) { +dot(const size_t n, T const* x, K const* y) { return cblas_sdot(n, x, 1, y, 1); } template @@ -29,31 +29,31 @@ static inline typename std::enable_if::value && !std::is_same>::value && !std::is_same>::value, K>::type -dot(const size_t n, T const *x, K const *y) { +dot(const size_t n, T const* x, K const* y) { return cblas_ddot(n, x, 1, y, 1); } template static inline typename std::enable_if::value>::type scale( - const size_t n, const std::atomic alpha, T *x) { + const size_t n, const std::atomic alpha, T* x) { cblas_sscal(n, alpha.load(), x, 1); } template static inline typename std::enable_if::value>::type scale(const size_t n, const K alpha, - T *x) { + T* x) { cblas_sscal(n, alpha, x, 1); } template static inline typename std::enable_if::value>::type scale( - const size_t n, const std::atomic alpha, T *x) { + const size_t n, const std::atomic alpha, T* x) { cblas_dscal(n, alpha.load(), x, 1); } template static inline typename std::enable_if::value>::type scale(const size_t n, const K alpha, - T *x) { + T* x) { cblas_dscal(n, alpha, x, 1); } } // namespace math diff --git a/inc/mkn/kul/math/noblas.hpp b/inc/mkn/kul/math/noblas.hpp index fec14ca..d63a73d 100644 --- a/inc/mkn/kul/math/noblas.hpp +++ b/inc/mkn/kul/math/noblas.hpp @@ -8,7 +8,7 @@ namespace math { template static inline typename std::enable_if::value || std::is_same::value>::type - mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { + mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { detail::mult_incr(n, alpha, x, y); } @@ -18,21 +18,21 @@ static inline !std::is_same>::value && !std::is_same>::value, K>::type - dot(const size_t n, T const *x, K const *y) { + dot(const size_t n, T const* x, K const* y) { return detail::dot(n, x, y); } template static inline typename std::enable_if::value || std::is_same::value>::type - scale(const size_t n, const std::atomic alpha, T *x) { + scale(const size_t n, const std::atomic alpha, T* x) { detail::scale(n, alpha.load(), x); } template static inline typename std::enable_if::value || std::is_same::value>::type - scale(const size_t n, const K alpha, T *x) { + scale(const size_t n, const K alpha, T* x) { detail::scale(n, alpha, x); } diff --git a/inc/mkn/kul/math/noop.hpp b/inc/mkn/kul/math/noop.hpp index b3436ee..a2e231e 100644 --- a/inc/mkn/kul/math/noop.hpp +++ b/inc/mkn/kul/math/noop.hpp @@ -26,7 +26,7 @@ namespace kul { namespace math { namespace detail { template -void mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { +void mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { for (uint64_t i = 0; i < n; ++i) { K y_i = y[i]; y_i += alpha * x[i]; @@ -34,11 +34,11 @@ void mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { } } template -void scale(const size_t n, const K alpha, T *x) { +void scale(const size_t n, const K alpha, T* x) { for (uint64_t i = 0; i < n; ++i) x[i] *= alpha; } template -void scale(const size_t n, const T alpha, std::atomic *x) { +void scale(const size_t n, const T alpha, std::atomic* x) { for (uint64_t i = 0; i < n; ++i) { K x_i = x[i].load(); x_i *= alpha; @@ -46,13 +46,13 @@ void scale(const size_t n, const T alpha, std::atomic *x) { } } template -T dot(const size_t n, T const *x, K const *y) { +T dot(const size_t n, T const* x, K const* y) { T result{0}; for (uint64_t i = 0; i < n; ++i) result += x[i] * y[i]; return result; } template -T dot(const size_t n, T const *x, std::atomic const *y) { +T dot(const size_t n, T const* x, std::atomic const* y) { K result{0}; for (uint64_t i = 0; i < n; ++i) result += x[i] * y[i].load(); return result; @@ -62,14 +62,14 @@ template typename std::enable_if<(!std::is_same::value && !std::is_same::value) && std::is_same>::value, T>::type -dot(const size_t n, T const *x, K const *y) { +dot(const size_t n, T const* x, K const* y) { return detail::dot(n, y, x); } template typename std::enable_if<(!std::is_same::value && !std::is_same::value) && std::is_same>::value, K>::type -dot(const size_t n, K const *x, T const *y) { +dot(const size_t n, K const* x, T const* y) { return detail::dot(n, x, y); } @@ -77,7 +77,7 @@ template typename std::enable_if<(!std::is_same::value && !std::is_same::value) && !std::is_same>::value, T>::type -dot(const size_t n, T const *x, K const *y) { +dot(const size_t n, T const* x, K const* y) { CHECK_BLAS_OPTIMIZATION_PP(x, y, "dot prod"); return detail::dot(n, x, y); } @@ -86,7 +86,7 @@ template typename std::enable_if::value || std::is_same::value) && std::is_same>::value && !std::is_same>::value>::type -mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { +mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { for (uint64_t i = 0; i < n; ++i) { K y_i = y[i].load(); y_i += alpha * x[i]; @@ -98,7 +98,7 @@ template typename std::enable_if::value || std::is_same::value) && std::is_same>::value && !std::is_same>::value>::type -mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { +mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { for (uint64_t i = 0; i < n; ++i) { K y_i = y[i]; y_i += alpha * x[i].load(); @@ -109,7 +109,7 @@ template typename std::enable_if::value || std::is_same::value) && std::is_same>::value && std::is_same>::value>::type -mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { +mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { for (uint64_t i = 0; i < n; ++i) { K y_i = y[i].load(); y_i += alpha * x[i].load(); @@ -118,31 +118,31 @@ mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { } template typename std::enable_if::value || std::is_same::value)>::type -mult_incr(const uint64_t n, const K alpha, Y const *x, T *y) { +mult_incr(const uint64_t n, const K alpha, Y const* x, T* y) { CHECK_BLAS_OPTIMIZATION_PP(x, y, "mult_incr"); return detail::mult_incr(n, alpha, x, y); } template typename std::enable_if>::value>::type set(const size_t n, - const K alpha, T *x) { + const K alpha, T* x) { for (uint64_t i = 0; i < n; ++i) x[i].store(alpha); } template typename std::enable_if>::value>::type set(const size_t n, - const K alpha, T *x) { + const K alpha, T* x) { for (uint64_t i = 0; i < n; ++i) x[i] = alpha; } template -K sum(const size_t n, T const *x) { +K sum(const size_t n, T const* x) { return std::accumulate(x, x + n, K{0}); } template typename std::enable_if::value || std::is_same::value) && std::is_same>::value>::type -scale(const size_t n, const K alpha, T *x) { +scale(const size_t n, const K alpha, T* x) { for (uint64_t i = 0; i < n; ++i) { K x_i = x[i].load(); x_i *= alpha; @@ -152,14 +152,14 @@ scale(const size_t n, const K alpha, T *x) { template typename std::enable_if::value || std::is_same::value) && !std::is_same>::value>::type -scale(const size_t n, const K alpha, T *x) { +scale(const size_t n, const K alpha, T* x) { CHECK_BLAS_OPTIMIZATION_PS(x, alpha, "scale"); detail::scale(n, alpha, x); } template typename std::enable_if>::value>::type dot_matrix_vector_incr( - const size_t m, const size_t n, const K alpha, T const *a, T const *x, const T beta, T *y) { + const size_t m, const size_t n, const K alpha, T const* a, T const* x, const T beta, T* y) { for (size_t i = 0; i < m; ++i) { K y_i = beta * y[i]; for (size_t j = 0; j < n; ++j) y_i += alpha * a[i * n + j] * x[j].load(); @@ -169,7 +169,7 @@ typename std::enable_if>::value>::type dot_matrix template typename std::enable_if>::value>::type dot_matrix_vector_incr( - const size_t m, const size_t n, const K alpha, T const *a, T const *x, const T beta, T *y) { + const size_t m, const size_t n, const K alpha, T const* a, T const* x, const T beta, T* y) { for (size_t i = 0; i < m; ++i) { y[i] = beta * y[i]; for (size_t j = 0; j < n; ++j) y[i] += alpha * a[i * n + j] * x[j]; @@ -178,7 +178,7 @@ typename std::enable_if>::value>::type dot_matri template typename std::enable_if>::value>::type dot_matrix_vector( - const size_t m, const size_t n, const K alpha, T const *a, T const *x, T *y) { + const size_t m, const size_t n, const K alpha, T const* a, T const* x, T* y) { for (size_t i = 0; i < m; ++i) { K y_i = 0; for (size_t j = 0; j < n; ++j) y_i += alpha * a[i * n + j] * x[j].load(); @@ -188,7 +188,7 @@ typename std::enable_if>::value>::type dot_matrix template typename std::enable_if>::value>::type dot_matrix_vector( - const size_t m, const size_t n, const K alpha, T const *a, T const *x, T *y) { + const size_t m, const size_t n, const K alpha, T const* a, T const* x, T* y) { for (size_t i = 0; i < m; ++i) { y[i] = 0; for (size_t j = 0; j < n; ++j) y[i] += alpha * a[i * n + j] * x[j]; diff --git a/inc/mkn/kul/os.hpp b/inc/mkn/kul/os.hpp index 978698f..4e66240 100644 --- a/inc/mkn/kul/os.hpp +++ b/inc/mkn/kul/os.hpp @@ -53,18 +53,19 @@ class File; namespace fs { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class TimeStamps { private: const uint64_t _a, _c, _m; - TimeStamps(const uint64_t &a, const uint64_t &c, const uint64_t &m) : _a(a), _c(c), _m(m) {} + TimeStamps(const uint64_t& a, const uint64_t& c, const uint64_t& m) : _a(a), _c(c), _m(m) {} public: - const uint64_t &accessed() const { return _a; } - const uint64_t &created() const { return _c; } - const uint64_t &modified() const { return _m; } + const uint64_t& accessed() const { return _a; } + const uint64_t& created() const { return _c; } + const uint64_t& modified() const { return _m; } friend class mkn::kul::Dir; friend class mkn::kul::File; }; @@ -82,13 +83,13 @@ class Item { class Dir : public fs::Item { private: std::string _p; - static fs::TimeStamps TIMESTAMPS(std::string const &s) { + static fs::TimeStamps TIMESTAMPS(std::string const& s) { uint64_t a = 0, c = 0, m = 0; fs::KulTimeStampsResolver::GET(s.c_str(), a, c, m); return fs::TimeStamps(a, c, m); } static inline std::string LOCL(std::string s); - static void ESC_REPLACE(std::string &s, std::string const &f, std::string const &r) { + static void ESC_REPLACE(std::string& s, std::string const& f, std::string const& r) { size_t p = s.find(f); while (p != std::string::npos) { s.replace(s.find(f, p), f.size(), r); @@ -96,44 +97,43 @@ class Dir : public fs::Item { } } static inline std::string ESC(std::string s); - static std::string PRNT(std::string const &s) { - std::string const &p = s.substr(0, s.rfind(SEP()) + 1); + static std::string PRNT(std::string const& s) { + std::string const& p = s.substr(0, s.rfind(SEP()) + 1); return mkn::kul::Dir(p).root() ? p : s.substr(0, s.rfind(SEP())); } - static std::string MINI(std::string const &a) { - return a.find(env::CWD()) == std::string::npos - ? a - : a.compare(env::CWD()) == 0 ? a.substr(std::string(env::CWD()).size()) - : a.substr(std::string(env::CWD()).size() + 1); + static std::string MINI(std::string const& a) { + return a.find(env::CWD()) == std::string::npos ? a + : a.compare(env::CWD()) == 0 ? a.substr(std::string(env::CWD()).size()) + : a.substr(std::string(env::CWD()).size() + 1); } public: Dir() : _p() {} - Dir(char const *p, bool m = false) KTHROW(fs::Exception) : Dir(std::string(p), m) {} - Dir(std::string const &p, bool m = false) KTHROW(fs::Exception) : _p(Dir::LOCL(p)) { + Dir(char const* p, bool m = false) KTHROW(fs::Exception) : Dir(std::string(p), m) {} + Dir(std::string const& p, bool m = false) KTHROW(fs::Exception) : _p(Dir::LOCL(p)) { #ifndef _WIN32 if (p.size() && p[0] == '~') this->_p = (env::GET("HOME") + p.substr(1)); #endif if (m && !is() && !mk()) KEXCEPT(fs::Exception, "Invalid directory path provided"); } - Dir(Dir const &d) : _p(d._p) {} - Dir(std::string const &s, Dir const &d) : _p(d.join(s)) {} + Dir(Dir const& d) : _p(d._p) {} + Dir(std::string const& s, Dir const& d) : _p(d.join(s)) {} - bool cp(Dir const &d) const; - bool mv(Dir const &d) const { return std::rename(this->path().c_str(), d.path().c_str()); } + bool cp(Dir const& d) const; + bool mv(Dir const& d) const { return std::rename(this->path().c_str(), d.path().c_str()); } inline void rm() const; inline bool is() const; inline bool mk() const; inline bool root() const; - const std::string join(std::string const &s) const { + const std::string join(std::string const& s) const { return _p.size() == 0 ? s : root() ? path() + s : JOIN(path(), s); } const std::string name() const { return root() ? path() : path().substr(path().rfind(SEP()) + 1); } - std::string const &path() const { return _p; } + std::string const& path() const { return _p; } std::string esc() const { return ESC(path()); } std::string escr() const { return ESC(is() ? real() : path()); } @@ -149,9 +149,9 @@ class Dir : public fs::Item { inline std::vector dirs(bool incHidden = false) const KTHROW(fs::Exception); inline std::vector files(bool recursive = false) const KTHROW(fs::Exception); - static std::string JOIN(std::string const &a, std::string const &b) { return a + SEP() + b; } + static std::string JOIN(std::string const& a, std::string const& b) { return a + SEP() + b; } - static inline std::string REAL(std::string const &s) KTHROW(fs::Exception); + static inline std::string REAL(std::string const& s) KTHROW(fs::Exception); #ifdef _WIN32 static std::string SEP() { return std::string("\\"); } @@ -160,18 +160,18 @@ class Dir : public fs::Item { #endif friend class File; - Dir &operator=(Dir const &d) { + Dir& operator=(Dir const& d) { this->_p = d._p; return *this; } - bool operator==(Dir const &d) const { + bool operator==(Dir const& d) const { if (is() && d.is()) return real().compare(d.real()) == 0; return path().compare(d.path()) == 0; } explicit operator bool() const { return is(); } - std::string relative(mkn::kul::Dir const &r) const { - const auto &l(*this); + std::string relative(mkn::kul::Dir const& r) const { + const auto& l(*this); std::string left = l.real(), right = r.real(); auto l_p = l, r_p = r; while (!l_p.root()) l_p = l_p.parent().real(); @@ -195,7 +195,7 @@ class Dir : public fs::Item { } }; -inline std::ostream &operator<<(std::ostream &s, Dir const &d) { return s << d.path(); } +inline std::ostream& operator<<(std::ostream& s, Dir const& d) { return s << d.path(); } class File : public fs::Item { private: @@ -204,13 +204,13 @@ class File : public fs::Item { public: File() : _n(), _d() {} - File(std::string const &n, bool m = false) : _n(Dir::LOCL(n)), _d(env::CWD()) { + File(std::string const& n, bool m = false) : _n(Dir::LOCL(n)), _d(env::CWD()) { if (is()) { try { this->_n = Dir::REAL(this->_n); this->_d = Dir(Dir::PRNT(this->_n), m); this->_n = this->_n.substr(_d.path().size() + 1); - } catch (const mkn::kul::fs::Exception &e) { + } catch (const mkn::kul::fs::Exception& e) { } } else { if (n.find(Dir::SEP()) != std::string::npos) { @@ -219,22 +219,22 @@ class File : public fs::Item { } else { try { this->_d = Dir(Dir::PRNT(Dir::REAL(this->_n)), m); - } catch (const mkn::kul::fs::Exception &e) { + } catch (const mkn::kul::fs::Exception& e) { } } } } - File(char const *n, bool m = false) : File(std::string(n), m) {} - File(std::string const &n, Dir const &d) : _n(n), _d(d) {} - File(std::string const &n, char const *c) : _n(n), _d(c) {} - File(std::string const &n, std::string const &d1) : _n(n), _d(d1) {} - File(File const &f) : _n(f._n), _d(f._d) {} + File(char const* n, bool m = false) : File(std::string(n), m) {} + File(std::string const& n, Dir const& d) : _n(n), _d(d) {} + File(std::string const& n, char const* c) : _n(n), _d(c) {} + File(std::string const& n, std::string const& d1) : _n(n), _d(d1) {} + File(File const& f) : _n(f._n), _d(f._d) {} - bool cp(Dir const &d) const { + bool cp(Dir const& d) const { if (!d.is() && !d.mk()) KEXCEPT(fs::Exception, "Directory: \"" + _d.path() + "\" is not valid"); return cp(mkn::kul::File(name(), d._p)); } - bool cp(File const &f) const { + bool cp(File const& f) const { std::ifstream src(_d.join(_n), std::ios::binary); std::ofstream dst(f.dir().join(f.name()), std::ios::binary); return (bool)(dst << src.rdbuf()); @@ -244,12 +244,12 @@ class File : public fs::Item { inline bool mk() const; inline bool rm() const; - bool mv(File const &f) const { return std::rename(this->full().c_str(), f.full().c_str()); } - bool mv(Dir const &d) const { + bool mv(File const& f) const { return std::rename(this->full().c_str(), f.full().c_str()); } + bool mv(Dir const& d) const { return std::rename(this->full().c_str(), d.join(this->name()).c_str()); } - std::string const &name() const { return _n; } + std::string const& name() const { return _n; } std::string esc() const { return Dir::ESC(full()); } std::string escr() const { return Dir::ESC(is() ? real() : full()); } @@ -260,35 +260,35 @@ class File : public fs::Item { inline uint64_t size() const; - Dir const &dir() const { return _d; } + Dir const& dir() const { return _d; } fs::TimeStamps timeStamps() const { return Dir::TIMESTAMPS(mini()); } - File &operator=(File const &f) = default; - bool operator==(File const &f) const { + File& operator=(File const& f) = default; + bool operator==(File const& f) const { if (is() && f.is()) return real().compare(f.real()) == 0; return full().compare(f.full()) == 0; } explicit operator bool() const { return is(); } - std::string relative(mkn::kul::Dir const &r) const { return this->dir().relative(r); } + std::string relative(mkn::kul::Dir const& r) const { return this->dir().relative(r); } - std::string relative(mkn::kul::File const &r) const { + std::string relative(mkn::kul::File const& r) const { std::string rel = this->dir().relative(r.dir()); rel += Dir::SEP() + r.name(); return rel; } }; -inline bool mkn::kul::Dir::cp(Dir const &d) const { +inline bool mkn::kul::Dir::cp(Dir const& d) const { if (!d.is() && !d.mk()) KEXCEPT(fs::Exception, "Directory: \"" + d.path() + "\" is not valid"); Dir c(d.join(name())); c.mk(); - for (const auto &f : files()) f.cp(c); - for (const auto &dd : dirs()) dd.cp(c); + for (const auto& f : files()) f.cp(c); + for (const auto& dd : dirs()) dd.cp(c); return 1; } -inline std::ostream &operator<<(std::ostream &s, File const &d) { return s << d.full(); } +inline std::ostream& operator<<(std::ostream& s, File const& d) { return s << d.full(); } } // namespace kul } // namespace mkn @@ -299,12 +299,12 @@ namespace os { class PushDir { public: - PushDir(mkn::kul::Dir const &d) { + PushDir(mkn::kul::Dir const& d) { if (!d) KEXCEPTION("PushDir directory does not exist: ") << d; cwd = mkn::kul::env::CWD(); mkn::kul::env::CWD(d.real()); } - PushDir(std::string const &d) : PushDir(mkn::kul::Dir(d)) {} + PushDir(std::string const& d) : PushDir(mkn::kul::Dir(d)) {} ~PushDir() { mkn::kul::env::CWD(cwd); } private: @@ -315,18 +315,18 @@ class PushDir { namespace env { -inline std::string WHERE(char const *c) { - for (const auto &s : mkn::kul::String::SPLIT(env::GET("PATH"), mkn::kul::env::SEP())) { +inline std::string WHERE(char const* c) { + for (const auto& s : mkn::kul::String::SPLIT(env::GET("PATH"), mkn::kul::env::SEP())) { const mkn::kul::Dir d(s); if (d) - for (const auto &f : d.files()) + for (const auto& f : d.files()) if (f.name().compare(c) == 0) return f.real(); } return ""; } -inline bool WHICH(char const *c) { return WHERE(c).size(); } -inline bool WHICH(std::string const &s) { return WHERE(s.c_str()).size(); } +inline bool WHICH(char const* c) { return WHERE(c).size(); } +inline bool WHICH(std::string const& s) { return WHERE(s.c_str()).size(); } } // namespace env } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/os/bsd/proc.os.hpp b/inc/mkn/kul/os/bsd/proc.os.hpp index 0bf0c29..839f537 100644 --- a/inc/mkn/kul/os/bsd/proc.os.hpp +++ b/inc/mkn/kul/os/bsd/proc.os.hpp @@ -59,12 +59,12 @@ class MemGetter { f = KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&inf, &inf_count); } #endif - void virtula(uint64_t &v) { + void virtula(uint64_t& v) { #if defined(__APPLE__) if (!f) v += inf.virtual_size; #endif } - void physical(uint64_t &v) { + void physical(uint64_t& v) { #if defined(__APPLE__) if (!f) v += inf.resident_size; #endif diff --git a/inc/mkn/kul/os/nix/proc.os.hpp b/inc/mkn/kul/os/nix/proc.os.hpp index 21fae9a..4afb56b 100644 --- a/inc/mkn/kul/os/nix/proc.os.hpp +++ b/inc/mkn/kul/os/nix/proc.os.hpp @@ -48,9 +48,9 @@ namespace this_proc { class ProcParser { private: - static inline int PARSE_LINE(char *line); - static inline void VIRTUAL(uint64_t &mem); - static inline void PHYSICAL(uint64_t &mem); // Note: this value is in KB! + static inline int PARSE_LINE(char* line); + static inline void VIRTUAL(uint64_t& mem); + static inline void PHYSICAL(uint64_t& mem); // Note: this value is in KB! friend uint64_t virtualMemory(); friend uint64_t physicalMemory(); diff --git a/inc/mkn/kul/os/nixish/env.hpp b/inc/mkn/kul/os/nixish/env.hpp index 7a96879..04da806 100644 --- a/inc/mkn/kul/os/nixish/env.hpp +++ b/inc/mkn/kul/os/nixish/env.hpp @@ -60,12 +60,12 @@ constexpr size_t KUL_MAX_PATH = PATH_MAX; #error // could not set KUL_MAX_PATH #endif /*_MKN_KUL_MAX_PATH_*/ -inline bool EXISTS(char const *c) { return getenv(c); } -inline std::string GET(char const *c, std::string default_ = "") { - char const *r = getenv(c); +inline bool EXISTS(char const* c) { return getenv(c); } +inline std::string GET(char const* c, std::string default_ = "") { + char const* r = getenv(c); return std::string(r ? r : default_); } -inline void SET(char const *var, char const *val) { setenv(var, val, 1); } +inline void SET(char const* var, char const* val) { setenv(var, val, 1); } inline char SEP() { return ':'; } inline std::string CWD() { @@ -76,7 +76,7 @@ inline std::string CWD() { return str; } -inline bool CWD(std::string const &c) { return chdir(c.c_str()) != -1; } +inline bool CWD(std::string const& c) { return chdir(c.c_str()) != -1; } } // namespace env } // namespace kul diff --git a/inc/mkn/kul/os/nixish/ipc.hpp b/inc/mkn/kul/os/nixish/ipc.hpp index 831c7fe..baf3007 100644 --- a/inc/mkn/kul/os/nixish/ipc.hpp +++ b/inc/mkn/kul/os/nixish/ipc.hpp @@ -55,7 +55,8 @@ namespace ipc { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class IPCCall { @@ -66,7 +67,7 @@ class IPCCall { while (s.size() < 9) s = "0" + s; write(fd, s.c_str(), 9); } - void writeLength(std::string const &m) const { + void writeLength(std::string const& m) const { std::string s = std::to_string(m.size()); while (s.size() < 3) s = "0" + s; write(fd, s.c_str(), 3); @@ -83,8 +84,8 @@ class Server : public IPCCall { } protected: - virtual void handle(std::string const &s) { KLOG(INF) << s; } - void respond(std::string const &s); + virtual void handle(std::string const& s) { KLOG(INF) << s; } + void respond(std::string const& s); public: virtual ~Server() {} @@ -105,13 +106,13 @@ class Server : public IPCCall { if (lp != -1) lp--; } } - Server(const int16_t &_lp = -1) KTHROW(Exception) + Server(const int16_t& _lp = -1) KTHROW(Exception) : lp(_lp), uuid(std::to_string(mkn::kul::this_proc::id()), Dir(_MKN_KUL_IPC_UUID_PREFIX_ + std::string("/pid/"))) { start(); } - Server(std::string const &ui, const int16_t &_lp = -1) KTHROW(Exception) + Server(std::string const& ui, const int16_t& _lp = -1) KTHROW(Exception) : lp(_lp), uuid(ui, Dir(_MKN_KUL_IPC_UUID_PREFIX_)) { start(); } @@ -129,14 +130,14 @@ class Client : public IPCCall { public: virtual ~Client() { stop(); } - Client(std::string const &ui) KTHROW(Exception) : uuid(ui, Dir(_MKN_KUL_IPC_UUID_PREFIX_)) { + Client(std::string const& ui) KTHROW(Exception) : uuid(ui, Dir(_MKN_KUL_IPC_UUID_PREFIX_)) { start(); } - Client(const int16_t &pid) KTHROW(Exception) + Client(const int16_t& pid) KTHROW(Exception) : uuid(std::to_string(pid), Dir(_MKN_KUL_IPC_UUID_PREFIX_ + std::string("/pid/"))) { start(); } - virtual void send(std::string const &m) const KTHROW(Exception) { + virtual void send(std::string const& m) const KTHROW(Exception) { writeLength(m); write(fd, m.c_str(), m.size()); } diff --git a/inc/mkn/kul/os/nixish/os.bot.hpp b/inc/mkn/kul/os/nixish/os.bot.hpp index 854c817..df2d2f5 100644 --- a/inc/mkn/kul/os/nixish/os.bot.hpp +++ b/inc/mkn/kul/os/nixish/os.bot.hpp @@ -35,8 +35,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. void mkn::kul::Dir::rm() const { if (is()) { - for (const auto &a : files()) a.rm(); - for (const auto &a : dirs()) a.rm(); + for (const auto& a : files()) a.rm(); + for (const auto& a : dirs()) a.rm(); remove(real().c_str()); } } @@ -52,13 +52,13 @@ std::string mkn::kul::Dir::ESC(std::string s) { } bool mkn::kul::Dir::is() const { if (path().empty()) return false; - DIR *d = opendir(path().c_str()); + DIR* d = opendir(path().c_str()); if (d) closedir(d); return d; } bool mkn::kul::Dir::mk() const { if (path().empty()) return false; - mkn::kul::Dir const &prnt(parent()); + mkn::kul::Dir const& prnt(parent()); if (_p != prnt.path() && !prnt.is()) parent().mk(); return mkdir(locl().c_str(), 0777) == 0; } @@ -78,7 +78,7 @@ bool mkn::kul::File::rm() const { return false; } bool mkn::kul::File::mk() const { - FILE *pFile = fopen(full().c_str(), "w"); + FILE* pFile = fopen(full().c_str(), "w"); if (pFile != NULL) { fclose(pFile); } @@ -95,7 +95,7 @@ namespace mkn { namespace kul { namespace os { -inline int exec(std::string const &cmd, bool q = false) { +inline int exec(std::string const& cmd, bool q = false) { int r = 0; if (q) r = system(std::string(cmd + " > /dev/null").c_str()); @@ -111,12 +111,14 @@ inline std::string EOL() { return "\n"; } namespace user { inline mkn::kul::Dir home() { return Dir(env::GET("HOME")); } -inline mkn::kul::Dir home(std::string const &app) { return Dir(Dir::JOIN(env::GET("HOME"), "." + app)); } +inline mkn::kul::Dir home(std::string const& app) { + return Dir(Dir::JOIN(env::GET("HOME"), "." + app)); +} } // namespace user namespace env { -inline bool CWD(mkn::kul::Dir const &d) { return chdir(d.path().c_str()) != -1; } +inline bool CWD(mkn::kul::Dir const& d) { return chdir(d.path().c_str()) != -1; } } // namespace env } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/os/nixish/os.top.hpp b/inc/mkn/kul/os/nixish/os.top.hpp index 4bdd77a..0133072 100644 --- a/inc/mkn/kul/os/nixish/os.top.hpp +++ b/inc/mkn/kul/os/nixish/os.top.hpp @@ -52,7 +52,7 @@ namespace fs { class KulTimeStampsResolver { private: - static void GET(char const *const p, uint64_t &a, uint64_t &c, uint64_t &m) { + static void GET(char const* const p, uint64_t& a, uint64_t& c, uint64_t& m) { struct stat att; if (stat(p, &att) != -1) { a = att.st_atime; diff --git a/inc/mkn/kul/os/nixish/proc.hpp b/inc/mkn/kul/os/nixish/proc.hpp index ecc4bef..2b3a7f3 100644 --- a/inc/mkn/kul/os/nixish/proc.hpp +++ b/inc/mkn/kul/os/nixish/proc.hpp @@ -58,7 +58,7 @@ namespace kul { namespace this_proc { inline int32_t id() { return getpid(); } -inline void kill(const int32_t &e) { ::kill(mkn::kul::this_proc::id(), e); } +inline void kill(const int32_t& e) { ::kill(mkn::kul::this_proc::id(), e); } } // namespace this_proc class Process : public mkn::kul::AProcess { @@ -66,7 +66,7 @@ class Process : public mkn::kul::AProcess { int inFd[2], outFd[2], errFd[2], popPip[3]; int cStat; // child status - inline int16_t recall(int16_t const &_s) { + inline int16_t recall(int16_t const& _s) { int ret; while ((ret = (_s)) < 0x0 && (errno == EINTR)) { } @@ -74,10 +74,10 @@ class Process : public mkn::kul::AProcess { } public: - Process(std::string const &cmd, const bool &_wfe = true) : mkn::kul::AProcess(cmd, _wfe) {} - Process(std::string const &cmd, std::string const &path, const bool &_wfe = true) + Process(std::string const& cmd, const bool& _wfe = true) : mkn::kul::AProcess(cmd, _wfe) {} + Process(std::string const& cmd, std::string const& path, const bool& _wfe = true) : mkn::kul::AProcess(cmd, path, _wfe) {} - Process(std::string const &cmd, mkn::kul::Dir const &_d, const bool &_wfe = true) + Process(std::string const& cmd, mkn::kul::Dir const& _d, const bool& _wfe = true) : mkn::kul::AProcess(cmd, (_d ? _d.real() : _d.path()), _wfe) {} bool kill(int16_t k = 6) { if (started()) { @@ -90,7 +90,7 @@ class Process : public mkn::kul::AProcess { protected: int16_t inline child(); - virtual void inline expand(std::string &) const; + virtual void inline expand(std::string&) const; void inline waitForStatus(); void inline waitExit() KTHROW(mkn::kul::proc::ExitException); void inline tearDown(); diff --git a/inc/mkn/kul/os/nixish/signal.hpp b/inc/mkn/kul/os/nixish/signal.hpp index eb48cac..a93c6e0 100644 --- a/inc/mkn/kul/os/nixish/signal.hpp +++ b/inc/mkn/kul/os/nixish/signal.hpp @@ -67,7 +67,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif /* __NetBSD__ */ #endif /* REG_EIP */ -inline void kul_sig_handler(int s, siginfo_t *info, void *v); +inline void kul_sig_handler(int s, siginfo_t* info, void* v); namespace mkn { namespace kul { @@ -75,7 +75,7 @@ namespace this_thread { #include "mkn/kul/os/nixish/src/signal/stacktrace.ipp" inline void print_stacktrace() { - for (auto const &s : stacktrace()) std::cout << s << std::endl; + for (auto const& s : stacktrace()) std::cout << s << std::endl; } } // namespace this_thread @@ -93,15 +93,15 @@ class SignalStatic { sigHandler.sa_sigaction = kul_sig_handler; sigaction(SIGSEGV, &sigHandler, NULL); } - static SignalStatic &INSTANCE() { + static SignalStatic& INSTANCE() { static SignalStatic ss; return ss; } - void abrt(const std::function &f) { + void abrt(const std::function& f) { if (ab.size() == 0) sigaction(SIGABRT, &sigHandler, NULL); ab.push_back(f); } - void intr(const std::function &f) { + void intr(const std::function& f) { if (in.size() == 0) sigaction(SIGINT, &sigHandler, NULL); in.push_back(f); } @@ -109,21 +109,21 @@ class SignalStatic { public: void quiet() { q = 1; } friend class Signal; - friend void ::kul_sig_handler(int s, siginfo_t *i, void *v); + friend void ::kul_sig_handler(int s, siginfo_t* i, void* v); }; class Signal { public: Signal() { mkn::kul::SignalStatic::INSTANCE(); } - Signal &abrt(const std::function &f) { + Signal& abrt(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().abrt(f); return *this; } - Signal &intr(const std::function &f) { + Signal& intr(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().intr(f); return *this; } - Signal &segv(const std::function &f) { + Signal& segv(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().se.push_back(f); return *this; } diff --git a/inc/mkn/kul/os/nixish/src/os/dir/dirs.ipp b/inc/mkn/kul/os/nixish/src/os/dir/dirs.ipp index 24949a9..8c18cfd 100644 --- a/inc/mkn/kul/os/nixish/src/os/dir/dirs.ipp +++ b/inc/mkn/kul/os/nixish/src/os/dir/dirs.ipp @@ -34,8 +34,8 @@ std::vector mkn::kul::Dir::dirs(bool incHidden) const KTHROW(fs:: if (!is()) KEXCEPT(fs::Exception, "Directory : \"" + path() + "\" does not exist"); std::vector dirs; - DIR *dir = opendir(real().c_str()); - struct dirent *entry = readdir(dir); + DIR* dir = opendir(real().c_str()); + struct dirent* entry = readdir(dir); while (entry != NULL) { std::string d(entry->d_name); mkn::kul::Dir dd(JOIN(real(), entry->d_name)); diff --git a/inc/mkn/kul/os/nixish/src/os/dir/files.ipp b/inc/mkn/kul/os/nixish/src/os/dir/files.ipp index e07551f..fd5b431 100644 --- a/inc/mkn/kul/os/nixish/src/os/dir/files.ipp +++ b/inc/mkn/kul/os/nixish/src/os/dir/files.ipp @@ -34,16 +34,16 @@ std::vector mkn::kul::Dir::files(bool recursive) const KTHROW(fs if (!is()) KEXCEPT(fs::Exception, "Directory : \"" + path() + "\" does not exist"); std::vector fs; - DIR *dir = opendir(path().c_str()); - struct dirent *entry = readdir(dir); + DIR* dir = opendir(path().c_str()); + struct dirent* entry = readdir(dir); while (entry != NULL) { - if ( !mkn::kul::Dir(JOIN(real(), entry->d_name)).is()) fs.push_back(File(entry->d_name, *this)); + if (!mkn::kul::Dir(JOIN(real(), entry->d_name)).is()) fs.push_back(File(entry->d_name, *this)); entry = readdir(dir); } closedir(dir); if (recursive) { - for (mkn::kul::Dir const &d : dirs()) { - const std::vector &tFs = d.files(true); + for (mkn::kul::Dir const& d : dirs()) { + const std::vector& tFs = d.files(true); fs.insert(fs.end(), tFs.begin(), tFs.end()); } } diff --git a/inc/mkn/kul/os/nixish/src/proc/child.ipp b/inc/mkn/kul/os/nixish/src/proc/child.ipp index ba9dc6a..562fe7f 100644 --- a/inc/mkn/kul/os/nixish/src/proc/child.ipp +++ b/inc/mkn/kul/os/nixish/src/proc/child.ipp @@ -34,8 +34,8 @@ int16_t mkn::kul::Process::child() { std::string _s(toString()); expand(_s); std::vector cli(mkn::kul::cli::asArgs(_s)); - std::vector argV; - for (auto &a : cli) argV.push_back(&a[0]); + std::vector argV; + for (auto& a : cli) argV.push_back(&a[0]); argV.push_back(NULL); return execvp(cli[0].c_str(), &argV[0]); } diff --git a/inc/mkn/kul/os/nixish/src/proc/run.ipp b/inc/mkn/kul/os/nixish/src/proc/run.ipp index 5418a6a..c1c655e 100644 --- a/inc/mkn/kul/os/nixish/src/proc/run.ipp +++ b/inc/mkn/kul/os/nixish/src/proc/run.ipp @@ -133,8 +133,7 @@ void mkn::kul::Process::run() KTHROW(mkn::kul::proc::Exception) { /* SETUP EnvVars */ // SET ENV, it's a forked process so it doesn't matter - // it'll die soon, like you. - for (auto const &ev : vars()) - env::SET(ev.first.c_str(), ev.second.c_str()); + for (auto const& ev : vars()) env::SET(ev.first.c_str(), ev.second.c_str()); if (!this->directory().empty()) mkn::kul::env::CWD(this->directory()); exit(this->child()); diff --git a/inc/mkn/kul/os/nixish/src/signal/handler.ipp b/inc/mkn/kul/os/nixish/src/signal/handler.ipp index 83f04b0..c326843 100644 --- a/inc/mkn/kul/os/nixish/src/signal/handler.ipp +++ b/inc/mkn/kul/os/nixish/src/signal/handler.ipp @@ -30,19 +30,19 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // IWYU pragma: private, include "mkn/kul/signal.hpp" -void kul_sig_handler(int s, siginfo_t *info, void *v) { +void kul_sig_handler(int s, siginfo_t* info, void* v) { if (info->si_pid == 0 || info->si_pid == mkn::kul::this_proc::id()) { if (s == SIGABRT) - for (auto &f : mkn::kul::SignalStatic::INSTANCE().ab) f(s); + for (auto& f : mkn::kul::SignalStatic::INSTANCE().ab) f(s); if (s == SIGINT) - for (auto &f : mkn::kul::SignalStatic::INSTANCE().in) f(s); + for (auto& f : mkn::kul::SignalStatic::INSTANCE().in) f(s); if (s == SIGSEGV) - for (auto &f : mkn::kul::SignalStatic::INSTANCE().se) f(s); - if (s == SIGSEGV && !mkn::kul::SignalStatic::INSTANCE().q) { + for (auto& f : mkn::kul::SignalStatic::INSTANCE().se) f(s); + if (s == SIGSEGV && !mkn::kul::SignalStatic::INSTANCE().q) { auto tid = mkn::kul::this_thread::id(); - ucontext_t *uc = (ucontext_t *)v; + ucontext_t* uc = (ucontext_t*)v; printf("[bt] Stacktrace:\n"); - for (auto const &st : mkn::kul::this_thread::stacktrace(uc)) KOUT(NON) << tid << " : " << st; + for (auto const& st : mkn::kul::this_thread::stacktrace(uc)) KOUT(NON) << tid << " : " << st; } exit(s); } diff --git a/inc/mkn/kul/os/nixish/src/signal/stacktrace.ipp b/inc/mkn/kul/os/nixish/src/signal/stacktrace.ipp index 85c234b..2932235 100644 --- a/inc/mkn/kul/os/nixish/src/signal/stacktrace.ipp +++ b/inc/mkn/kul/os/nixish/src/signal/stacktrace.ipp @@ -32,8 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // This file is included by other files and is not in itself syntactically correct. -inline std::vector stacktrace(ucontext_t */*uc*/ = nullptr, int /*start*/ = 2) { - +inline std::vector stacktrace(ucontext_t* /*uc*/ = nullptr, int /*start*/ = 2) { // std::vector stacktrace(ucontext_t *uc, int start) { // constexpr size_t max_trace = 128; std::string const invalid = "??:0"; @@ -142,7 +141,7 @@ inline std::vector stacktrace(ucontext_t */*uc*/ = nullptr, int /*s constexpr size_t SIZE = 256; int i; - void *buffer[SIZE]; + void* buffer[SIZE]; int nptrs = backtrace(buffer, SIZE); diff --git a/inc/mkn/kul/os/nixish/sys.hpp b/inc/mkn/kul/os/nixish/sys.hpp index b64e17c..1dbb184 100644 --- a/inc/mkn/kul/os/nixish/sys.hpp +++ b/inc/mkn/kul/os/nixish/sys.hpp @@ -45,22 +45,15 @@ namespace mkn { namespace kul { namespace sys { -template +template class SharedFunction; class SharedLibrary { - template - friend class SharedFunction; - - private: - bool _loaded = 0; - void *_handle; - const mkn::kul::File _f; - public: - SharedLibrary(mkn::kul::File const &f) KTHROW(Exception) : _f(f) { + SharedLibrary(mkn::kul::File const& f, int const flags = __MKN_KUL_SYS_DLOPEN__) KTHROW(Exception) + : _f(f) { if (!_f) KEXCEPSTREAM << "Library attempted to be loaded does not exist: " << _f.full(); - _handle = dlopen(_f.real().c_str(), __MKN_KUL_SYS_DLOPEN__); + _handle = dlopen(_f.real().c_str(), flags); if (!_handle) KEXCEPSTREAM << "Cannot load library: " << f << " - Error: " << dlerror(); _loaded = 1; } @@ -68,50 +61,59 @@ class SharedLibrary { if (_loaded) dlclose(_handle); dlerror(); } - const mkn::kul::File file() const { return _f; } -}; + auto& file() const { return _f; } -template -class SharedFunction { private: - F *_funcP; - SharedLibrary &_lib; + bool _loaded = 0; + void* _handle; + mkn::kul::File const _f; + + template + friend class SharedFunction; +}; +template +class SharedFunction { public: - SharedFunction(SharedLibrary &lib, std::string const &f) KTHROW(Exception) : _lib(lib) { - _funcP = (F *)dlsym(_lib._handle, f.c_str()); - char const *dlsym_error = dlerror(); + SharedFunction(SharedLibrary& lib, std::string const& f) KTHROW(Exception) : _lib(lib) { + _funcP = (F*)dlsym(_lib._handle, f.c_str()); + char const* dlsym_error = dlerror(); if (dlsym_error) KEXCEPSTREAM << "Cannot load symbol create " << dlsym_error; } ~SharedFunction() { dlerror(); } - F *pointer() { return _funcP; } -}; - -template -class SharedClass { - typedef T *construct_t(); - typedef void destruct_t(T *t); + auto pointer() { return _funcP; } private: - SharedLibrary _lib; - SharedFunction _c; - SharedFunction _d; + F* _funcP; + SharedLibrary& _lib; +}; +template +class SharedClass { public: - SharedClass(mkn::kul::File const &f, std::string const &c, std::string const &d) KTHROW(Exception) + SharedClass(mkn::kul::File const& f, std::string const& c, std::string const& d) KTHROW(Exception) : _lib(f), _c(_lib, c), _d(_lib, d) {} + virtual ~SharedClass() {} protected: - void construct(T *&t) KTHROW(Exception) { + void construct(T*& t) KTHROW(Exception) { t = _c.pointer()(); if (!t) KEXCEPSTREAM << "Dynamically loaded class was not created"; } - void destruct(T *&t) { + void destruct(T*& t) { _d.pointer()(t); t = nullptr; if (t) KEXCEPSTREAM << "Dynamically loaded class was not destroyed"; } + + private: + typedef T* construct_t(); + typedef void destruct_t(T* t); + + SharedLibrary _lib; + SharedFunction _c; + SharedFunction _d; }; } // namespace sys diff --git a/inc/mkn/kul/os/nixish/threads.os.hpp b/inc/mkn/kul/os/nixish/threads.os.hpp index 60ac901..9802745 100644 --- a/inc/mkn/kul/os/nixish/threads.os.hpp +++ b/inc/mkn/kul/os/nixish/threads.os.hpp @@ -90,29 +90,28 @@ class Thread : public threading::AThread { private: std::function func; pthread_t thr; - static void *threadFunction(void *th) { - ((Thread *)th)->act(); + static void* threadFunction(void* th) { + ((Thread*)th)->act(); return 0; } void act() { try { func(); - } catch (const std::exception &e) { + } catch (const std::exception& e) { ep = std::current_exception(); } f = 1; } public: - Thread(const std::function &_func) : func(_func) {} + Thread(const std::function& _func) : func(_func) {} template - Thread(const T &t) : func(std::bind((void (T::*)()) & T::operator(), t)) {} + Thread(const T& t) : func(std::bind((void(T::*)()) & T::operator(), t)) {} template - Thread(const std::reference_wrapper &r) - : func(std::bind((void (T::*)()) & T::operator(), r)) {} + Thread(const std::reference_wrapper& r) : func(std::bind((void(T::*)()) & T::operator(), r)) {} template - Thread(const std::reference_wrapper &r) - : func(std::bind((void (T::*)() const) & T::operator(), r)) {} + Thread(const std::reference_wrapper& r) + : func(std::bind((void(T::*)() const) & T::operator(), r)) {} virtual ~Thread() {} bool detach() { return pthread_detach(thr); } diff --git a/inc/mkn/kul/os/threads.hpp b/inc/mkn/kul/os/threads.hpp index f7d249a..6e3cfb2 100644 --- a/inc/mkn/kul/os/threads.hpp +++ b/inc/mkn/kul/os/threads.hpp @@ -48,13 +48,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace mkn { namespace kul { namespace this_thread { -inline void sleep(unsigned long const &millis) { +inline void sleep(unsigned long const& millis) { std::this_thread::sleep_for(std::chrono::milliseconds(millis)); } -inline void uSleep(unsigned long const µs) { +inline void uSleep(unsigned long const& micros) { std::this_thread::sleep_for(std::chrono::microseconds(micros)); } -inline void nSleep(unsigned long const &nanos) { +inline void nSleep(unsigned long const& nanos) { std::this_thread::sleep_for(std::chrono::nanoseconds(nanos)); } } // namespace this_thread @@ -65,11 +65,12 @@ inline void nSleep(unsigned long const &nanos) { namespace threading { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class InterruptionException : public Exception { public: - InterruptionException(char const *f, uint16_t const &l, std::string const &s) + InterruptionException(char const* f, uint16_t const& l, std::string const& s) : Exception(f, l, s) {} }; @@ -103,4 +104,3 @@ class AThread { #endif #endif // _MKN_KUL_OS_THREADS_HPP_ - diff --git a/inc/mkn/kul/os/win/cli.os.hpp b/inc/mkn/kul/os/win/cli.os.hpp index 90a4acd..976514a 100644 --- a/inc/mkn/kul/os/win/cli.os.hpp +++ b/inc/mkn/kul/os/win/cli.os.hpp @@ -44,7 +44,7 @@ namespace cli { inline std::string hidden(std::string const& t) { #include "mkn/kul/os/win/src/cli/hidden.cpp" } -inline void show() { +inline void show(){ #include "mkn/kul/os/win/src/cli/show.cpp" } #else diff --git a/inc/mkn/kul/os/win/env.hpp b/inc/mkn/kul/os/win/env.hpp index 6af91d1..95a7b85 100644 --- a/inc/mkn/kul/os/win/env.hpp +++ b/inc/mkn/kul/os/win/env.hpp @@ -62,17 +62,17 @@ inline std::string EOL() { #endif } -inline bool EXISTS(char const *c) { +inline bool EXISTS(char const* c) { bool set = 0; - char *r = 0; + char* r = 0; size_t len; _dupenv_s(&r, &len, c); set = r; if (len) free(r); return set; } -inline std::string GET(char const *c, std::string default_ = "") { - char *r; +inline std::string GET(char const* c, std::string default_ = "") { + char* r; size_t len; _dupenv_s(&r, &len, c); if (len) { @@ -83,7 +83,7 @@ inline std::string GET(char const *c, std::string default_ = "") { return default_; } -inline void SET(char const *var, char const *val) { +inline void SET(char const* var, char const* val) { _putenv(std::string(std::string(var) + "=" + std::string(val)).c_str()); } @@ -96,7 +96,7 @@ inline std::string CWD() { return str; } -inline bool CWD(std::string const &c) { return _chdir(c.c_str()) != -1; } +inline bool CWD(std::string const& c) { return _chdir(c.c_str()) != -1; } } // namespace env } // namespace kul diff --git a/inc/mkn/kul/os/win/ipc.hpp b/inc/mkn/kul/os/win/ipc.hpp index dc204c5..86e86dc 100644 --- a/inc/mkn/kul/os/win/ipc.hpp +++ b/inc/mkn/kul/os/win/ipc.hpp @@ -51,7 +51,8 @@ namespace ipc { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class Server { @@ -59,7 +60,7 @@ class Server { int16_t lp; const std::string uuid; HANDLE hPipe; - TCHAR *pchRequest = 0; + TCHAR* pchRequest = 0; void start() KTHROW(Exception) { DWORD dwThreadId = 0; @@ -76,11 +77,12 @@ class Server { 0, // client time-out NULL); // default security attribute if (hPipe == INVALID_HANDLE_VALUE) - KEXCEPT(mkn::kul::ipc::Exception, "CreateNamedPipe failed: " + std::to_string(GetLastError())); + KEXCEPT(mkn::kul::ipc::Exception, + "CreateNamedPipe failed: " + std::to_string(GetLastError())); } protected: - virtual void handle(std::string const &s) { KOUT(INF) << s; } + virtual void handle(std::string const& s) { KOUT(INF) << s; } public: virtual ~Server() { @@ -89,7 +91,7 @@ class Server { void listen() KTHROW(Exception) { while (lp) { HANDLE hHeap = GetProcessHeap(); - pchRequest = (TCHAR *)HeapAlloc(hHeap, 0, KUL_IPC_BUFFER * sizeof(TCHAR)); + pchRequest = (TCHAR*)HeapAlloc(hHeap, 0, KUL_IPC_BUFFER * sizeof(TCHAR)); bool fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); if (!fConnected) continue; @@ -109,12 +111,13 @@ class Server { } CloseHandle(hPipe); } - Server(const int16_t &lp = -1) KTHROW(Exception) + Server(const int16_t& lp = -1) KTHROW(Exception) : lp(lp), - uuid(_MKN_KUL_IPC_UUID_PREFIX_ + std::string("pid\\") + std::to_string(mkn::kul::this_proc::id())) { + uuid(_MKN_KUL_IPC_UUID_PREFIX_ + std::string("pid\\") + + std::to_string(mkn::kul::this_proc::id())) { start(); } - Server(std::string const &ui, const int16_t &lp = -1) KTHROW(Exception) + Server(std::string const& ui, const int16_t& lp = -1) KTHROW(Exception) : uuid(_MKN_KUL_IPC_UUID_PREFIX_ + ui), lp(lp) { start(); } @@ -156,17 +159,20 @@ class Client { public: virtual ~Client() { stop(); } - Client(std::string const &ui) KTHROW(Exception) : uuid(_MKN_KUL_IPC_UUID_PREFIX_ + ui) { start(); } - Client(const int16_t &pid) KTHROW(Exception) + Client(std::string const& ui) KTHROW(Exception) : uuid(_MKN_KUL_IPC_UUID_PREFIX_ + ui) { + start(); + } + Client(const int16_t& pid) KTHROW(Exception) : uuid(_MKN_KUL_IPC_UUID_PREFIX_ + std::string("pid\\") + std::to_string(pid)) { start(); } - virtual void send(std::string const &m) const KTHROW(Exception) { + virtual void send(std::string const& m) const KTHROW(Exception) { DWORD cbToWrite, cbWritten; LPTSTR lpvMessage = _strdup(m.c_str()); cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof(TCHAR); if (!WriteFile(hPipe, lpvMessage, cbToWrite, &cbWritten, NULL)) - KEXCEPT(mkn::kul::ipc::Exception, "WriteFile to pipe failed: " + std::to_string(GetLastError())); + KEXCEPT(mkn::kul::ipc::Exception, + "WriteFile to pipe failed: " + std::to_string(GetLastError())); } }; diff --git a/inc/mkn/kul/os/win/os.bot.hpp b/inc/mkn/kul/os/win/os.bot.hpp index 86471ab..d4bce08 100644 --- a/inc/mkn/kul/os/win/os.bot.hpp +++ b/inc/mkn/kul/os/win/os.bot.hpp @@ -33,8 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. void mkn::kul::Dir::rm() const { if (is()) { - for (const auto &a : files()) a.rm(); - for (const auto &a : dirs()) a.rm(); + for (const auto& a : files()) a.rm(); + for (const auto& a : dirs()) a.rm(); _rmdir(path().c_str()); } } @@ -56,13 +56,15 @@ bool mkn::kul::Dir::is() const { } bool mkn::kul::Dir::mk() const { if (path().empty()) return false; - mkn::kul::Dir const &prnt(parent()); + mkn::kul::Dir const& prnt(parent()); if (_p != prnt.path() && !prnt.is()) parent().mk(); return CreateDirectory(locl().c_str(), NULL); } bool mkn::kul::Dir::root() const { return is() && real().size() == 3; } -bool mkn::kul::File::is() const { return !name().empty() && (bool)std::ifstream(_d.join(_n).c_str()); } +bool mkn::kul::File::is() const { + return !name().empty() && (bool)std::ifstream(_d.join(_n).c_str()); +} bool mkn::kul::File::rm() const { if (is()) { _unlink(_d.join(_n).c_str()); @@ -72,7 +74,7 @@ bool mkn::kul::File::rm() const { } bool mkn::kul::File::mk() const { - FILE *pFile; + FILE* pFile; fopen_s(&pFile, full().c_str(), "w"); if (pFile != NULL) { fclose(pFile); @@ -98,7 +100,7 @@ namespace mkn { namespace kul { namespace os { -inline uint16_t exec(std::string const &cmd, bool q = false) { +inline uint16_t exec(std::string const& cmd, bool q = false) { if (q) { return system(std::string(cmd + " > nul").c_str()); } @@ -121,12 +123,12 @@ inline mkn::kul::Dir home() { if (h.size()) return mkn::kul::Dir(h); return mkn::kul::Dir(std::string(env::GET("HOMEDRIVE")) + std::string(env::GET("HOMEPATH"))); } -inline mkn::kul::Dir home(std::string const &app) { return mkn::kul::Dir(home().join(app)); } +inline mkn::kul::Dir home(std::string const& app) { return mkn::kul::Dir(home().join(app)); } } // namespace user namespace env { -inline bool CWD(mkn::kul::Dir const &d) { return _chdir(d.path().c_str()) != -1; } +inline bool CWD(mkn::kul::Dir const& d) { return _chdir(d.path().c_str()) != -1; } } // namespace env } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/os/win/os.top.hpp b/inc/mkn/kul/os/win/os.top.hpp index e1ec89d..79b964e 100644 --- a/inc/mkn/kul/os/win/os.top.hpp +++ b/inc/mkn/kul/os/win/os.top.hpp @@ -52,7 +52,7 @@ namespace fs { class KulTimeStampsResolver { private: - static uint64_t FileTimeToPOSIX(FILETIME &ft) { + static uint64_t FileTimeToPOSIX(FILETIME& ft) { LARGE_INTEGER date, adjust; date.HighPart = ft.dwHighDateTime; date.LowPart = ft.dwLowDateTime; @@ -60,7 +60,7 @@ class KulTimeStampsResolver { date.QuadPart -= adjust.QuadPart; return (uint64_t)date.QuadPart / 10000000; } - static void GET(char const *const p, uint64_t &a, uint64_t &c, uint64_t &m) { + static void GET(char const* const p, uint64_t& a, uint64_t& c, uint64_t& m) { WIN32_FIND_DATA ffd; HANDLE h = FindFirstFile(TEXT(p), &ffd); if (h) { diff --git a/inc/mkn/kul/os/win/proc.hpp b/inc/mkn/kul/os/win/proc.hpp index 6d90156..b71640e 100644 --- a/inc/mkn/kul/os/win/proc.hpp +++ b/inc/mkn/kul/os/win/proc.hpp @@ -54,10 +54,10 @@ class MemGetter { private: PROCESS_MEMORY_COUNTERS_EX pmc; MemGetter() { - GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof(pmc)); + GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)); } - void virtula(uint64_t &v) { v += pmc.PrivateUsage; } - void physical(uint64_t &v) { v += pmc.WorkingSetSize; } + void virtula(uint64_t& v) { v += pmc.PrivateUsage; } + void physical(uint64_t& v) { v += pmc.WorkingSetSize; } friend uint64_t virtualMemory(); friend uint64_t physicalMemory(); friend uint64_t totalMemory(); @@ -83,7 +83,7 @@ inline uint64_t totalMemory() { inline uint16_t cpuLoad() { return 0; } inline int32_t id() { return GetCurrentProcessId(); } -inline void kill(const int32_t &e) { +inline void kill(const int32_t& e) { TerminateProcess(OpenProcess(PROCESS_TERMINATE, 0, mkn::kul::this_proc::id()), 128 + e); } } // namespace this_proc @@ -103,10 +103,10 @@ class Process : public mkn::kul::AProcess { HANDLE revent = CreateEvent(0, 1, 0, 0); public: - Process(std::string const &cmd, const bool &wfe = true) : mkn::kul::AProcess(cmd, wfe) {} - Process(std::string const &cmd, std::string const &path, const bool &wfe = true) + Process(std::string const& cmd, const bool& wfe = true) : mkn::kul::AProcess(cmd, wfe) {} + Process(std::string const& cmd, std::string const& path, const bool& wfe = true) : mkn::kul::AProcess(cmd, path, wfe) {} - Process(std::string const &cmd, mkn::kul::Dir const &d, const bool &wfe = true) + Process(std::string const& cmd, mkn::kul::Dir const& d, const bool& wfe = true) : mkn::kul::AProcess(cmd, d ? d.real() : d.path(), wfe) {} ~Process() { tearDown(); } bool kill(int16_t k = 6) { @@ -126,7 +126,7 @@ class Process : public mkn::kul::AProcess { void tearDown() { #include "mkn/kul/os/win/src/proc/tearDown.cpp" } - virtual void expand(std::string &s) const { + virtual void expand(std::string& s) const { #include "mkn/kul/os/win/src/proc/expand.cpp" } void run() KTHROW(mkn::kul::Exception) { @@ -134,7 +134,7 @@ class Process : public mkn::kul::AProcess { } #else void tearDown(); - virtual void expand(std::string &s) const; + virtual void expand(std::string& s) const; void run() KTHROW(mkn::kul::Exception); #endif }; diff --git a/inc/mkn/kul/os/win/signal.hpp b/inc/mkn/kul/os/win/signal.hpp index 4131f03..36ce9dd 100644 --- a/inc/mkn/kul/os/win/signal.hpp +++ b/inc/mkn/kul/os/win/signal.hpp @@ -37,18 +37,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #pragma comment(lib, "Dbghelp.lib") -void kul_real_se_handler(EXCEPTION_POINTERS *pExceptionInfo); +void kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo); LONG WINAPI kul_top_level_exception_handler(PEXCEPTION_POINTERS pExceptionInfo) { kul_real_se_handler(pExceptionInfo); return (LONG)0L; } -void kul_se_translator_function(unsigned int sig, EXCEPTION_POINTERS *pExceptionInfo) { +void kul_se_translator_function(unsigned int sig, EXCEPTION_POINTERS* pExceptionInfo) { kul_real_se_handler(pExceptionInfo); } -void kul_sig_function_handler(uint16_t const &s); +void kul_sig_function_handler(uint16_t const& s); BOOL WINAPI kul_sigint_function(DWORD d) { switch (d) { @@ -73,7 +73,7 @@ std::vector stacktrace() { } void print_stacktrace() { - for (auto const &s : stacktrace()) std::cout << s << std::endl; + for (auto const& s : stacktrace()) std::cout << s << std::endl; } } // namespace this_thread @@ -84,13 +84,13 @@ class SignalStatic { bool q = 0; std::vector> ab, in, se; friend class Signal; - friend void ::kul_real_se_handler(EXCEPTION_POINTERS *pExceptionInfo); - friend void ::kul_sig_function_handler(uint16_t const &s); - static SignalStatic &INSTANCE() { + friend void ::kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo); + friend void ::kul_sig_function_handler(uint16_t const& s); + static SignalStatic& INSTANCE() { static SignalStatic ss; return ss; } - void intr(const std::function &f) { + void intr(const std::function& f) { if (in.size() == 0) if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)kul_sigint_function, TRUE)) KEXCEPTION("Unable to install signal handler!"); @@ -109,15 +109,15 @@ class Signal { _set_se_translator(kul_se_translator_function); SetUnhandledExceptionFilter(kul_top_level_exception_handler); } - Signal &abrt(const std::function &f) { + Signal& abrt(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().ab.push_back(f); return *this; } - Signal &intr(const std::function &f) { + Signal& intr(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().intr(f); return *this; } - Signal &segv(const std::function &f) { + Signal& segv(const std::function& f) { mkn::kul::SignalStatic::INSTANCE().se.push_back(f); return *this; } @@ -127,19 +127,19 @@ class Signal { } // namespace kul } // namespace mkn -void kul_sig_function_handler(uint16_t const &s) { +void kul_sig_function_handler(uint16_t const& s) { if (s == 2) - for (auto &f : mkn::kul::SignalStatic::INSTANCE().in) f(s); + for (auto& f : mkn::kul::SignalStatic::INSTANCE().in) f(s); if (s == 11) - for (auto &f : mkn::kul::SignalStatic::INSTANCE().se) f(s); + for (auto& f : mkn::kul::SignalStatic::INSTANCE().se) f(s); } #ifndef _MKN_KUL_COMPILED_LIB_ -void kul_real_se_handler(EXCEPTION_POINTERS *pExceptionInfo) { +void kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo) { #include "mkn/kul/os/win/src/signal/se_handler.cpp" } #else -void kul_real_se_handler(EXCEPTION_POINTERS *pExceptionInfo); +void kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo); #endif #endif /* _MKN_KUL_OS_WIN_SIGNAL_HPP_ */ diff --git a/inc/mkn/kul/os/win/src/cli/hidden.cpp b/inc/mkn/kul/os/win/src/cli/hidden.cpp index bcef5bc..dcec1da 100644 --- a/inc/mkn/kul/os/win/src/cli/hidden.cpp +++ b/inc/mkn/kul/os/win/src/cli/hidden.cpp @@ -38,7 +38,7 @@ if (!t.empty()) std::cout << t << std::endl; HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; GetConsoleMode(hStdin, &mode); -SetConsoleMode(hStdin, mode &(~ENABLE_ECHO_INPUT)); +SetConsoleMode(hStdin, mode&(~ENABLE_ECHO_INPUT)); std::string s; std::getline(std::cin, s); SetConsoleMode(hStdin, mode); diff --git a/inc/mkn/kul/os/win/src/proc/run.cpp b/inc/mkn/kul/os/win/src/proc/run.cpp index 6d0fd24..3fe3bb5 100644 --- a/inc/mkn/kul/os/win/src/proc/run.cpp +++ b/inc/mkn/kul/os/win/src/proc/run.cpp @@ -44,7 +44,7 @@ sa.lpSecurityDescriptor = NULL; std::stringstream ss; ss << this; -const ULONG &pipeID = PIPE_ID(); +const ULONG& pipeID = PIPE_ID(); std::string pipeOut = "\\\\.\\Pipe\\kul_proc_out." + std::to_string(this_proc::id()) + "." + ss.str() + "." + std::to_string(pipeID); std::string pipeErr = "\\\\.\\Pipe\\kul_proc_err." + std::to_string(this_proc::id()) + "." + @@ -53,9 +53,10 @@ std::string pipeIn = "\\\\.\\Pipe\\kul_proc_in." + std::to_string(this_proc::id( ss.str() + "." + std::to_string(pipeID); LPSTR lPipeOut = _strdup(pipeOut.c_str()); -g_hChildStd_OUT_Wr = ::CreateNamedPipeA(lPipeOut, PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, - __MKN_KUL_PROCESS_BUFFER__, __MKN_KUL_PROCESS_BUFFER__, 0, &sa); +g_hChildStd_OUT_Wr = + ::CreateNamedPipeA(lPipeOut, PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, + __MKN_KUL_PROCESS_BUFFER__, __MKN_KUL_PROCESS_BUFFER__, 0, &sa); if (!g_hChildStd_OUT_Wr) error(__LINE__, "CreatePipe failed"); g_hChildStd_OUT_Rd = ::CreateFileA(lPipeOut, GENERIC_READ, 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0); @@ -64,9 +65,10 @@ if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) error(__LINE__, "SetHandleInformation failed"); LPSTR lPipeErr = _strdup(pipeErr.c_str()); -g_hChildStd_ERR_Wr = ::CreateNamedPipeA(lPipeErr, PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, - __MKN_KUL_PROCESS_BUFFER__, __MKN_KUL_PROCESS_BUFFER__, 0, &sa); +g_hChildStd_ERR_Wr = + ::CreateNamedPipeA(lPipeErr, PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, + __MKN_KUL_PROCESS_BUFFER__, __MKN_KUL_PROCESS_BUFFER__, 0, &sa); if (!g_hChildStd_ERR_Wr) error(__LINE__, "CreatePipe failed"); g_hChildStd_ERR_Rd = ::CreateFileA(lPipeErr, GENERIC_READ, 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0); @@ -117,7 +119,7 @@ for (lpszVariable = (LPTSTR)lpvEnv; *lpszVariable; lpszVariable++) { } if (FreeEnvironmentStrings(lpvEnv) == 0) error(__LINE__, "FreeEnvironmentStrings() failed"); -char const *dir = directory().empty() ? 0 : directory().c_str(); +char const* dir = directory().empty() ? 0 : directory().c_str(); std::string cmd(toString()); expand(cmd); @@ -126,9 +128,9 @@ LPSTR szCmdline = _strdup(cmd.c_str()); if (vars().size()) { std::wstring newEnv; - for (auto &evs : vars()) env[evs.first] = evs.second; + for (auto& evs : vars()) env[evs.first] = evs.second; - for (auto &evs : env) { + for (auto& evs : env) { std::string name = evs.first; std::string var(name + "=" + evs.second); newEnv += std::wstring(var.begin(), var.end()); diff --git a/inc/mkn/kul/os/win/src/signal/se_handler.cpp b/inc/mkn/kul/os/win/src/signal/se_handler.cpp index 4289584..30d2e0a 100644 --- a/inc/mkn/kul/os/win/src/signal/se_handler.cpp +++ b/inc/mkn/kul/os/win/src/signal/se_handler.cpp @@ -34,12 +34,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // void kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo){ -std::string const &tid(mkn::kul::this_thread::id()); +std::string const& tid(mkn::kul::this_thread::id()); uint16_t sig = pExceptionInfo->ExceptionRecord->ExceptionCode; if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) kul_sig_function_handler(sig = 11); -if ( !mkn::kul::SignalStatic::INSTANCE().q) { +if (!mkn::kul::SignalStatic::INSTANCE().q) { HANDLE process = GetCurrentProcess(); SymInitialize(process, NULL, TRUE); @@ -75,8 +75,8 @@ if ( !mkn::kul::SignalStatic::INSTANCE().q) { stack_frame.AddrFrame.Mode = AddrModeFlat; stack_frame.AddrStack.Mode = AddrModeFlat; - SYMBOL_INFO *symbol; - symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1); + SYMBOL_INFO* symbol; + symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1); symbol->MaxNameLen = 255; symbol->SizeOfStruct = sizeof(SYMBOL_INFO); std::cout << "[bt] Stacktrace:" << std::endl; diff --git a/inc/mkn/kul/os/win/sys.hpp b/inc/mkn/kul/os/win/sys.hpp index 8d899cb..e4d18de 100644 --- a/inc/mkn/kul/os/win/sys.hpp +++ b/inc/mkn/kul/os/win/sys.hpp @@ -52,7 +52,7 @@ class SharedLibrary { const mkn::kul::File _f; public: - SharedLibrary(mkn::kul::File const &f) KTHROW(Exception) : _f(f) { + SharedLibrary(mkn::kul::File const& f) KTHROW(Exception) : _f(f) { if (!_f) KEXCEPSTREAM << "Library attempted to be loaded does not exist: " << _f.full(); LPSTR lib = _strdup(_f.real().c_str()); _handle = LoadLibrary(lib); @@ -68,23 +68,23 @@ class SharedLibrary { template class SharedFunction { private: - F *_funcP; - SharedLibrary &_lib; + F* _funcP; + SharedLibrary& _lib; public: - SharedFunction(SharedLibrary &lib, std::string const &f) KTHROW(Exception) : _lib(lib) { + SharedFunction(SharedLibrary& lib, std::string const& f) KTHROW(Exception) : _lib(lib) { LPSTR func = _strdup(f.c_str()); - _funcP = (F *)GetProcAddress(_lib._handle, func); + _funcP = (F*)GetProcAddress(_lib._handle, func); if (!_funcP) KEXCEPSTREAM << "Cannot load symbol create " << GetLastError(); } - F *pointer() { return _funcP; } + F* pointer() { return _funcP; } }; template class SharedClass { - typedef T *construct_t(); - typedef void destruct_t(T *t); + typedef T* construct_t(); + typedef void destruct_t(T* t); private: SharedLibrary _lib; @@ -92,16 +92,16 @@ class SharedClass { SharedFunction _d; public: - SharedClass(mkn::kul::File const &f, std::string const &c, std::string const &d) KTHROW(Exception) + SharedClass(mkn::kul::File const& f, std::string const& c, std::string const& d) KTHROW(Exception) : _lib(f), _c(_lib, c), _d(_lib, d) {} virtual ~SharedClass() {} protected: - void construct(T *&t) KTHROW(Exception) { + void construct(T*& t) KTHROW(Exception) { t = _c.pointer()(); if (!t) KEXCEPSTREAM << "Dynamically loaded class was not created"; } - void destruct(T *&t) { + void destruct(T*& t) { _d.pointer()(t); t = nullptr; if (t) KEXCEPSTREAM << "Dynamically loaded class was not destroyed"; diff --git a/inc/mkn/kul/os/win/threads.os.hpp b/inc/mkn/kul/os/win/threads.os.hpp index cbc2136..cb542b7 100644 --- a/inc/mkn/kul/os/win/threads.os.hpp +++ b/inc/mkn/kul/os/win/threads.os.hpp @@ -77,22 +77,21 @@ class Thread : public threading::AThread { void act() { try { func(); - } catch (const std::exception &e) { + } catch (const std::exception& e) { ep = std::current_exception(); } f = 1; } public: - Thread(const std::function &func) : func(func) {} + Thread(const std::function& func) : func(func) {} template - Thread(const T &t) : func(std::bind((void (T::*)()) & T::operator(), t)) {} + Thread(const T& t) : func(std::bind((void(T::*)()) & T::operator(), t)) {} template - Thread(const std::reference_wrapper &r) - : func(std::bind((void (T::*)()) & T::operator(), r)) {} + Thread(const std::reference_wrapper& r) : func(std::bind((void(T::*)()) & T::operator(), r)) {} template - Thread(const std::reference_wrapper &r) - : func(std::bind((void (T::*)() const) & T::operator(), r)) {} + Thread(const std::reference_wrapper& r) + : func(std::bind((void(T::*)() const) & T::operator(), r)) {} virtual ~Thread() {} void join() { if (!s) run(); @@ -115,7 +114,7 @@ class Thread : public threading::AThread { namespace threading { inline DWORD WINAPI threadFunction(LPVOID th) { - reinterpret_cast(th)->act(); + reinterpret_cast(th)->act(); return 0; } } // namespace threading diff --git a/inc/mkn/kul/proc.hpp b/inc/mkn/kul/proc.hpp index e9a0d3d..526ddca 100644 --- a/inc/mkn/kul/proc.hpp +++ b/inc/mkn/kul/proc.hpp @@ -45,7 +45,7 @@ namespace kul { namespace this_proc { int32_t id(); -void kill(const uint32_t &e); +void kill(const uint32_t& e); uint64_t virtualMemory(); uint64_t physicalMemory(); @@ -57,7 +57,8 @@ namespace proc { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class ExitException : public mkn::kul::proc::Exception { @@ -65,36 +66,35 @@ class ExitException : public mkn::kul::proc::Exception { const short ec; public: - ExitException(char const *f, uint16_t const &l, const short _ec, std::string const &s) + ExitException(char const* f, uint16_t const& l, const short _ec, std::string const& s) : Exception(f, l, s), ec(_ec) {} - const short &code() const { return ec; } + const short& code() const { return ec; } }; class Call { private: std::string cwd; const std::string d; - std::string const &s; + std::string const& s; mkn::kul::hash::map::S2S oldEvs; void setCWD() { if (d.size()) { cwd = mkn::kul::env::CWD(); - if ( !mkn::kul::env::CWD(d)) KEXCEPTION("FAILED TO SET DIRECTORY: " + d); + if (!mkn::kul::env::CWD(d)) KEXCEPTION("FAILED TO SET DIRECTORY: " + d); } } public: ~Call() { if (d.size()) mkn::kul::env::CWD(cwd); - for (auto const &oldEv : oldEvs) - mkn::kul::env::SET(oldEv.first.c_str(), oldEv.second.c_str()); + for (auto const& oldEv : oldEvs) mkn::kul::env::SET(oldEv.first.c_str(), oldEv.second.c_str()); } - Call(std::string const &_s, std::string const &_d = "") : d(_d), s(_s) { setCWD(); } - Call(std::string const &_s, const mkn::kul::hash::map::S2S &evs, std::string const &_d = "") + Call(std::string const& _s, std::string const& _d = "") : d(_d), s(_s) { setCWD(); } + Call(std::string const& _s, const mkn::kul::hash::map::S2S& evs, std::string const& _d = "") : d(_d), s(_s) { setCWD(); - for (const auto &ev : evs) { - std::string const &v = mkn::kul::env::GET(ev.first.c_str()); + for (const auto& ev : evs) { + std::string const& v = mkn::kul::env::GET(ev.first.c_str()); if (v.size()) oldEvs.insert(ev.first, v); mkn::kul::env::SET(ev.first.c_str(), ev.second.c_str()); } @@ -109,19 +109,19 @@ class AProcess { const bool wfe = 1; int32_t pec = -1, pi = 0; const std::string d; - std::function e, o; + std::function e, o; std::vector argv; mkn::kul::hash::map::S2S evs; - friend std::ostream &operator<<(std::ostream &, const AProcess &); + friend std::ostream& operator<<(std::ostream&, const AProcess&); protected: - AProcess(std::string const &cmd, const bool &_wfe) : wfe(_wfe) { argv.push_back(cmd); } - AProcess(std::string const &cmd, std::string const &_d, const bool &_wfe) : wfe(_wfe), d(_d) { + AProcess(std::string const& cmd, const bool& _wfe) : wfe(_wfe) { argv.push_back(cmd); } + AProcess(std::string const& cmd, std::string const& _d, const bool& _wfe) : wfe(_wfe), d(_d) { argv.push_back(cmd); } virtual ~AProcess() {} - std::string const &directory() const { return d; } + std::string const& directory() const { return d; } void setFinished() { f = 1; } virtual bool kill(int16_t k = 6) = 0; virtual void preStart() {} @@ -129,70 +129,70 @@ class AProcess { virtual void tearDown() {} virtual void run() KTHROW(mkn::kul::Exception) = 0; bool waitForExit() const { return wfe; } - void pid(const int32_t &_pi) { this->pi = _pi; } + void pid(const int32_t& _pi) { this->pi = _pi; } - const std::vector &args() const { return argv; }; - const mkn::kul::hash::map::S2S &vars() const { return evs; } - virtual void out(std::string const &_s) { + const std::vector& args() const { return argv; }; + const mkn::kul::hash::map::S2S& vars() const { return evs; } + virtual void out(std::string const& _s) { if (this->o) this->o(_s); else printf("%s", _s.c_str()); } - virtual void err(std::string const &_s) { + virtual void err(std::string const& _s) { if (this->e) this->e(_s); else fprintf(stderr, "%s", _s.c_str()); } - void error(const int16_t &line, std::string const &_s) KTHROW(mkn::kul::Exception) { + void error(const int16_t& line, std::string const& _s) KTHROW(mkn::kul::Exception) { tearDown(); throw Exception("mkn/kul/proc.hpp", line, _s); } - void exitCode(const int32_t &_e) { pec = _e; } + void exitCode(const int32_t& _e) { pec = _e; } public: template - AProcess &arg(const T &a) { + AProcess& arg(const T& a) { std::stringstream ss; ss << a; if (ss.str().size()) argv.push_back(ss.str()); return *this; } - AProcess &arg(std::string const &a) { + AProcess& arg(std::string const& a) { argv.push_back(a); return *this; } - AProcess &args(std::string const &a) { + AProcess& args(std::string const& a) { if (a.size()) - for (const auto &c : mkn::kul::cli::asArgs(a)) argv.push_back(c); + for (const auto& c : mkn::kul::cli::asArgs(a)) argv.push_back(c); return *this; } - AProcess &var(std::string const &n, std::string const &v) { + AProcess& var(std::string const& n, std::string const& v) { evs[n] = v; return *this; } - AProcess &set(std::vector const &in) { - for (auto const &ev : in) evs[ev.name()] = ev.toString(); + AProcess& set(std::vector const& in) { + for (auto const& ev : in) evs[ev.name()] = ev.toString(); return *this; } virtual inline void start() KTHROW(mkn::kul::Exception); virtual inline std::string toString() const; - const int32_t &pid() const { return pi; } + const int32_t& pid() const { return pi; } bool started() const { return pi > 0; } bool finished() const { return f; } - const int32_t &exitCode() { return pec; } - AProcess &operator<<(std::string const &_s) { + const int32_t& exitCode() { return pec; } + AProcess& operator<<(std::string const& _s) { arg(_s); return *this; } - void setOut(std::function _o) { this->o = _o; } - void setErr(std::function _e) { this->e = _e; } + void setOut(std::function _o) { this->o = _o; } + void setErr(std::function _e) { this->e = _e; } }; -inline std::ostream &operator<<(std::ostream &s, const AProcess &p) { return s << p.toString(); } +inline std::ostream& operator<<(std::ostream& s, const AProcess& p) { return s << p.toString(); } class ProcessCapture { private: @@ -200,16 +200,16 @@ class ProcessCapture { protected: ProcessCapture() {} - ProcessCapture(const ProcessCapture &pc) : so(pc.so.str()), se(pc.se.str()) {} - virtual void out(std::string const &s) { so << s; } - virtual void err(std::string const &s) { se << s; } + ProcessCapture(const ProcessCapture& pc) : so(pc.so.str()), se(pc.se.str()) {} + virtual void out(std::string const& s) { so << s; } + virtual void err(std::string const& s) { se << s; } public: - ProcessCapture(AProcess &p) { setProcess(p); } + ProcessCapture(AProcess& p) { setProcess(p); } virtual ~ProcessCapture() {} const std::string outs() const { return so.str(); } const std::string errs() const { return se.str(); } - void setProcess(AProcess &p) { + void setProcess(AProcess& p) { p.setOut(std::bind(&ProcessCapture::out, std::ref(*this), std::placeholders::_1)); p.setErr(std::bind(&ProcessCapture::err, std::ref(*this), std::placeholders::_1)); } diff --git a/inc/mkn/kul/scm.hpp b/inc/mkn/kul/scm.hpp index d6e9103..d491355 100644 --- a/inc/mkn/kul/scm.hpp +++ b/inc/mkn/kul/scm.hpp @@ -42,12 +42,13 @@ namespace scm { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; class NotFoundException : public mkn::kul::Exception { public: - NotFoundException(char const *f, uint16_t const &l, std::string const &s) + NotFoundException(char const* f, uint16_t const& l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; } // namespace scm @@ -59,27 +60,27 @@ class SCM { public: virtual ~SCM() {} std::string type() { return typeid(*this).name(); } - virtual std::string co(std::string const &d, std::string const &r, std::string const &v) const + virtual std::string co(std::string const& d, std::string const& r, std::string const& v) const KTHROW(Exception) = 0; - virtual void up(std::string const &d, std::string const &r, std::string const &v) const + virtual void up(std::string const& d, std::string const& r, std::string const& v) const KTHROW(Exception) = 0; - virtual std::string origin(std::string const &d) const = 0; - virtual std::string localVersion(std::string const &d, std::string const &b) const = 0; - virtual std::string remoteVersion(std::string const &url, std::string const &branch) const + virtual std::string origin(std::string const& d) const = 0; + virtual std::string localVersion(std::string const& d, std::string const& b) const = 0; + virtual std::string remoteVersion(std::string const& url, std::string const& branch) const KTHROW(Exception) = 0; - virtual bool hasChanges(std::string const &d) const = 0; - virtual void status(std::string const &d, bool full = 1) const = 0; - virtual void diff(std::string const &d) const = 0; + virtual bool hasChanges(std::string const& d) const = 0; + virtual void status(std::string const& d, bool full = 1) const = 0; + virtual void diff(std::string const& d) const = 0; - virtual std::string defaultRemoteBranch(std::string const &repo) const = 0; + virtual std::string defaultRemoteBranch(std::string const& repo) const = 0; }; // review https://gist.github.com/aleksey-bykov/1273f4982c317c92d532 namespace scm { class Git : public SCM { public: - std::string defaultRemoteBranch(std::string const &repo) const override { + std::string defaultRemoteBranch(std::string const& repo) const override { mkn::kul::Process p("git"); mkn::kul::ProcessCapture pc(p); p << "ls-remote" @@ -96,7 +97,7 @@ class Git : public SCM { // e.g. git ls-remote --symref git@github.com:user/repo HEAD }; - std::string branch(mkn::kul::Dir const &dr) const { + std::string branch(mkn::kul::Dir const& dr) const { mkn::kul::os::PushDir pushd(dr); mkn::kul::Process p("git"); mkn::kul::ProcessCapture pc(p); @@ -110,9 +111,9 @@ class Git : public SCM { return mkn::kul::String::LINES(pc.outs())[0]; } - std::string branch(std::string const &d) const { return branch(mkn::kul::Dir(d)); } + std::string branch(std::string const& d) const { return branch(mkn::kul::Dir(d)); } - std::string co(std::string const &d, std::string const &r, std::string const &v) const + std::string co(std::string const& d, std::string const& r, std::string const& v) const KTHROW(Exception) override { Dir dr(d, true); mkn::kul::Process p("git"); @@ -126,7 +127,7 @@ class Git : public SCM { } return p.toString(); } - void up(std::string const &d, std::string const &r, std::string const &v) const + void up(std::string const& d, std::string const& r, std::string const& v) const KTHROW(Exception) override { if (!Dir(d).is()) co(d, r, v); @@ -142,7 +143,7 @@ class Git : public SCM { } } } - std::string origin(std::string const &d) const override { + std::string origin(std::string const& d) const override { mkn::kul::Process p("git", d); mkn::kul::ProcessCapture pc(p); try { @@ -155,12 +156,12 @@ class Git : public SCM { KEXCEPT(Exception, "SCM ERROR: Directory may not be git repository : " + d); std::vector lines; mkn::kul::String::LINES(pc.outs(), lines); - for (auto &line : lines) mkn::kul::String::REPLACE_ALL(line, "\t", " "); - for (auto &line : lines) mkn::kul::String::REPLACE_ALL(line, " ", " "); + for (auto& line : lines) mkn::kul::String::REPLACE_ALL(line, "\t", " "); + for (auto& line : lines) mkn::kul::String::REPLACE_ALL(line, " ", " "); if (lines.size()) return mkn::kul::String::SPLIT(lines[0], ' ')[1]; KEXCEPT(Exception, "SCM ERROR - Check remote dependency location / version"); } - std::string localVersion(std::string const &d, std::string const &b) const override { + std::string localVersion(std::string const& d, std::string const& b) const override { mkn::kul::Process p("git", d); mkn::kul::ProcessCapture pc(p); try { @@ -174,7 +175,7 @@ class Git : public SCM { return mkn::kul::String::LINES(pc.outs())[0]; } - std::string remoteVersion(std::string const &url, std::string const &b) const + std::string remoteVersion(std::string const& url, std::string const& b) const KTHROW(Exception) override { mkn::kul::Process p("git"); mkn::kul::ProcessCapture pc(p); @@ -191,7 +192,7 @@ class Git : public SCM { return s.substr(0, s.find('\t')); } - bool hasChanges(std::string const &d) const override { + bool hasChanges(std::string const& d) const override { mkn::kul::Process p("git", d); mkn::kul::ProcessCapture pc(p); try { @@ -202,7 +203,7 @@ class Git : public SCM { } return mkn::kul::String::LINES(pc.outs()).size() > 1; } - void status(std::string const &d, bool full = 1) const override { + void status(std::string const& d, bool full = 1) const override { mkn::kul::Process p("git", d); try { p << "status"; @@ -212,7 +213,7 @@ class Git : public SCM { KEXCEPT(Exception, "SCM ERROR " + std::string(e.what())); } } - void diff(std::string const &d) const override { + void diff(std::string const& d) const override { mkn::kul::Process p("git", d); try { p.arg("diff").start(); diff --git a/inc/mkn/kul/scm/man.hpp b/inc/mkn/kul/scm/man.hpp index 93905eb..87b2a95 100644 --- a/inc/mkn/kul/scm/man.hpp +++ b/inc/mkn/kul/scm/man.hpp @@ -41,11 +41,11 @@ namespace scm { class Manager { public: - static Manager &INSTANCE() { + static Manager& INSTANCE() { static Manager instance; return instance; } - const SCM &get(std::string const &s) KTHROW(NotFoundException) { + const SCM& get(std::string const& s) KTHROW(NotFoundException) { if (SCMs.count(s) > 0) return *(*SCMs.find(s)).second; KEXCEPT(NotFoundException, "Source Control Management for " + s + " is not implemented"); } @@ -54,10 +54,10 @@ class Manager { Manager() { git.reset(new Git()); // svn.reset(new Svn()); - SCMs.insert(std::pair("git", git.get())); + SCMs.insert(std::pair("git", git.get())); // SCMs.insert(std::pair("svn", svn.get())); } - hash::map::S2T SCMs; + hash::map::S2T SCMs; std::unique_ptr git; // std::unique_ptr svn; }; diff --git a/inc/mkn/kul/serial/cli.arg.end.hpp b/inc/mkn/kul/serial/cli.arg.end.hpp index 434df07..f61ee7f 100644 --- a/inc/mkn/kul/serial/cli.arg.end.hpp +++ b/inc/mkn/kul/serial/cli.arg.end.hpp @@ -34,7 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #if defined(_MKN_WITH_IO_CEREAL_) template -void save(Archive &ar) const { +void save(Archive& ar) const { auto convert_to_std_map = [](mkn::kul::hash::map::S2S const& s) -> std::unordered_map { std::unordered_map ret; @@ -44,7 +44,7 @@ void save(Archive &ar) const { ar(convert_to_std_map(vals)); } template -void load(Archive &ar) { +void load(Archive& ar) { auto convert_to_kul_map = [](std::unordered_map const& s) -> mkn::kul::hash::map::S2S { mkn::kul::hash::map::S2S ret; diff --git a/inc/mkn/kul/sort.hpp b/inc/mkn/kul/sort.hpp new file mode 100644 index 0000000..731702f --- /dev/null +++ b/inc/mkn/kul/sort.hpp @@ -0,0 +1,178 @@ +/** +Copyright (c) 2020, Philip Deegan. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Philip Deegan nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef _MKN_KUL_SORT_HPP_ +#define _MKN_KUL_SORT_HPP_ + +#include +#include +#include + +#include "mkn/kul/span.hpp" + +namespace mkn { +namespace kul { + +namespace detail { + +auto constexpr LT_fn = [](auto const& x, auto const& y) constexpr { return x < y; }; +using LT = decltype(LT_fn); + +auto constexpr GT_fn = [](auto const& x, auto const& y) constexpr { return x > y; }; +using GT = decltype(GT_fn); + +auto constexpr accessor_fn = [](auto& c) constexpr -> auto& { return c; }; +using Accessor = decltype(accessor_fn); + +auto constexpr swaper_fn = [](auto& x, auto& y) constexpr { std::swap(x, y); }; +using Swaper = decltype(swaper_fn); + +} // namespace detail + +template +class QuickSorter { + public: + QuickSorter(C& c_, LT const& lt_, GT const& gt_, + Accessor accessor_ = detail::accessor_fn, // + Swaper swaper_ = detail::swaper_fn) + : c{c_}, lt{lt_}, gt{gt_}, accessor{accessor_}, swaper{swaper_} {} + + void sort(S l, S r) { + auto i = l, j = r; + auto const half = v[(l + r) / 2]; + do { + while (fn0(v[i], half)) i++; + while (fn1(v[j], half)) j--; + if (i <= j) { + swaper(v_[i], v_[j]); + i++, j--; + } + } while (i <= j); + if (l < j) sort(l, j); + if (i < r) sort(i, r); + } + + void sort() { sort(0, c.size() - 1); } + + private: + C& c; + + LT const& lt; + GT const& gt; + + Accessor const& accessor; + Swaper const& swaper; + + decltype(accessor(c))& v_ = accessor(c); + decltype(accessor(c)) const& v = accessor(c); + + auto static constexpr _fn0(LT const& lt_, GT const& gt_) -> auto const& { + if constexpr (ascending) + return lt_; + else + return gt_; + } + auto static constexpr _fn1(LT const& lt_, GT const& gt_) -> auto const& { + if constexpr (ascending) + return gt_; + else + return lt_; + } + + using Fn0 = std::conditional_t; + using Fn1 = std::conditional_t; + Fn0 const& fn0 = _fn0(lt, gt); + Fn1 const& fn1 = _fn1(lt, gt); +}; + +template +void sort(Container& c, S start, S end) { + using QS = QuickSorter; + QS{c, detail::LT_fn, detail::GT_fn}.sort(start, end); +} + +template +void sort(Container& c, LT const& lt, GT const& gt) { + using QS = QuickSorter; + QS{c, lt, gt}.sort(); +} + +template +void sort(Container& c) { + // using QS = QuickSorter; + sort(c, detail::LT_fn, detail::GT_fn); +} + +template +void para_sort(Container& c, LT const& lt, GT const& gt, std::uint8_t n_threads = 2) { + using V = typename Container::value_type; + auto constexpr PT = CL / sizeof(V); + if (PT < 1) n_threads = 1; // no point + + auto bytes = c.size() * sizeof(V); + // auto each = c.size() / n_threads; + auto even = bytes % CL == 0; + auto cls = bytes / CL + (even ? 0 : 1); + auto off = (cls / n_threads) * PT; + + std::vector threads; + threads.reserve(n_threads - 1); + std::vector starts(n_threads, 0); + std::vector ends(n_threads, c.size() - 1); + + for (std::size_t i = 1; i < n_threads; ++i) { + starts[i] = off * i; + ends[i - 1] = starts[i] - 1; + } + + using QS = QuickSorter; + for (std::size_t i = 1; i < n_threads; ++i) + threads.emplace_back([&, i]() { QS{c, lt, gt}.sort(starts[i], ends[i]); }); + + QS{c, lt, gt}.sort(starts[0], ends[0]); + for (auto& thread : threads) + if (thread.joinable()) thread.join(); + + if (n_threads > 1) QS{c, lt, gt}.sort(); +} + +template +void para_sort(Container& c, std::uint8_t n_threads = 2) { + para_sort(c, detail::LT_fn, detail::GT_fn, n_threads); +} + +} // namespace kul +} // namespace mkn + +#endif /* _MKN_KUL_SORT_HPP_ */ diff --git a/inc/mkn/kul/span.hpp b/inc/mkn/kul/span.hpp index 8857ed3..a703301 100644 --- a/inc/mkn/kul/span.hpp +++ b/inc/mkn/kul/span.hpp @@ -33,12 +33,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include + #include "mkn/kul/decltype.hpp" namespace mkn { namespace kul { - template struct is_span_like : std::false_type {}; @@ -154,7 +154,6 @@ struct SpanSet { std::vector m_vec; }; - } // namespace kul } // namespace mkn diff --git a/inc/mkn/kul/string.hpp b/inc/mkn/kul/string.hpp index b85b7ef..96dc0b9 100644 --- a/inc/mkn/kul/string.hpp +++ b/inc/mkn/kul/string.hpp @@ -48,7 +48,7 @@ enum STR_INT_RET { IS_SUCCESS = 0, IS_OVERFLOW, IS_UNDERFLOW, IS_INCONVERTIBLE } class StringException : public mkn::kul::Exception { public: - StringException(char const *f, uint16_t const &l, std::string const &s) + StringException(char const* f, uint16_t const& l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; @@ -58,7 +58,7 @@ class StringOpHelper { private: std::unordered_map STR_INT_STR; - std::string getStrForRet(const STR_INT_RET &ret) { return (*STR_INT_STR.find(ret)).second; } + std::string getStrForRet(const STR_INT_RET& ret) { return (*STR_INT_STR.find(ret)).second; } StringOpHelper() { STR_INT_STR.insert(std::make_pair(0, "SUCCESS")); STR_INT_STR.insert(std::make_pair(1, "OVERFLOW")); @@ -67,7 +67,7 @@ class StringOpHelper { } public: - static StringOpHelper &INSTANCE() { + static StringOpHelper& INSTANCE() { static StringOpHelper i; return i; } @@ -75,43 +75,43 @@ class StringOpHelper { class String { public: - static void REPLACE(std::string &s, std::string const &f, std::string const &r) { + static void REPLACE(std::string& s, std::string const& f, std::string const& r) { size_t p = 0; if ((p = s.find(f)) != std::string::npos) s.replace(p, f.size(), r); } - static void REPLACE_ALL(std::string &s, std::string const &f, std::string const &r) { + static void REPLACE_ALL(std::string& s, std::string const& f, std::string const& r) { size_t p = s.find(f); while (p != std::string::npos) { s.replace(s.find(f, p), f.size(), r); p = s.find(f, p + r.size()); } } - static void TRIM_LEFT(std::string &s, char const &delim = ' ') { + static void TRIM_LEFT(std::string& s, char const& delim = ' ') { while (s.find(delim) == 0) s.erase(0, 1); } - static void TRIM_RIGHT(std::string &s, char const &delim = ' ') { + static void TRIM_RIGHT(std::string& s, char const& delim = ' ') { while (s.rfind(delim) == s.size() - 1) s.pop_back(); } - static void TRIM(std::string &s) { + static void TRIM(std::string& s) { while (s.find(' ') == 0 || s.find('\t') == 0) s.erase(0, 1); if (s.size() == 0) return; while (s.rfind(' ') == s.size() - 1 || s.rfind('\t') == s.size() - 1) s.pop_back(); } template - static void TRIM(V &strs) { - for (auto &v : strs) TRIM(v); + static void TRIM(V& strs) { + for (auto& v : strs) TRIM(v); } - static void PAD(std::string &s, uint16_t const &p) { + static void PAD(std::string& s, uint16_t const& p) { while (s.size() < p) s += " "; } - static std::vector SPLIT(std::string const &s, char const &d) { + static std::vector SPLIT(std::string const& s, char const& d) { std::vector v; SPLIT(s, d, v); return v; } - static void SPLIT(std::string const &s, char const &d, std::vector &v) { + static void SPLIT(std::string const& s, char const& d, std::vector& v) { if (s.find(d) != std::string::npos) { std::string l; std::stringstream stream(s); @@ -120,12 +120,12 @@ class String { } else v.push_back(s); } - static std::vector SPLIT(std::string const &s, std::string const &d) { + static std::vector SPLIT(std::string const& s, std::string const& d) { std::vector v; SPLIT(s, d, v); return v; } - static void SPLIT(std::string const &s, std::string const &d, std::vector &v) { + static void SPLIT(std::string const& s, std::string const& d, std::vector& v) { std::string l = s; size_t pos = 0; while ((pos = l.find(d)) != std::string::npos) { @@ -134,14 +134,14 @@ class String { } v.push_back(l); } - static std::vector ESC_SPLIT(std::string const &s, char const &d, - char const &e = '\\') { + static std::vector ESC_SPLIT(std::string const& s, char const& d, + char const& e = '\\') { std::vector v; ESC_SPLIT(s, d, v, e); return v; } - static void ESC_SPLIT(std::string const &s, char const &d, std::vector &v, - char const &e = '\\') { + static void ESC_SPLIT(std::string const& s, char const& d, std::vector& v, + char const& e = '\\') { std::string l = s, ds = std::string(1, d), es = std::string(1, e); std::string tmp = l; size_t pos = 0, esc = 0; @@ -167,12 +167,12 @@ class String { std::transform(b.begin(), b.end(), b.begin(), ::tolower); return (a == b); } - static std::vector LINES(std::string const &s) { + static std::vector LINES(std::string const& s) { std::vector v; LINES(s, v); return v; } - static void LINES(std::string const &s, std::vector &v) { + static void LINES(std::string const& s, std::vector& v) { if (s.find("\n") != std::string::npos) { std::string l; std::stringstream ss(s); @@ -184,17 +184,17 @@ class String { static bool BOOL(std::string s) { TRIM(s); - const std::vector &pos{"yes", "y", "true", "1"}; - const std::vector &neg{"no", "n", "false", "0"}; + const std::vector& pos{"yes", "y", "true", "1"}; + const std::vector& neg{"no", "n", "false", "0"}; std::transform(s.begin(), s.end(), s.begin(), ::tolower); if (std::find(pos.begin(), pos.end(), s) != pos.end()) return true; if (std::find(neg.begin(), neg.end(), s) != neg.end()) return false; KEXCEPT(StringException, "input not bool-able, " + s); } - static uint16_t UINT16(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, uint32_t &lresult) { - char *end; + static uint16_t UINT16(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, uint32_t& lresult) { + char* end; errno = 0; lresult = strtoul(s, &end, 10); if ((errno == ERANGE) || lresult > UINT16_MAX) return IS_OVERFLOW; @@ -210,9 +210,9 @@ class String { if (result != lresult) KEXCEPT(StringException, "UINT16 conversion failed"); return result; } - static int16_t INT16(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, int32_t &lresult) { - char *end; + static int16_t INT16(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, int32_t& lresult) { + char* end; errno = 0; lresult = strtol(s, &end, 10); if ((errno == ERANGE) || lresult > INT16_MAX) return IS_OVERFLOW; @@ -230,9 +230,9 @@ class String { return result; } - static uint32_t UINT32(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, uint64_t &lresult) { - char *end; + static uint32_t UINT32(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, uint64_t& lresult) { + char* end; errno = 0; lresult = strtoull(s, &end, 10); if ((errno == ERANGE) || lresult > UINT32_MAX) return IS_OVERFLOW; @@ -248,9 +248,9 @@ class String { if (result != lresult) KEXCEPT(StringException, "UINT32 conversion failed"); return result; } - static int32_t INT32(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, int64_t &lresult) { - char *end; + static int32_t INT32(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, int64_t& lresult) { + char* end; errno = 0; lresult = strtoll(s, &end, 10); if ((errno == ERANGE) || lresult > INT32_MAX) return IS_OVERFLOW; @@ -268,9 +268,9 @@ class String { return result; } - static uint64_t UINT64(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, uint64_t &lresult) { - char *end; + static uint64_t UINT64(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, uint64_t& lresult) { + char* end; errno = 0; lresult = strtoull(s, &end, 10); if ((errno == ERANGE) || lresult > UINT64_MAX) return IS_OVERFLOW; @@ -287,9 +287,9 @@ class String { return result; } - static int64_t INT64(std::string const &str) KTHROW(StringException) { - auto lambda = [](char const *s, int64_t &lresult) { - char *end; + static int64_t INT64(std::string const& str) KTHROW(StringException) { + auto lambda = [](char const* s, int64_t& lresult) { + char* end; errno = 0; lresult = strtoll(s, &end, 10); if ((errno == ERANGE) || lresult > INT64_MAX) return IS_OVERFLOW; diff --git a/inc/mkn/kul/sys.hpp b/inc/mkn/kul/sys.hpp index 5b32e09..988a3a1 100644 --- a/inc/mkn/kul/sys.hpp +++ b/inc/mkn/kul/sys.hpp @@ -39,7 +39,8 @@ namespace sys { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; } // namespace sys diff --git a/inc/mkn/kul/threads.hpp b/inc/mkn/kul/threads.hpp index 6c78ad5..6246fb9 100644 --- a/inc/mkn/kul/threads.hpp +++ b/inc/mkn/kul/threads.hpp @@ -39,10 +39,10 @@ namespace kul { class ScopeLock { private: - Mutex &m; + Mutex& m; public: - ScopeLock(Mutex &_m) : m(_m) { this->m.lock(); } + ScopeLock(Mutex& _m) : m(_m) { this->m.lock(); } ~ScopeLock() { this->m.unlock(); } }; @@ -67,20 +67,20 @@ class ThreadQueue { } public: - ThreadQueue(const std::function &_func) : th(std::ref(*this)), func(_func) {} + ThreadQueue(const std::function& _func) : th(std::ref(*this)), func(_func) {} template - ThreadQueue(const T &t) : th(std::ref(*this)), func(std::bind(&T::operator(), t)) {} + ThreadQueue(const T& t) : th(std::ref(*this)), func(std::bind(&T::operator(), t)) {} template - ThreadQueue(const std::reference_wrapper &ref) + ThreadQueue(const std::reference_wrapper& ref) : th(std::ref(*this)), func(std::bind(&T::operator(), ref)) {} - void setMax(const int16_t &max) { m = max; } + void setMax(const int16_t& max) { m = max; } void run() { th.run(); } void operator()() { start(); } virtual void join() KTHROW(std::exception) { th.join(); while (ts.size()) { - const std::shared_ptr *del = 0; - for (const auto &t : ts) { + const std::shared_ptr* del = 0; + for (const auto& t : ts) { if (t->finished()) { t->join(); if (t->exception() != std::exception_ptr()) { @@ -108,7 +108,7 @@ class ThreadQueue { template class PredicatedThreadQueue : public ThreadQueue { private: - P &p; + P& p; size_t ps; protected: @@ -124,8 +124,8 @@ class PredicatedThreadQueue : public ThreadQueue { ts.push_back(at); this_thread::nSleep(__MKN_KUL_THREAD_SPAWN_WAIT__); } - const std::shared_ptr *del = 0; - for (const auto &t : ts) { + const std::shared_ptr* del = 0; + for (const auto& t : ts) { if (t->finished()) { t->join(); if (t->exception() != std::exception_ptr()) { @@ -142,12 +142,12 @@ class PredicatedThreadQueue : public ThreadQueue { } public: - PredicatedThreadQueue(const std::function &_func, P &pr) + PredicatedThreadQueue(const std::function& _func, P& pr) : ThreadQueue(_func), p(pr), ps(p.size()) {} template - PredicatedThreadQueue(const T &t, P &pr) : ThreadQueue(t), p(pr), ps(p.size()) {} + PredicatedThreadQueue(const T& t, P& pr) : ThreadQueue(t), p(pr), ps(p.size()) {} template - PredicatedThreadQueue(const std::reference_wrapper &ref, P &pr) + PredicatedThreadQueue(const std::reference_wrapper& ref, P& pr) : ThreadQueue(ref), p(pr), ps(p.size()) {} }; @@ -159,17 +159,17 @@ class ConcurrentThreadQueue { size_t _cur = 0, _max = 1; const uint64_t m_nWait; std::atomic _block, _detatched, _up; - std::queue, std::function>>> _q; + std::queue, std::function>>> _q; mkn::kul::hash::map::S2T> _k; - mkn::kul::hash::map::S2T> _e; + mkn::kul::hash::map::S2T> _e; mkn::kul::Thread _thread; mkn::kul::Mutex _mmutex, _qmutex; - void _KTHROW(const std::exception_ptr &ep, const std::function &func) { + void _KTHROW(const std::exception_ptr& ep, const std::function& func) { try { std::rethrow_exception(ep); - } catch (const E &e) { + } catch (const E& e) { func(e); } } @@ -183,8 +183,8 @@ class ConcurrentThreadQueue { for (; _cur < _max; _cur++) { mkn::kul::ScopeLock l(_qmutex); - auto &f_ptr(_q.front()); - auto &f(*f_ptr.get()); + auto& f_ptr(_q.front()); + auto& f(*f_ptr.get()); std::stringstream ss; ss << &f; auto k(ss.str()); @@ -195,7 +195,7 @@ class ConcurrentThreadQueue { } mkn::kul::hash::set::String del; - for (const auto &t : _k) { + for (const auto& t : _k) { if (t.second->finished()) { t.second->join(); if (t.second->exception() != std::exception_ptr()) { @@ -208,17 +208,17 @@ class ConcurrentThreadQueue { _cur--; } } - for (const auto &s : del) _k.erase(s) && _e.erase(s); + for (const auto& s : del) _k.erase(s) && _e.erase(s); } } - ConcurrentThreadQueue(const ConcurrentThreadQueue &) = delete; - ConcurrentThreadQueue(const ConcurrentThreadQueue &&) = delete; - ConcurrentThreadQueue &operator=(const ConcurrentThreadQueue &) = delete; - ConcurrentThreadQueue &operator=(const ConcurrentThreadQueue &&) = delete; + ConcurrentThreadQueue(const ConcurrentThreadQueue&) = delete; + ConcurrentThreadQueue(const ConcurrentThreadQueue&&) = delete; + ConcurrentThreadQueue& operator=(const ConcurrentThreadQueue&) = delete; + ConcurrentThreadQueue& operator=(const ConcurrentThreadQueue&&) = delete; public: - ConcurrentThreadQueue(const size_t &max = 1, bool strt = 0, const uint64_t &nWait = 1000000) + ConcurrentThreadQueue(const size_t& max = 1, bool strt = 0, const uint64_t& nWait = 1000000) : _max(max), m_nWait(nWait), _block(0), _detatched(0), _up(0), _thread(std::ref(*this)) { _k.setDeletedKey(""); _e.setDeletedKey(""); @@ -226,24 +226,25 @@ class ConcurrentThreadQueue { } virtual ~ConcurrentThreadQueue() {} - virtual ConcurrentThreadQueue &detach() { + virtual ConcurrentThreadQueue& detach() { _detatched = 1; return *this; } - virtual ConcurrentThreadQueue &join() { + virtual ConcurrentThreadQueue& join() { _thread.join(); return *this; } - virtual ConcurrentThreadQueue &stop() { + virtual ConcurrentThreadQueue& stop() { _up = 0; return *this; } - virtual ConcurrentThreadQueue &interrupt() { + virtual ConcurrentThreadQueue& interrupt() { mkn::kul::ScopeLock l(_mmutex); _thread.interrupt(); return *this; } - virtual ConcurrentThreadQueue &finish(const uint64_t &nWait = 1000000) KTHROW(mkn::kul::Exception) { + virtual ConcurrentThreadQueue& finish(const uint64_t& nWait = 1000000) + KTHROW(mkn::kul::Exception) { while (_up) { this_thread::nSleep(nWait); { @@ -253,15 +254,15 @@ class ConcurrentThreadQueue { } return *this; } - virtual ConcurrentThreadQueue &block() { + virtual ConcurrentThreadQueue& block() { _block = 1; return *this; } - virtual ConcurrentThreadQueue &unblock() { + virtual ConcurrentThreadQueue& unblock() { _block = 0; return *this; } - virtual ConcurrentThreadQueue &start() { + virtual ConcurrentThreadQueue& start() { if (!_up) { _up = 1; _thread.run(); @@ -269,19 +270,19 @@ class ConcurrentThreadQueue { return *this; } - bool async(std::function &&function, - std::function &&exception = std::function()) { + bool async(std::function&& function, + std::function&& exception = std::function()) { if (_block) return false; { - auto pair = std::unique_ptr, std::function>>( - new std::pair, std::function>(function, exception)); + auto pair = std::unique_ptr, std::function>>( + new std::pair, std::function>(function, exception)); mkn::kul::ScopeLock l(_qmutex); _q.push(std::move(pair)); } return true; } - std::exception_ptr const& exception() const { return _thread.exception(); } + std::exception_ptr const& exception() const { return _thread.exception(); } void rethrow() { if (_thread.exception()) std::rethrow_exception(_thread.exception()); @@ -301,7 +302,7 @@ class PoolThread { std::function m_function; mkn::kul::Mutex _mutex; - bool if_ready_set(const std::function &f) { + bool if_ready_set(const std::function& f) { if (!m_ready) return false; m_function = f; m_ready = 0; @@ -323,7 +324,7 @@ class PoolThread { } public: - PoolThread(const uint64_t &nWait = 1000000) : m_nWait(nWait), m_ready(1), m_run(1) {} + PoolThread(const uint64_t& nWait = 1000000) : m_nWait(nWait), m_ready(1), m_run(1) {} virtual ~PoolThread() {} virtual void operator()() { while (m_run) { @@ -348,8 +349,8 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { mkn::kul::ScopeLock l(_qmutex); for (size_t i = 0; i < _max; i++) { const auto n = std::to_string(i); - auto &f_ptr(_q.front()); - auto &f(*f_ptr.get()); + auto& f_ptr(_q.front()); + auto& f(*f_ptr.get()); if (!_p[n]->if_ready_set(f.first)) continue; _e[n] = f.second; _q.pop(); @@ -361,7 +362,7 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { { if (!_up) return false; mkn::kul::ScopeLock l(_mmutex); - for (const auto &t : _k) { + for (const auto& t : _k) { if (t.second->started() && t.second->finished()) { if (t.second->exception() != std::exception_ptr()) { if (_e.count(t.first)) @@ -377,7 +378,7 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { { mkn::kul::ScopeLock l(_mmutex); if (!_up) return false; - for (const auto &n : del) { + for (const auto& n : del) { _k[n]->join(); _e.erase(n); _k.erase(n); @@ -396,13 +397,13 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { } } - ConcurrentThreadPool(const ConcurrentThreadPool &) = delete; - ConcurrentThreadPool(const ConcurrentThreadPool &&) = delete; - ConcurrentThreadPool &operator=(const ConcurrentThreadPool &) = delete; - ConcurrentThreadPool &operator=(const ConcurrentThreadPool &&) = delete; + ConcurrentThreadPool(const ConcurrentThreadPool&) = delete; + ConcurrentThreadPool(const ConcurrentThreadPool&&) = delete; + ConcurrentThreadPool& operator=(const ConcurrentThreadPool&) = delete; + ConcurrentThreadPool& operator=(const ConcurrentThreadPool&&) = delete; public: - ConcurrentThreadPool(const size_t &max = 1, bool strt = 0, const uint64_t &nWait = 1000000) + ConcurrentThreadPool(const size_t& max = 1, bool strt = 0, const uint64_t& nWait = 1000000) : ConcurrentThreadQueue(max, 0, nWait) { for (size_t i = 0; i < max; i++) { auto n = std::to_string(i); @@ -416,21 +417,21 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { stop(); join(); } - virtual ConcurrentThreadPool &start() override { + virtual ConcurrentThreadPool& start() override { if (!_up) { _up = 1; _thread.run(); - for (const auto &t : _k) t.second->run(); + for (const auto& t : _k) t.second->run(); } return *this; } - virtual ConcurrentThreadPool &stop() override { + virtual ConcurrentThreadPool& stop() override { _up = 0; mkn::kul::ScopeLock l(_mmutex); - for (auto &t : _p) t.second->stop(); + for (auto& t : _p) t.second->stop(); return *this; } - virtual ConcurrentThreadPool &finish(const uint64_t &nWait = 1000000) + virtual ConcurrentThreadPool& finish(const uint64_t& nWait = 1000000) KTHROW(mkn::kul::Exception) override { while (_up && !_thread.exception()) { this_thread::nSleep(nWait); @@ -443,31 +444,31 @@ class ConcurrentThreadPool : public ConcurrentThreadQueue { if (!_p[std::to_string(i)]->ready()) break; if (i >= _max) { _up = 0; - for (auto &t : _p) t.second->stop(); + for (auto& t : _p) t.second->stop(); } } } } return *this; } - virtual ConcurrentThreadPool &interrupt() override { + virtual ConcurrentThreadPool& interrupt() override { mkn::kul::ScopeLock l(_mmutex); _thread.interrupt(); - for (auto &t : _k) t.second->interrupt(); + for (auto& t : _k) t.second->interrupt(); return *this; } - virtual ConcurrentThreadPool &join() override { + virtual ConcurrentThreadPool& join() override { _thread.join(); mkn::kul::ScopeLock l(_mmutex); - for (auto &t : _k) + for (auto& t : _k) if (t.second->started()) t.second->join(); return *this; } - virtual ConcurrentThreadPool &detach() override { + virtual ConcurrentThreadPool& detach() override { if (_up) { _thread.detach(); mkn::kul::ScopeLock l(_mmutex); - for (auto &t : _k) t.second->detach(); + for (auto& t : _k) t.second->detach(); } return *this; } @@ -478,7 +479,7 @@ class AutoChronPoolThread : public PoolThread { uint64_t m_scale = 0; public: - AutoChronPoolThread(const uint64_t &nWait = 1000000, const uint64_t &scale = 1000) + AutoChronPoolThread(const uint64_t& nWait = 1000000, const uint64_t& scale = 1000) : PoolThread(nWait), m_scale(scale) { if (m_scale > nWait) KEXCEPTION("Time scale cannot be larger than wait period"); } @@ -500,14 +501,14 @@ class ChroncurrentThreadPool : public ConcurrentThreadPool nWait) KEXCEPTION("Time scale cannot be larger than wait period"); if (strt) start(); diff --git a/inc/mkn/kul/time.hpp b/inc/mkn/kul/time.hpp index 6647fc5..2d6ed41 100644 --- a/inc/mkn/kul/time.hpp +++ b/inc/mkn/kul/time.hpp @@ -42,7 +42,7 @@ namespace kul { namespace time { class Exception : public mkn::kul::Exception { public: - Exception(char const *f, const int l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, const int l, std::string const& s) : mkn::kul::Exception(f, l, s) {} }; } // namespace time @@ -85,15 +85,15 @@ class DateTime { std::strftime(buffer, 80, f.c_str(), &ti); return std::string(buffer); } - static const std::string AS(std::string const &epoch, - std::string const &f = "%Y-%m-%d-%H:%M:%S") { + static const std::string AS(std::string const& epoch, + std::string const& f = "%Y-%m-%d-%H:%M:%S") { uint64_t e = 0; std::stringstream ss(epoch); ss >> e; if (!e) KEXCEPT(time::Exception, "Invalid time used :" + epoch); return AS(e, f); } - static const std::string NOW(std::string const &f = "%Y-%m-%d-%H:%M:%S") { + static const std::string NOW(std::string const& f = "%Y-%m-%d-%H:%M:%S") { return AS(std::time(NULL), f); } }; diff --git a/inc/mkn/kul/tuple.hpp b/inc/mkn/kul/tuple.hpp index 09043b6..1191cad 100644 --- a/inc/mkn/kul/tuple.hpp +++ b/inc/mkn/kul/tuple.hpp @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef _MKN_KUL_TUPLE_HPP_ #define _MKN_KUL_TUPLE_HPP_ -#include +#include #include #include "mkn/kul/for.hpp" diff --git a/inc/mkn/kul/wstring.hpp b/inc/mkn/kul/wstring.hpp index 3dbdace..ff4fde2 100644 --- a/inc/mkn/kul/wstring.hpp +++ b/inc/mkn/kul/wstring.hpp @@ -41,26 +41,26 @@ namespace kul { class WString { public: - static std::string toString(const std::wstring &ws) { return std::string(ws.begin(), ws.end()); } - static std::wstring toWString(std::string const &s) { return std::wstring(s.begin(), s.end()); } - static void replace(std::wstring &s, const std::wstring &f, const std::wstring &r) { + static std::string toString(const std::wstring& ws) { return std::string(ws.begin(), ws.end()); } + static std::wstring toWString(std::string const& s) { return std::wstring(s.begin(), s.end()); } + static void replace(std::wstring& s, const std::wstring& f, const std::wstring& r) { s = s.substr(0, s.find(f)) + r + s.substr(s.find(f) + f.size()); } - static void replaceAll(std::wstring &s, const std::wstring &f, const std::wstring &r) { + static void replaceAll(std::wstring& s, const std::wstring& f, const std::wstring& r) { while (s.find(f) < s.size()) replace(s, f, r); } - static void leftTrim(std::wstring &s, const wchar_t &d = ' ') { + static void leftTrim(std::wstring& s, const wchar_t& d = ' ') { while (s.find(d) == 0) s = s.substr(1); } - static void rightTrim(std::wstring &s, const wchar_t &d = ' ') { + static void rightTrim(std::wstring& s, const wchar_t& d = ' ') { while (s.rfind(d) == s.size()) s = s.substr(0, s.size() - 2); } - static void trim(std::wstring &s) { + static void trim(std::wstring& s) { while (s.find(' ') == 0 || s.find('\t') == 0) s = s.substr(1); while (s.rfind(' ') == s.size() - 1 || s.rfind('\t') == s.size() - 1) s = s.substr(0, s.size() - 2); } - static std::vector split(const std::wstring &s, const wchar_t &d) { + static std::vector split(const std::wstring& s, const wchar_t& d) { std::vector ss; std::wstring l = s; uint16_t pos = 0; @@ -71,7 +71,7 @@ class WString { ss.push_back(l); return ss; } - static std::vector split(const std::wstring &s, const std::wstring &d) { + static std::vector split(const std::wstring& s, const std::wstring& d) { std::vector ss; std::wstring l = s; uint16_t pos = 0; diff --git a/inc/mkn/kul/yaml.hpp b/inc/mkn/kul/yaml.hpp index 229debb..4029dd8 100644 --- a/inc/mkn/kul/yaml.hpp +++ b/inc/mkn/kul/yaml.hpp @@ -55,7 +55,8 @@ class NodeValidator; class Exception : public mkn::kul::Exception { public: - Exception(char const *f, uint16_t const &l, std::string const &s) : mkn::kul::Exception(f, l, s) {} + Exception(char const* f, uint16_t const& l, std::string const& s) + : mkn::kul::Exception(f, l, s) {} }; enum NodeType { NON = 0, STRING, LIST, MAP }; @@ -68,14 +69,14 @@ class NodeValidator { std::vector kids; public: - NodeValidator(std::string const &n, bool m = 0) : m(m), typ(STRING), nam(n) {} - NodeValidator(std::string const &n, const std::vector &c, bool m, - const NodeType &typ) + NodeValidator(std::string const& n, bool m = 0) : m(m), typ(STRING), nam(n) {} + NodeValidator(std::string const& n, const std::vector& c, bool m, + const NodeType& typ) : m(m), typ(typ), nam(n), kids(c) {} - const std::vector &children() const { return this->kids; } + const std::vector& children() const { return this->kids; } bool mandatory() const { return this->m; } - std::string const &name() const { return this->nam; } - const NodeType &type() const { return this->typ; } + std::string const& name() const { return this->nam; } + const NodeType& type() const { return this->typ; } }; class Item { @@ -85,24 +86,24 @@ class Item { YAML::Node r; // root Item() {} - Item(YAML::Node const &r) : r(r) {} + Item(YAML::Node const& r) : r(r) {} public: virtual ~Item() {} - YAML::Node const &root() const { return r; } + YAML::Node const& root() const { return r; } - static void VALIDATE(YAML::Node const &n, const std::vector &nvs) + static void VALIDATE(YAML::Node const& n, const std::vector& nvs) KTHROW(Exception) { mkn::kul::hash::set::String keys; - for (const auto &nv : nvs) + for (const auto& nv : nvs) if (nv.name() == "*") return; for (YAML::const_iterator it = n.begin(); it != n.end(); ++it) { - std::string const &key(it->first.as()); + std::string const& key(it->first.as()); if (keys.count(key)) KEXCEPTION("Duplicate key detected: " + key); keys.insert(key); bool f = 0; - for (const auto &nv : nvs) { + for (const auto& nv : nvs) { if (nv.name() != key) continue; f = 1; if (nv.type() == 1 && it->second.Type() != 2) KEXCEPTION("String expected: " + nv.name()); @@ -114,7 +115,7 @@ class Item { } if (!f) KEXCEPTION("Unexpected key: " + key); } - for (const auto &nv : nvs) { + for (const auto& nv : nvs) { if (nv.mandatory() && !keys.count(nv.name())) KEXCEPTION("Key mandatory: : " + nv.name()); } } @@ -125,9 +126,9 @@ class Validator { const std::vector kids; public: - Validator(const std::vector &kids) : kids(kids) {} - const std::vector &children() const { return this->kids; } - void validate(YAML::Node const &n) { Item::VALIDATE(n, children()); } + Validator(const std::vector& kids) : kids(kids) {} + const std::vector& children() const { return this->kids; } + void validate(YAML::Node const& n) { Item::VALIDATE(n, children()); } }; class String : public Item { @@ -135,14 +136,14 @@ class String : public Item { const std::string s; public: - String(std::string const &s) KTHROW(Exception) : s(s) { + String(std::string const& s) KTHROW(Exception) : s(s) { try { r = YAML::Load(s); - } catch (const std::exception &e) { + } catch (const std::exception& e) { KEXCEPTION("YAML failed to parse\nError/String: " + std::string(e.what())) << "\n" << s; } } - YAML::Node const &validate(const Validator &&v) KTHROW(Exception) { + YAML::Node const& validate(const Validator&& v) KTHROW(Exception) { Item::VALIDATE(root(), v.children()); return r; } @@ -151,30 +152,30 @@ class String : public Item { class File : public Item { public: template - static T CREATE(std::string const &f) KTHROW(Exception) { + static T CREATE(std::string const& f) KTHROW(Exception) { T file(f); try { Item::VALIDATE(file.root(), file.validator().children()); - } catch (const mkn::kul::yaml::Exception &e) { + } catch (const mkn::kul::yaml::Exception& e) { KEXCEPTION("YAML error encountered in file: " + f); } return file; } virtual ~File() {} - std::string const &file() const { return f; } + std::string const& file() const { return f; } const virtual Validator validator() const = 0; protected: void reload() KTHROW(Exception) { try { r = YAML::LoadFile(f); - } catch (const std::exception &e) { + } catch (const std::exception& e) { KEXCEPTION("YAML failed to parse\nFile: " + f); } } - File(File const &f) : Item(f.r), f(f.f) {} - File(mkn::kul::File const &f) KTHROW(Exception) : f(f.real()) { reload(); } - File(std::string const &f) KTHROW(Exception) : f(f) { reload(); } + File(File const& f) : Item(f.r), f(f.f) {} + File(mkn::kul::File const& f) KTHROW(Exception) : f(f.real()) { reload(); } + File(std::string const& f) KTHROW(Exception) : f(f) { reload(); } private: const std::string f; // file diff --git a/mkn.yaml b/mkn.yaml index abe48f5..62e3c03 100644 --- a/mkn.yaml +++ b/mkn.yaml @@ -31,20 +31,20 @@ profile: - name: test parent: lib inc: . - main: test/test.cpp + main: tst/test.cpp mode: none dep: google.test if_arg: win_shared: -DGTEST_LINKED_AS_SHARED_LIBRARY=1 - - name: bench + - name: usage parent: lib - main: test/bench.cpp - dep: google.benchmark + main: tst/usage.cpp - - name: usage + - name: bench parent: lib - main: test/usage.cpp + dep: google.benchmark + test: res/bench/(\w).cpp - name: format mod: | diff --git a/test/bench.cpp b/res/bench/bench.cpp similarity index 89% rename from test/bench.cpp rename to res/bench/bench.cpp index c1feea2..879927e 100644 --- a/test/bench.cpp +++ b/res/bench/bench.cpp @@ -40,7 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "benchmark/benchmark_api.h" #endif -void createDeleteFile(benchmark::State &state) { +void createDeleteFile(benchmark::State& state) { while (state.KeepRunning()) { mkn::kul::File f("tmp.tmp"); f.mk(); @@ -49,7 +49,7 @@ void createDeleteFile(benchmark::State &state) { } BENCHMARK(createDeleteFile)->Unit(benchmark::kMicrosecond); -void parseStringAsCommandLineArguments(benchmark::State &state) { +void parseStringAsCommandLineArguments(benchmark::State& state) { while (state.KeepRunning()) { std::vector v; mkn::kul::cli::asArgs("/path/to \"words in quotes\" words\\ not\\ in\\ quotes end", v); @@ -57,7 +57,7 @@ void parseStringAsCommandLineArguments(benchmark::State &state) { } BENCHMARK(parseStringAsCommandLineArguments)->Unit(benchmark::kMicrosecond); -void splitStringByChar(benchmark::State &state) { +void splitStringByChar(benchmark::State& state) { while (state.KeepRunning()) { std::vector v; mkn::kul::String::SPLIT("split - by - char - dash", '-', v); @@ -65,7 +65,7 @@ void splitStringByChar(benchmark::State &state) { } BENCHMARK(splitStringByChar)->Unit(benchmark::kMicrosecond); -void splitStringByString(benchmark::State &state) { +void splitStringByString(benchmark::State& state) { while (state.KeepRunning()) { std::vector v; mkn::kul::String::SPLIT("split - by - char - dash", "-", v); @@ -73,7 +73,7 @@ void splitStringByString(benchmark::State &state) { } BENCHMARK(splitStringByString)->Unit(benchmark::kMicrosecond); -void splitStringByEscapedChar(benchmark::State &state) { +void splitStringByEscapedChar(benchmark::State& state) { while (state.KeepRunning()) { std::vector v; mkn::kul::String::ESC_SPLIT("split \\- by - char - dash", '-', v); @@ -86,7 +86,7 @@ auto lambda = [](uint a, uint b) { (void)c; }; -void concurrentThreadPool(benchmark::State &state) { +void concurrentThreadPool(benchmark::State& state) { while (state.KeepRunning()) { mkn::kul::ConcurrentThreadPool<> ctp(3, 1); for (size_t i = 0; i < 10000; i++) ctp.async(std::bind(lambda, 2, 4)); @@ -95,7 +95,7 @@ void concurrentThreadPool(benchmark::State &state) { } BENCHMARK(concurrentThreadPool)->Unit(benchmark::kMicrosecond); -void chroncurrentThreadPool(benchmark::State &state) { +void chroncurrentThreadPool(benchmark::State& state) { while (state.KeepRunning()) { mkn::kul::ChroncurrentThreadPool<> ctp(3, 1); for (size_t i = 0; i < 10000; i++) ctp.async(std::bind(lambda, 2, 4)); @@ -104,7 +104,7 @@ void chroncurrentThreadPool(benchmark::State &state) { } BENCHMARK(chroncurrentThreadPool)->Unit(benchmark::kMicrosecond); -int main(int argc, char **argv) { +int main(int argc, char** argv) { ::benchmark::Initialize(&argc, argv); ::benchmark::RunSpecifiedBenchmarks(); } diff --git a/res/bench/sort.cpp b/res/bench/sort.cpp new file mode 100644 index 0000000..659bcf0 --- /dev/null +++ b/res/bench/sort.cpp @@ -0,0 +1,69 @@ +// #include "mkn/kul/log.hpp" + +#include +#include +#include + +#include "mkn/kul/sort.hpp" + +#if __has_include("benchmark/benchmark.h") +#include "benchmark/benchmark.h" +#else +#include "benchmark/benchmark_api.h" +#endif + +namespace mkn::kul { + +static std::vector SIZES{/*100000,*/ 5000000}; + +template +struct SortFixture : public benchmark::Fixture { + public: + void SetUp(::benchmark::State const& state) override {} + + void TearDown(::benchmark::State const& /*state*/) override {} + + void solve(::benchmark::State& state) { + std::uint32_t size = state.range(0); + + auto gen = [&]() { + std::random_device rd; + std::seed_seq seed_seq{rd(), rd(), rd(), rd(), rd(), rd(), rd()}; + return std::mt19937_64(seed_seq); + }(); + + std::vector vec(size); + + std::uniform_real_distribution dist{0, 10000}; + for (std::size_t i = 0; i < size; ++i) vec[i] = dist(gen); + + for (auto _ : state) + if constexpr (std) + std::sort(vec.begin(), vec.end()); + else + mkn::kul::sort(vec); + } +}; + +BENCHMARK_TEMPLATE_DEFINE_F(SortFixture, std_double, double, true)(benchmark::State& state) { + solve(state); +} +BENCHMARK_REGISTER_F(SortFixture, std_double) + ->Unit(benchmark::kNanosecond) + ->Threads(1) + ->ArgsProduct({SIZES}); + +BENCHMARK_TEMPLATE_DEFINE_F(SortFixture, _double, double, false)(benchmark::State& state) { + solve(state); +} +BENCHMARK_REGISTER_F(SortFixture, _double) + ->Unit(benchmark::kNanosecond) + ->Threads(1) + ->ArgsProduct({SIZES}); + +} // namespace mkn::kul + +int main(int argc, char** argv) { + ::benchmark::Initialize(&argc, argv); + ::benchmark::RunSpecifiedBenchmarks(); +} \ No newline at end of file diff --git a/src/os/win/proc.cpp b/src/os/win/proc.cpp index 7617e9c..5c3b77a 100644 --- a/src/os/win/proc.cpp +++ b/src/os/win/proc.cpp @@ -30,7 +30,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "mkn/kul/proc.hpp" -void mkn::kul::Process::expand(std::string &s) const { +void mkn::kul::Process::expand(std::string& s) const { #include "mkn/kul/src/proc/expand.cpp" } diff --git a/src/os/win/signal.cpp b/src/os/win/signal.cpp index 5fb4890..ea9c551 100644 --- a/src/os/win/signal.cpp +++ b/src/os/win/signal.cpp @@ -30,6 +30,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "mkn/kul/signal.hpp" -void kul_real_se_handler(EXCEPTION_POINTERS *pExceptionInfo) { +void kul_real_se_handler(EXCEPTION_POINTERS* pExceptionInfo) { #include "mkn/kul/src/signal/se_handler.cpp" } diff --git a/test/test.cpp b/tst/test.cpp similarity index 92% rename from test/test.cpp rename to tst/test.cpp index 4de3d22..c0495c1 100644 --- a/test/test.cpp +++ b/tst/test.cpp @@ -32,6 +32,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "gmock/gmock.h" #include "gtest/gtest.h" +#include + #include "mkn/kul/assert.hpp" #include "mkn/kul/cli.hpp" #include "mkn/kul/io.hpp" @@ -41,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/kul/proc.hpp" #include "mkn/kul/threads.hpp" #include "mkn/kul/span.hpp" +#include "mkn/kul/sort.hpp" #include "mkn/kul/tuple.hpp" #ifdef _WIN32 @@ -48,10 +51,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif auto tryCatch = [](std::vector> funcs, bool katch) { - for (const auto &func : funcs) try { + for (const auto& func : funcs) try { func(); ASSERT_TRUE(!katch); - } catch (const mkn::kul::Exception &e) { + } catch (const mkn::kul::Exception& e) { if (!katch) KOUT(NON) << e.debug(); ASSERT_TRUE(katch); } @@ -65,8 +68,9 @@ auto tryCatch = [](std::vector> funcs, bool katch) { #include "test/proc.ipp" #include "test/string.ipp" #include "test/span.ipp" +#include "test/sort.ipp" -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { KOUT(NON) << __FILE__; ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); diff --git a/test/test/cli.ipp b/tst/test/cli.ipp similarity index 100% rename from test/test/cli.ipp rename to tst/test/cli.ipp diff --git a/test/test/except.ipp b/tst/test/except.ipp similarity index 100% rename from test/test/except.ipp rename to tst/test/except.ipp diff --git a/test/test/io.ipp b/tst/test/io.ipp similarity index 100% rename from test/test/io.ipp rename to tst/test/io.ipp diff --git a/test/test/math.ipp b/tst/test/math.ipp similarity index 100% rename from test/test/math.ipp rename to tst/test/math.ipp diff --git a/test/test/os.ipp b/tst/test/os.ipp similarity index 100% rename from test/test/os.ipp rename to tst/test/os.ipp diff --git a/test/test/proc.ipp b/tst/test/proc.ipp similarity index 100% rename from test/test/proc.ipp rename to tst/test/proc.ipp diff --git a/tst/test/sort.ipp b/tst/test/sort.ipp new file mode 100644 index 0000000..ee809ce --- /dev/null +++ b/tst/test/sort.ipp @@ -0,0 +1,185 @@ + + +TEST(QSort, ascending) { + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, + 3, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9}; + mkn::kul::sort(v); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, half) { + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, + 3, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9}; + auto start = v.size() / 2; + auto end = v.size() - 1; + mkn::kul::sort(v, start, end); + auto prev = v[start]; + for (std::size_t i = start + 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, descending) { + auto constexpr ascending = false; + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, + 3, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9}; + mkn::kul::sort(v); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_GE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, parallel_ascending0) { // 128 values + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, + 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, + 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, + 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 3, 7, 8, 9, 2, 3, 4, 5, 6, 7, + 8, 9, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 2, 3, 3, 2, + 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4}; + mkn::kul::para_sort(v); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, parallel_ascending1) { // 126 values + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, + 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, + 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, + 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 3, 7, 8, 9, 2, 3, + 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, + 3, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1}; + mkn::kul::para_sort(v); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, parallel_ascending2) { // 4 values + // defaults to serial if elements are smaller than a cache line + std::vector v{1, 2, 3, 1}; + mkn::kul::para_sort(v); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, parallel_ascending3) { // 128 values + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, + 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, + 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, + 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 3, 7, 8, 9, 2, 3, 4, 5, 6, 7, + 8, 9, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 2, 3, 3, 2, + 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4}; + mkn::kul::para_sort(v, 3); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, parallel_ascending4) { // 126 values + std::vector v{1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, + 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, + 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, + 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 3, 7, 8, 9, 2, 3, + 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, + 3, 2, 3, 3, 2, 1, 6, 1, 6, 4, 1, 2, 3, 1, 2, 3, 3, 2, 1, 6, 1}; + mkn::kul::para_sort(v, 3); + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +auto constexpr arr_LT_fn = [](auto const& x, auto const& y) { + for (std::uint16_t i = 0; i < x.size(); ++i) + if (x[i] < y[i]) + return true; + else if (x[i] != y[i]) + return false; + return false; +}; + +auto constexpr arr_GT_fn = [](auto const& x, auto const& y) { + for (std::uint16_t i = 0; i < x.size(); ++i) + if (x[i] > y[i]) + return true; + else if (x[i] != y[i]) + return false; + return false; +}; + +TEST(QSort, ascending_array) { + using C = std::vector>; + // clang-format off + C v{{1, 2, 3}, {3, 3, 3}, {2, 1, 2}, {3, 1, 3}, + {1, 3, 1}, {0, 2, 0}, {2, 0, 2}, {0, 0, 1}, + {1, 1, 0}, {4, 0, 4}, {0, 0, 4}, {4,4,0} + }; + // clang-format on + + mkn::kul::sort(v, arr_LT_fn, arr_GT_fn); + + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, ascending_array1) { + using C = std::vector>; + // clang-format off + C v{{1, 2, 3}, {3, 3, 3}, {2, 1, 2}, {3, 1, 3}, + {1, 3, 1}, {0, 2, 0}, {2, 0, 2}, {0, 0, 1}, + {1, 1, 0}, {4, 0, 4}, {0, 0, 4}, {4,4,0} + }; + // clang-format on + + mkn::kul::sort(v); // will break in C++20? + + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} + +TEST(QSort, para_ascending_array1) { + using C = std::vector>; + // clang-format off + C v{{1, 2, 3}, {3, 3, 3}, {2, 1, 2}, {3, 1, 3}, + {1, 3, 1}, {0, 2, 0}, {2, 0, 2}, {0, 0, 1}, + {1, 1, 0}, {4, 0, 4}, {0, 0, 4}, {4,4,0}, + {1, 2, 3}, {3, 3, 3}, {2, 1, 2}, {3, 1, 3}, + {1, 3, 1}, {0, 2, 0}, {2, 0, 2}, {0, 0, 1}, + {1, 1, 0}, {4, 0, 4}, {0, 0, 4}, {4,4,0}, + {1, 2, 3}, {3, 3, 3}, {2, 1, 2}, {3, 1, 3}, + {1, 3, 1}, {0, 2, 0}, {2, 0, 2}, {0, 0, 1} + }; + // clang-format on + + mkn::kul::para_sort(v); // will break in C++20? + + auto prev = v[0]; + for (std::size_t i = 1; i < v.size(); ++i) { + EXPECT_LE(prev, v[i]); + prev = v[i]; + } +} diff --git a/test/test/span.ipp b/tst/test/span.ipp similarity index 100% rename from test/test/span.ipp rename to tst/test/span.ipp diff --git a/test/test/string.ipp b/tst/test/string.ipp similarity index 96% rename from test/test/string.ipp rename to tst/test/string.ipp index 937d939..bfbc204 100644 --- a/test/test/string.ipp +++ b/tst/test/string.ipp @@ -32,7 +32,9 @@ TEST(StringOperations, SplitByEscapedChar) { TEST(StringOperations, String_2_UInt16_t_invalid_tooLarge) { tryCatch( {// toolarge - []() { mkn::kul::String::UINT16(std::to_string((std::numeric_limits::max)() + 10)); }}, + []() { + mkn::kul::String::UINT16(std::to_string((std::numeric_limits::max)() + 10)); + }}, true); } TEST(StringOperations, String_2_UInt16_t_invalid_tooNegative) { @@ -57,7 +59,9 @@ TEST(StringOperations, String_2_UInt16_t_valid) { TEST(StringOperations, String_2_Int16_t_invalid_tooLarge) { tryCatch( {// toolarge - []() { mkn::kul::String::INT16(std::to_string((std::numeric_limits::max)() + 10)); }}, + []() { + mkn::kul::String::INT16(std::to_string((std::numeric_limits::max)() + 10)); + }}, true); } TEST(StringOperations, String_2_Int16_t_invalid_tooNegative) { diff --git a/test/usage.cpp b/tst/usage.cpp similarity index 87% rename from test/usage.cpp rename to tst/usage.cpp index 6d162c9..ddddae8 100644 --- a/test/usage.cpp +++ b/tst/usage.cpp @@ -78,10 +78,10 @@ class TestThreadObject { class TestThreadQueueObject { protected: int i = 0; - Mutex &mutex; + Mutex& mutex; public: - TestThreadQueueObject(Mutex &_mutex) : mutex(_mutex) {} + TestThreadQueueObject(Mutex& _mutex) : mutex(_mutex) {} void operator()() { mkn::kul::ScopeLock lock(mutex); KLOG(INF) << "THREAD RUNNING"; @@ -93,10 +93,10 @@ class TestThreadQueueObject { class TestThreadQueueQObject : public TestThreadQueueObject { private: - std::queue &q; + std::queue& q; public: - TestThreadQueueQObject(Mutex &_mutex, std::queue &_q) + TestThreadQueueQObject(Mutex& _mutex, std::queue& _q) : TestThreadQueueObject(_mutex), q(_q) {} void operator()() { mkn::kul::ScopeLock lock(mutex); @@ -120,7 +120,7 @@ class TestConcQueueQObject { class TestIPCServer : public mkn::kul::ipc::Server { public: TestIPCServer() : mkn::kul::ipc::Server("uuid", 1) {} // UUID CHECKS ONCE - void handle(std::string const &s) { KLOG(INF) << "TestIPCServer " << s; } + void handle(std::string const& s) { KLOG(INF) << "TestIPCServer " << s; } void operator()() { listen(); } }; @@ -138,7 +138,7 @@ class TestIPC { class Catch { public: - void print(const int16_t &s) { + void print(const int16_t& s) { (void)s; KOUT(NON) << "SEGMENTATION FAULT INTERCEPTED"; KOUT(NON) << "PRINT STACKTRACE"; @@ -150,11 +150,11 @@ class Test { const std::string s; public: - Test(int argc, char *argv[]) : s("LAMBDAS ALLOWED IN SIGNAL") { + Test(int argc, char* argv[]) : s("LAMBDAS ALLOWED IN SIGNAL") { KUL_DBG_FUNC_ENTER; Catch c; mkn::kul::Signal sig; // Windows: each thread requires own handler, static - // singleton otherwise so only ever one. + // singleton otherwise so only ever one. sig.segv(std::bind(&Catch::print, std::ref(c), std::placeholders::_1)); // Vector of // functions to call @@ -183,8 +183,8 @@ class Test { { mkn::kul::io::Writer wo(fo); mkn::kul::io::Writer we(fe); - auto lo = [&](std::string const &_s) { wo << _s; }; - auto le = [&](std::string const &_s) { we << _s; }; + auto lo = [&](std::string const& _s) { wo << _s; }; + auto le = [&](std::string const& _s) { we << _s; }; mkn::kul::LogMan::INSTANCE().setOut(lo); mkn::kul::LogMan::INSTANCE().setErr(le); KOUT(INF) << "KOUT(INF)"; @@ -205,43 +205,44 @@ class Test { KLOG(INF) << os_inc.dir().relative(os_hpp.dir()); } - for (mkn::kul::Dir const &d : mkn::kul::Dir(mkn::kul::env::CWD()).dirs()) - for (mkn::kul::File const &f : d.files()) KOUT(NON) << d.join(f.name()); // or f.full() + for (mkn::kul::Dir const& d : mkn::kul::Dir(mkn::kul::env::CWD()).dirs()) + for (mkn::kul::File const& f : d.files()) KOUT(NON) << d.join(f.name()); // or f.full() try { mkn::kul::Process("echo").arg("Hello").arg("World").start(); - } catch (const mkn::kul::proc::ExitException &e) { + } catch (const mkn::kul::proc::ExitException& e) { KERR << e.stack(); KERR << e.code(); - } catch (const mkn::kul::proc::Exception &e) { + } catch (const mkn::kul::proc::Exception& e) { KERR << e.debug() << " : " << typeid(e).name(); KERR << "Error expected on windows without echo on path"; } { - std::vector argV{{mkn::kul::cli::Arg('f', "flag"), - mkn::kul::cli::Arg('m', "maybe_value", mkn::kul::cli::ArgType::MAYBE), - mkn::kul::cli::Arg('o', "option", mkn::kul::cli::ArgType::STRING)}}; + std::vector argV{ + {mkn::kul::cli::Arg('f', "flag"), + mkn::kul::cli::Arg('m', "maybe_value", mkn::kul::cli::ArgType::MAYBE), + mkn::kul::cli::Arg('o', "option", mkn::kul::cli::ArgType::STRING)}}; std::vector cmdV{{"COMMAND"}}; mkn::kul::cli::Args args(cmdV, argV); try { args.process(argc, argv); - } catch (const mkn::kul::cli::Exception &e) { + } catch (const mkn::kul::cli::Exception& e) { KEXIT(1, e.what()); } } - for (std::string const &arg : + for (std::string const& arg : mkn::kul::cli::asArgs("/path/to \"words in quotes\" words\\ not\\ in\\ quotes end")) KOUT(NON) << "ARG: " << arg; - for (std::string const &arg : mkn::kul::String::SPLIT("split - by - char - dash", '-')) + for (std::string const& arg : mkn::kul::String::SPLIT("split - by - char - dash", '-')) KOUT(NON) << "BIT: " << arg; - for (std::string const &arg : mkn::kul::String::SPLIT("split - by - string - dash", "-")) + for (std::string const& arg : mkn::kul::String::SPLIT("split - by - string - dash", "-")) KOUT(NON) << "BIT: " << arg; - for (std::string const &arg : + for (std::string const& arg : mkn::kul::String::ESC_SPLIT("split \\- by - char - dash with escape backslash", '-')) KOUT(NON) << "BIT: " << arg; @@ -275,9 +276,9 @@ class Test { mkn::kul::Span raw = spanset.raw(); std::vector vals{1, 5, 5, 3, 4, 5}; for (size_t i = 0; i < 6; i++) raw[i] = vals[i]; - for (auto const &span : spanset) { + for (auto const& span : spanset) { KLOG(INF) << span.size(); - for (auto const &d0 : span) KLOG(INF) << d0; + for (auto const& d0 : span) KLOG(INF) << d0; } } @@ -345,7 +346,7 @@ class Test { KLOG(INF) << (a + b); KEXCEPTION("Exceptional!"); }; - auto lambex = [](const mkn::kul::Exception &e) { KLOG(ERR) << e.stack(); }; + auto lambex = [](const mkn::kul::Exception& e) { KLOG(ERR) << e.stack(); }; ctp.async(std::bind(lambdb, 2, 4), std::bind(lambex, std::placeholders::_1)); mkn::kul::this_thread::sleep(500); ctp.block().finish().join(); @@ -359,7 +360,7 @@ class Test { KLOG(INF) << (a + b); KEXCEPT(mkn::kul::Exception, "Exceptional!"); }; - auto lambex = [](const mkn::kul::Exception &e) { KLOG(ERR) << e.stack(); }; + auto lambex = [](const mkn::kul::Exception& e) { KLOG(ERR) << e.stack(); }; ctp.async(std::bind(lambdb, 2, 4), std::bind(lambex, std::placeholders::_1)); mkn::kul::this_thread::sleep(500); ctp.block().finish().join(); @@ -384,12 +385,12 @@ class Test { } // namespace kul } // namespace mkn -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { try { mkn::kul::Test(argc, argv); - } catch (const mkn::kul::Exception &e) { + } catch (const mkn::kul::Exception& e) { KERR << e.stack(); - } catch (const std::exception &e) { + } catch (const std::exception& e) { KERR << e.what(); } catch (...) { KERR << "UNKNOWN EXCEPTION CAUGHT";