moddef-cpp
Idiomatic C++ runtime for ModDef
Loading...
Searching...
No Matches
device.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
12#ifndef MODDEF_DEVICE_HPP
13#define MODDEF_DEVICE_HPP
14
15#include <cstdint>
16#include <span>
17#include <string_view>
18
19#include "moddef/document.hpp"
20#include "moddef/error.hpp"
21#include "moddef/transport.hpp"
22#include "moddef/value.hpp"
23
24extern "C" {
25#include "moddef/device.h"
26}
27
28namespace moddef {
29
32class Device {
33 public:
36 static Result<Device> open(const Document& doc, std::string_view device_id,
37 Transport& transport) {
38 Device d;
39 d.ct_ = detail::bind(transport);
40 if (auto e = md_dev_init(&d.dev_, doc.c_doc(), to_c(device_id), &d.ct_); e != MD_OK)
41 return std::unexpected(from_c(e));
42 return d;
43 }
44
46 Result<Value> read(std::string_view point_id) {
47 md_value_t v{};
48 if (auto e = md_dev_read(&dev_, to_c(point_id), &v); e != MD_OK)
49 return std::unexpected(from_c(e));
50 return Value{v};
51 }
52
54 Result<std::string_view> read_string(std::string_view point_id, std::span<char> buf) {
55 std::size_t len = 0;
56 if (auto e = md_dev_read_string(&dev_, to_c(point_id), buf.data(), buf.size(), &len);
57 e != MD_OK)
58 return std::unexpected(from_c(e));
59 return std::string_view{buf.data(), len};
60 }
61
63 Result<void> write_f64(std::string_view point_id, double value) {
64 return check(md_dev_write_f64(&dev_, to_c(point_id), value));
65 }
66 Result<void> write_i64(std::string_view point_id, std::int64_t value) {
67 return check(md_dev_write_i64(&dev_, to_c(point_id), value));
68 }
69 Result<void> write_bool(std::string_view point_id, bool on) {
70 md_value_t v = md_value_bool(on);
71 return check(md_dev_write(&dev_, to_c(point_id), &v));
72 }
73
74 // Movable: md_dev_t stores a pointer to our inline transport table, so a
75 // move must re-point it at the moved-to member.
76 Device(Device&& o) noexcept : dev_(o.dev_), ct_(o.ct_) { dev_.t = &ct_; }
77 Device& operator=(Device&& o) noexcept {
78 dev_ = o.dev_;
79 ct_ = o.ct_;
80 dev_.t = &ct_;
81 return *this;
82 }
83 Device(const Device&) = delete;
84 Device& operator=(const Device&) = delete;
85
86 private:
87 Device() = default;
88
89 md_dev_t dev_{};
90 md_transport_t ct_{};
91};
92
93} // namespace moddef
94
95#endif // MODDEF_DEVICE_HPP
A device bound to a transport: read and write its points by id.
Definition device.hpp:32
Result< void > write_f64(std::string_view point_id, double value)
Encode and write; validates access mode and §11.4 constraints first.
Definition device.hpp:63
static Result< Device > open(const Document &doc, std::string_view device_id, Transport &transport)
Bind transport to the named profile in doc (empty id = first).
Definition device.hpp:36
Device(const Device &)=delete
Device & operator=(Device &&o) noexcept
Definition device.hpp:77
Result< std::string_view > read_string(std::string_view point_id, std::span< char > buf)
Read a string point (§15) into buf; returns a view into buf.
Definition device.hpp:54
Result< void > write_i64(std::string_view point_id, std::int64_t value)
Definition device.hpp:66
Device(Device &&o) noexcept
Definition device.hpp:76
Result< Value > read(std::string_view point_id)
Read and decode a point (numeric / bool / flags / fields / datetime).
Definition device.hpp:46
Result< void > write_bool(std::string_view point_id, bool on)
Definition device.hpp:69
Device & operator=(const Device &)=delete
A parsed ModDef document.
Definition document.hpp:33
const md_doc_t * c_doc() const noexcept
Definition document.hpp:54
Blocking register-level transport.
Definition transport.hpp:28
A decoded point value: a typed view over the C core's md_value_t.
Definition value.hpp:38
A binary .moddef document (spec §4, §27), parsed in place.
Error type and Result<T> (spec §26.3/§26.4, §32).
md_transport_t bind(Transport &t) noexcept
Definition transport.hpp:68
Definition device.hpp:28
std::expected< T, Error > Result
The canonical result type across the API; Result<void> for actions.
Definition error.hpp:53
Result< void > check(md_err_t e)
Definition error.hpp:62
md_err_t to_c(Error e) noexcept
Definition error.hpp:47
Error from_c(md_err_t e) noexcept
Definition error.hpp:46
Transport (spec §32.3).
Decoded value (spec §8, §13).