libsafestoi: Drop the usage of std::optional
- Drops the requirement of C++17 - Also adapt the library users code
This commit is contained in:
@@ -9,7 +9,6 @@ cc_binary {
|
||||
relative_install_path: "hw",
|
||||
init_rc: ["vendor.samsung_ext.framework.battery-service.rc"],
|
||||
vintf_fragments: ["vendor.samsung_ext.framework.battery-service.xml"],
|
||||
defaults: ["safestoi_defaults"],
|
||||
srcs: [
|
||||
"SmartCharge.cpp",
|
||||
"service.cpp",
|
||||
@@ -18,9 +17,9 @@ cc_binary {
|
||||
"libbase",
|
||||
"libbinder_ndk",
|
||||
"liblog",
|
||||
"libsafestoi",
|
||||
"vendor.samsung_ext.framework.battery-V1-ndk",
|
||||
],
|
||||
header_libs: ["libext_threadpool"],
|
||||
cpp_std: "c++17",
|
||||
system_ext_specific: true,
|
||||
}
|
||||
|
||||
@@ -42,19 +42,18 @@ struct ConfigPair {
|
||||
std::string fromPair(void) {
|
||||
return std::to_string(first) + kComma + std::to_string(second);
|
||||
}
|
||||
static std::optional<ConfigPair> fromString(const std::string &v) {
|
||||
std::optional<int> first, second;
|
||||
static bool fromString(const std::string &v, ConfigPair *pair) {
|
||||
std::stringstream ss(v);
|
||||
std::string res;
|
||||
|
||||
if (v.find(kComma) != std::string::npos) {
|
||||
getline(ss, res, kComma);
|
||||
first = stoi_safe(res);
|
||||
pair->first = stoi_safe(res);
|
||||
getline(ss, res, kComma);
|
||||
second = stoi_safe(res);
|
||||
if (first && second) return ConfigPair{*first, *second};
|
||||
pair->second = stoi_safe(res);
|
||||
return true;
|
||||
}
|
||||
return std::nullopt;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -62,7 +61,7 @@ class BatteryHelper {
|
||||
static int _readSysfs(const char *sysfs) {
|
||||
std::string data;
|
||||
ReadFileToString(sysfs, &data);
|
||||
return stoi_safe(data).value_or(-1);
|
||||
return stoi_safe(data);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -72,14 +71,14 @@ class BatteryHelper {
|
||||
static int getPercent(void) { return _readSysfs(kBatteryPercentSysfs); }
|
||||
};
|
||||
|
||||
static std::optional<ConfigPair> getAndParseIfPossible(const char *prop) {
|
||||
static bool getAndParse(const char *prop, ConfigPair *pair) {
|
||||
if (WaitForPropertyCreation(prop, std::chrono::milliseconds(500))) {
|
||||
std::string propval = GetProperty(prop, "");
|
||||
if (!propval.empty()) {
|
||||
return ConfigPair::fromString(propval);
|
||||
return ConfigPair::fromString(propval, pair);
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool verifyConfig(const int lower, const int upper) {
|
||||
@@ -88,12 +87,12 @@ static inline bool verifyConfig(const int lower, const int upper) {
|
||||
|
||||
SmartCharge::SmartCharge(void) {
|
||||
kPoolPtr = std::make_shared<ThreadPool>(3);
|
||||
#define func "<constructor>"
|
||||
#define func "SmartCharge()"
|
||||
kPoolPtr->Enqueue([this] {
|
||||
auto ret = getAndParseIfPossible(kSmartChargeConfigProp);
|
||||
if (ret.has_value() && verifyConfig(ret->first, ret->second)) {
|
||||
upper = ret->second;
|
||||
lower = ret->first;
|
||||
ConfigPair ret {};
|
||||
if (getAndParse(kSmartChargeConfigProp, &ret) && verifyConfig(ret.first, ret.second)) {
|
||||
upper = ret.second;
|
||||
lower = ret.first;
|
||||
ALOGD("%s: upper: %d, lower: %d", func, upper, lower);
|
||||
} else {
|
||||
upper = -1;
|
||||
@@ -101,11 +100,10 @@ SmartCharge::SmartCharge(void) {
|
||||
ALOGW("%s: Parsing config failed", func);
|
||||
return;
|
||||
}
|
||||
ret = getAndParseIfPossible(kSmartChargeEnabledProp);
|
||||
if (ret.has_value() && !!ret->first) {
|
||||
ALOGD("%s: Starting loop, withrestart: %d", func, !!ret->second);
|
||||
if (getAndParse(kSmartChargeEnabledProp, &ret) && !!ret.first) {
|
||||
ALOGD("%s: Starting loop, withrestart: %d", func, !!ret.second);
|
||||
kRun.store(true);
|
||||
startLoop(!!ret->second);
|
||||
startLoop(!!ret.second);
|
||||
} else
|
||||
ALOGD("%s: Not starting loop", func);
|
||||
});
|
||||
@@ -127,7 +125,7 @@ void SmartCharge::startLoop(bool withrestart) {
|
||||
if (per < 0) {
|
||||
kRun.store(false);
|
||||
SetProperty(kSmartChargeEnabledProp, ConfigPair{0, 0}.fromPair());
|
||||
ALOGE("%s: exit loop: per %d", __func__, per);
|
||||
ALOGE("%s: exit loop: percent: %d", __func__, per);
|
||||
break;
|
||||
}
|
||||
if (per > upper)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
cc_binary {
|
||||
name: "test_smartcharge",
|
||||
defaults: ["safestoi_defaults"],
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
"libbinder_ndk",
|
||||
"libsafestoi",
|
||||
"vendor.samsung_ext.framework.battery-V1-ndk",
|
||||
],
|
||||
header_libs: [
|
||||
|
||||
@@ -17,18 +17,14 @@ int main(int argc, const char **argv) {
|
||||
fprintf(stderr, "getService returned null\n");
|
||||
return 1;
|
||||
}
|
||||
std::optional<int> _arg1, _arg2, _arg3;
|
||||
int arg1, arg2, arg3;
|
||||
_arg1 = stoi_safe(argv[1]);
|
||||
_arg2 = stoi_safe(argv[2]);
|
||||
_arg3 = stoi_safe(argv[3]);
|
||||
if (!_arg1 || !_arg2 || !_arg3) {
|
||||
fprintf(stderr, "Failed to parse arguments to int\n");
|
||||
arg1 = stoi_safe(argv[1]);
|
||||
arg2 = stoi_safe(argv[2]);
|
||||
arg3 = stoi_safe(argv[3]);
|
||||
if (arg1 < 0 || arg2 < 0 || arg3 < 0) {
|
||||
fprintf(stderr, "Failed to parse arguments to int or is invalid input\n");
|
||||
return 1;
|
||||
}
|
||||
arg1 = *_arg1;
|
||||
arg2 = *_arg2;
|
||||
arg3 = *_arg3;
|
||||
switch (arg1) {
|
||||
case 1: {
|
||||
TEST_LOG2(svc, setChargeLimit, arg2, arg3);
|
||||
|
||||
@@ -3,7 +3,6 @@ cc_binary {
|
||||
relative_install_path: "hw",
|
||||
init_rc: ["vendor.samsung_ext.hardware.camera.flashlight-service.rc"],
|
||||
vintf_fragments: ["vendor.samsung_ext.hardware.camera.flashlight-service.xml"],
|
||||
defaults: ["safestoi_defaults"],
|
||||
srcs: [
|
||||
"Flashlight.cpp",
|
||||
"service.cpp",
|
||||
@@ -11,6 +10,7 @@ cc_binary {
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
"libbinder_ndk",
|
||||
"libsafestoi",
|
||||
"vendor.samsung_ext.hardware.camera.flashlight-V1-ndk",
|
||||
],
|
||||
system_ext_specific: true,
|
||||
|
||||
@@ -28,17 +28,16 @@ static constexpr const char *FLASH_BRIGHTNESS_PROP = "persist.ext.flashlight.las
|
||||
|
||||
ndk::ScopedAStatus Flashlight::getCurrentBrightness(int32_t* _aidl_return) {
|
||||
std::string value;
|
||||
int intvalue = -1;
|
||||
int intvalue;
|
||||
|
||||
ReadFileToString(FLASH_NODE, &value);
|
||||
intvalue = stoi_safe(value).value_or(-1);
|
||||
intvalue = stoi_safe(value);
|
||||
switch (intvalue) {
|
||||
case 0:
|
||||
*_aidl_return = 0;
|
||||
break;
|
||||
case 1:
|
||||
*_aidl_return = stoi_safe(GetProperty(FLASH_BRIGHTNESS_PROP, "1"))
|
||||
.value_or(level_saved);
|
||||
*_aidl_return = stoi_safe(GetProperty(FLASH_BRIGHTNESS_PROP, "1"), level_saved);
|
||||
break;
|
||||
case 1001:
|
||||
*_aidl_return = 1;
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
cc_library_shared {
|
||||
name: "libstoisafe",
|
||||
name: "libsafestoi",
|
||||
srcs: ["SafeStoi.cpp"],
|
||||
export_include_dirs: ["."],
|
||||
cpp_std: "c++17",
|
||||
system_ext_specific: true,
|
||||
}
|
||||
|
||||
cc_defaults {
|
||||
name: "safestoi_defaults",
|
||||
cpp_std: "c++17",
|
||||
shared_libs: ["libstoisafe"],
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include "SafeStoi.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
std::optional<int> stoi_safe(const std::string& str) {
|
||||
int stoi_safe(const std::string& str, const int fallback) {
|
||||
std::istringstream iss(str);
|
||||
int value;
|
||||
if (!(iss >> value)) {
|
||||
return std::nullopt;
|
||||
return fallback;
|
||||
}
|
||||
return std::optional(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
std::optional<int> stoi_safe(const std::string& str);
|
||||
int stoi_safe(const std::string& str, const int fallback = -1);
|
||||
|
||||
Reference in New Issue
Block a user