This commit is contained in:
2026-09-15 17:24:55 +03:00
parent d0502e0df0
commit eb4fa72d4b
2 changed files with 79 additions and 3 deletions

View File

@@ -2,15 +2,58 @@
#include <adclib/adclib_device1.h>
using namespace adc;
template <typename ID_T, typename VT>
void add_attr_mark(ID_T id, VT const& val, std::string_view acc = "RW")
{
auto tp = snplib::snplib_get_type_name<VT>();
std::println("add {}-attr with ID='{}' of type '{}'", acc, id, tp);
}
template <typename DEV_T, typename VT>
void set_attr(DEV_T& dev, typename DEV_T::attr_id_t id, VT const& val)
{
auto tp = snplib::snplib_get_type_name<VT>();
std::print("set '{}'-attr value to {} of type '{}':\t", id, val, tp);
auto err = dev[id] = val;
if (err) {
std::println("ERROR = {}", err.message());
} else {
std::println("OK");
}
}
template <typename DEV_T, typename VT>
void get_attr(DEV_T& dev, typename DEV_T::attr_id_t id, adc_result_t<VT>& val)
{
std::string tp;
[&tp]<typename T>(T v) { tp = snplib::snplib_get_type_name<T>(); }(val.value());
std::print("get '{}'-attr value as one of type '{}':\t", id, tp);
val = dev[id];
if (val) {
std::println("OK (value = {})", val.value());
} else {
std::println("ERROR = {}", val.error().message());
}
}
int main()
{
std::string id1 = "1", id2 = "2", id3 = "str";
std::string id1 = "1", id2 = "2", id3 = "str", id4 = "fsp";
int i = 10;
double d = 8.1;
std::string s{"dwedjkwlk"};
std::filesystem::path pt{"/etc/ewk/dwkej.wed"};
AdcGenericDevice<> dev("DEV#1");
@@ -58,6 +101,15 @@ int main()
return {};
});
// std::println("add RW-attr with id='{}' (val={})", id4, pt.native());
add_attr_mark(id4, pt);
dev.addFSPathAttr(
id4, [&pt]() -> adc_result_t<std::filesystem::path> { return pt; },
[&pt](std::filesystem::path const& p) -> adc_error_t {
pt = p;
return {};
});
std::print("get '{}'-attr value as native int:\t", id1);
adc_result_t<int> ri = dev[id1];
if (ri) {
@@ -133,5 +185,27 @@ int main()
}
s = "/kgtr/grth/hth34.rth";
std::vector<char> vch{std::from_range_t{}, s};
std::print("set '{}'-attr value to {} as std::vector<char>:\t", id4, vch);
err = dev[id4] = vch;
if (err) {
std::println("cannot set dev[{}] as std::vector<char>: {}", id4, err.message());
} else {
std::println("OK");
}
set_attr(dev, id4, vch);
std::print("get '{}'-attr value as string:\t", id4);
rs = dev[id4];
if (rs) {
std::println("dev[{}] = {}", id3, rs.value());
} else {
std::println("cannot get dev[{}] as string: {}", id4, rs.error().message());
}
get_attr(dev, id4, rs);
return 0;
}