pmt: improve PureTuple class and improve function manager.
This commit is contained in:
@@ -34,63 +34,6 @@
|
|||||||
#define PART_MAP (*VARS.PartMap)
|
#define PART_MAP (*VARS.PartMap)
|
||||||
|
|
||||||
namespace PartitionManager {
|
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<int> 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<std::unique_ptr<basic_function>> _functions;
|
|
||||||
|
|
||||||
public:
|
|
||||||
void registerFunction(std::unique_ptr<basic_function> _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<PartitionMap::BuildMap> 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<VariableTable> Variables;
|
|
||||||
extern FILE *pstdout, *pstderr;
|
|
||||||
|
|
||||||
int Main(int argc, char **argv);
|
int Main(int argc, char **argv);
|
||||||
|
|
||||||
// Print messages if not using quiet mode
|
// 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
|
std::string getAppVersion(); // Not Android app version (an Android app is
|
||||||
// planned!), tells pmt version.
|
// 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<int> 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 _Type>
|
||||||
|
class basic_manager {
|
||||||
|
private:
|
||||||
|
std::vector<std::unique_ptr<_Type>> _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<PartitionMap::BuildMap> PartMap;
|
||||||
|
|
||||||
|
std::string searchPath, logFile;
|
||||||
|
bool onLogical;
|
||||||
|
bool quietProcess;
|
||||||
|
bool verboseMode;
|
||||||
|
bool viewVersion;
|
||||||
|
bool forceProcess;
|
||||||
|
};
|
||||||
|
|
||||||
|
using FunctionBase = basic_function;
|
||||||
|
using FunctionManager = basic_manager<FunctionBase>;
|
||||||
|
using FunctionFlags = basic_function_flags;
|
||||||
|
using VariableTable = basic_variables;
|
||||||
|
using Error = Helper::Error;
|
||||||
|
|
||||||
|
extern std::unique_ptr<VariableTable> Variables;
|
||||||
|
extern FILE *pstdout, *pstderr;
|
||||||
} // namespace PartitionManager
|
} // namespace PartitionManager
|
||||||
|
|
||||||
#endif // #ifndef LIBPMT_LIB_HPP
|
#endif // #ifndef LIBPMT_LIB_HPP
|
||||||
|
|||||||
@@ -66,61 +66,4 @@ void processCommandLine(std::vector<std::string> &vec1,
|
|||||||
if (vec1.empty() && !s1.empty()) vec1.push_back(s1);
|
if (vec1.empty() && !s1.empty()) vec1.push_back(s1);
|
||||||
if (vec2.empty() && !s2.empty()) vec2.push_back(s2);
|
if (vec2.empty() && !s2.empty()) vec2.push_back(s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void basic_function_manager::registerFunction(
|
|
||||||
std::unique_ptr<basic_function> _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
|
} // namespace PartitionManager
|
||||||
|
|||||||
@@ -18,6 +18,5 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// Call integrated main function in library
|
// Call integrated main function in library
|
||||||
Helper::LoggingProperties::setProgramName(PMTE);
|
|
||||||
return PartitionManager::Main(argc, argv);
|
return PartitionManager::Main(argc, argv);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ basic_variables::basic_variables()
|
|||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor)) void init() {
|
__attribute__((constructor)) void init() {
|
||||||
|
Helper::LoggingProperties::setProgramName(PMTE);
|
||||||
Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log");
|
Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -214,6 +214,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
Data *tuple_data = nullptr;
|
Data *tuple_data = nullptr;
|
||||||
|
Data tuple_data_type = { _Type1{}, _Type2{}, _Type3{}};
|
||||||
size_t capacity{}, count{};
|
size_t capacity{}, count{};
|
||||||
|
|
||||||
PureTuple() : tuple_data(new Data[20]), capacity(20), count(0) {}
|
PureTuple() : tuple_data(new Data[20]), capacity(20), count(0) {}
|
||||||
@@ -856,7 +857,7 @@ std::string getLibVersion();
|
|||||||
#define MKVERSION(name) \
|
#define MKVERSION(name) \
|
||||||
char vinfo[512]; \
|
char vinfo[512]; \
|
||||||
sprintf(vinfo, \
|
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", \
|
"BuildFlags: -Wall;-Werror;-Wno-deprecated-declarations;-Os", \
|
||||||
name); \
|
name); \
|
||||||
return std::string(vinfo)
|
return std::string(vinfo)
|
||||||
|
|||||||
@@ -120,11 +120,11 @@ int main(int argc, char **argv) {
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
Helper::PureTuple<int, std::string, bool> values = {
|
Helper::PureTuple<int, std::string, bool> 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(std::make_tuple(0, "hi", false));
|
||||||
values.insert(2, "im", true);
|
values.insert(2, "im", true);
|
||||||
values.insert(3, "helper", true);
|
values.insert({3, "helper", true});
|
||||||
values.pop({3, "helper", true});
|
values.pop({3, "helper", true});
|
||||||
values.pop_back();
|
values.pop_back();
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ int main(int argc, char **argv) {
|
|||||||
LOG(WARNING) << "Warning message" << std::endl;
|
LOG(WARNING) << "Warning message" << std::endl;
|
||||||
LOG(ERROR) << "Error message" << std::endl;
|
LOG(ERROR) << "Error message" << std::endl;
|
||||||
LOG(ABORT) << "Abort message" << std::endl;
|
LOG(ABORT) << "Abort message" << std::endl;
|
||||||
} catch (Helper::Error &err) {
|
} catch (std::exception &err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cout << err.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user