From 0ff94cc4b912c98e242d17fbe990b6a60479d644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1z=20Zengin?= Date: Tue, 23 Sep 2025 14:13:36 +0300 Subject: [PATCH] pmt: improve PureTuple class and improve function manager. --- include/PartitionManager/PartitionManager.hpp | 167 ++++++++++++------ src/FunctionManager.cpp | 57 ------ src/Main.cpp | 1 - src/PartitionManager.cpp | 1 + srclib/libhelper/include/libhelper/lib.hpp | 3 +- srclib/libhelper/tests/test.cpp | 6 +- 6 files changed, 115 insertions(+), 120 deletions(-) diff --git a/include/PartitionManager/PartitionManager.hpp b/include/PartitionManager/PartitionManager.hpp index 367ba4a..954d2e7 100644 --- a/include/PartitionManager/PartitionManager.hpp +++ b/include/PartitionManager/PartitionManager.hpp @@ -34,63 +34,6 @@ #define PART_MAP (*VARS.PartMap) namespace PartitionManager { -enum basic_function_flags { - NO_SU = 1, - NO_MAP_CHECK = 2, - ADB_SUFFICIENT = 3, -}; - -// All function classes must inherit from this class. -class basic_function { -public: - CLI::App *cmd = nullptr; - std::vector flags = {}; - - virtual bool init(CLI::App &_app) = 0; - virtual bool run() = 0; - - [[nodiscard]] virtual bool isUsed() const = 0; - [[nodiscard]] virtual const char *name() const = 0; - - virtual ~basic_function() = default; -}; - -// A class for function management. -class basic_function_manager final { -private: - std::vector> _functions; - -public: - void registerFunction(std::unique_ptr _func, CLI::App &_app); - - [[nodiscard]] bool hasFlagOnUsedFunction(int flag) const; - [[nodiscard]] bool isUsed(const std::string &name) const; - [[nodiscard]] bool handleAll() const; -}; - -class basic_variables final { -public: - basic_variables(); - - std::unique_ptr PartMap; - - std::string searchPath, logFile; - bool onLogical; - bool quietProcess; - bool verboseMode; - bool viewVersion; - bool forceProcess; -}; - -using FunctionBase = basic_function; -using FunctionManager = basic_function_manager; -using FunctionFlags = basic_function_flags; -using VariableTable = basic_variables; -using Error = Helper::Error; - -extern std::unique_ptr Variables; -extern FILE *pstdout, *pstderr; - int Main(int argc, char **argv); // Print messages if not using quiet mode @@ -116,6 +59,114 @@ std::string getLibVersion(); std::string getAppVersion(); // Not Android app version (an Android app is // planned!), tells pmt version. + +enum basic_function_flags { + NO_SU = 1, + NO_MAP_CHECK = 2, + ADB_SUFFICIENT = 3, +}; + +// All function classes must inherit from this class. +class basic_function { +public: + CLI::App *cmd = nullptr; + std::vector flags = {}; + + virtual bool init(CLI::App &_app) = 0; + virtual bool run() = 0; + + [[nodiscard]] virtual bool isUsed() const = 0; + [[nodiscard]] virtual const char *name() const = 0; + + virtual ~basic_function() = default; +}; + +// A class for function management. +template +class basic_manager { +private: + std::vector> _functions; + +public: + void registerFunction(std::unique_ptr<_Type> _func, CLI::App &_app) { + LOGN(PMTF, INFO) << "registering: " << _func->name() << std::endl; + for (const auto &f : _functions) { + if (std::string(_func->name()) == std::string(f->name())) { + LOGN(PMTF, INFO) << "Is already registered: " << _func->name() + << ". Skipping." << std::endl; + return; + } + } + if (!_func->init(_app)) + throw Helper::Error("Cannot init: %s", _func->name()); + _functions.push_back(std::move(_func)); + LOGN(PMTF, INFO) << _functions.back()->name() << " successfully registered." + << std::endl; + } + + [[nodiscard]] bool hasFlagOnUsedFunction(int flag) const { + for (const auto &func : _functions) { + if (func->isUsed()) { + std::for_each(func->flags.begin(), func->flags.end(), [&](const int x) { + LOGN(PMTF, INFO) << "Used flag " << x << " on " << func->name() + << std::endl; + }); + return std::find(func->flags.begin(), func->flags.end(), flag) != + func->flags.end(); + } + } + return false; + } + + [[nodiscard]] bool isUsed(const std::string &name) const { + if (_functions.empty()) return false; + for (const auto &func : _functions) { + if (func->name() == name) return func->isUsed(); + } + return false; + } + + [[nodiscard]] bool handleAll() const { + LOGN(PMTF, INFO) << "running caught commands in command-line." + << std::endl; + for (const auto &func : _functions) { + if (func->isUsed()) { + LOGN(PMTF, INFO) << func->name() + << " is calling because used in command-line." + << std::endl; + return func->run(); + } + } + + LOGN(PMTF, INFO) << "not found any used function from command-line." + << std::endl; + println("Target progress is not specified. Specify a progress."); + return false; + } +}; + +class basic_variables final { +public: + basic_variables(); + + std::unique_ptr PartMap; + + std::string searchPath, logFile; + bool onLogical; + bool quietProcess; + bool verboseMode; + bool viewVersion; + bool forceProcess; +}; + +using FunctionBase = basic_function; +using FunctionManager = basic_manager; +using FunctionFlags = basic_function_flags; +using VariableTable = basic_variables; +using Error = Helper::Error; + +extern std::unique_ptr Variables; +extern FILE *pstdout, *pstderr; } // namespace PartitionManager -#endif // #ifndef LIBPMT_LIB_HPP \ No newline at end of file +#endif // #ifndef LIBPMT_LIB_HPP diff --git a/src/FunctionManager.cpp b/src/FunctionManager.cpp index 322d047..64d70d7 100644 --- a/src/FunctionManager.cpp +++ b/src/FunctionManager.cpp @@ -66,61 +66,4 @@ void processCommandLine(std::vector &vec1, if (vec1.empty() && !s1.empty()) vec1.push_back(s1); if (vec2.empty() && !s2.empty()) vec2.push_back(s2); } - -void basic_function_manager::registerFunction( - std::unique_ptr _func, CLI::App &_app) { - LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl; - for (const auto &f : _functions) { - if (std::string(_func->name()) == std::string(f->name())) { - LOGN(PMTF, INFO) << "Function is already registered: " << _func->name() - << ". Skipping." << std::endl; - return; - } - } - if (!_func->init(_app)) - throw Error("Cannot init function: %s", _func->name()); - _functions.push_back(std::move(_func)); - LOGN(PMTF, INFO) << _functions.back()->name() << " successfully registered." - << std::endl; -} - -bool basic_function_manager::isUsed(const std::string &name) const { - if (_functions.empty()) return false; - for (const auto &func : _functions) { - if (func->name() == name) return func->isUsed(); - } - return false; -} - -bool basic_function_manager::hasFlagOnUsedFunction(const int flag) const { - for (const auto &func : _functions) { - if (func->isUsed()) { - std::for_each(func->flags.begin(), func->flags.end(), [&](const int x) { - LOGN(PMTF, INFO) << "Used flag " << x << " on " << func->name() - << std::endl; - }); - return std::find(func->flags.begin(), func->flags.end(), flag) != - func->flags.end(); - } - } - return false; -} - -bool basic_function_manager::handleAll() const { - LOGN(PMTF, INFO) << "running caught function commands in command-line." - << std::endl; - for (const auto &func : _functions) { - if (func->isUsed()) { - LOGN(PMTF, INFO) << func->name() - << " is calling because used in command-line." - << std::endl; - return func->run(); - } - } - - LOGN(PMTF, INFO) << "not found any used function from command-line." - << std::endl; - println("Target progress is not specified. Specify a progress."); - return false; -} } // namespace PartitionManager diff --git a/src/Main.cpp b/src/Main.cpp index 11be173..03e297e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -18,6 +18,5 @@ int main(int argc, char **argv) { // Call integrated main function in library - Helper::LoggingProperties::setProgramName(PMTE); return PartitionManager::Main(argc, argv); } diff --git a/src/PartitionManager.cpp b/src/PartitionManager.cpp index 182343a..2ff569e 100644 --- a/src/PartitionManager.cpp +++ b/src/PartitionManager.cpp @@ -47,6 +47,7 @@ basic_variables::basic_variables() } __attribute__((constructor)) void init() { + Helper::LoggingProperties::setProgramName(PMTE); Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log"); } diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index d2c1b1b..1026daa 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -214,6 +214,7 @@ public: }; Data *tuple_data = nullptr; + Data tuple_data_type = { _Type1{}, _Type2{}, _Type3{}}; size_t capacity{}, count{}; PureTuple() : tuple_data(new Data[20]), capacity(20), count(0) {} @@ -856,7 +857,7 @@ std::string getLibVersion(); #define MKVERSION(name) \ char vinfo[512]; \ sprintf(vinfo, \ - "%s 1.2.0\nBuildType: Release\nCompiler: clang\n" \ + "%s 1.3.0\nCompiler: clang\n" \ "BuildFlags: -Wall;-Werror;-Wno-deprecated-declarations;-Os", \ name); \ return std::string(vinfo) diff --git a/srclib/libhelper/tests/test.cpp b/srclib/libhelper/tests/test.cpp index adb7c39..9621c3d 100644 --- a/srclib/libhelper/tests/test.cpp +++ b/srclib/libhelper/tests/test.cpp @@ -120,11 +120,11 @@ int main(int argc, char **argv) { << std::endl; Helper::PureTuple values = { - {1, "hi", true}, {2, "im", true}, {3, "helper", false}}; + {1, "hi", true}, {2, "im", true}, {3, "helper", false}}; values.insert(std::make_tuple(0, "hi", false)); values.insert(2, "im", true); - values.insert(3, "helper", true); + values.insert({3, "helper", true}); values.pop({3, "helper", true}); values.pop_back(); @@ -141,7 +141,7 @@ int main(int argc, char **argv) { LOG(WARNING) << "Warning message" << std::endl; LOG(ERROR) << "Error message" << std::endl; LOG(ABORT) << "Abort message" << std::endl; - } catch (Helper::Error &err) { + } catch (std::exception &err) { std::cout << err.what() << std::endl; return 1; }