VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
setter.h
浏览该文件的文档.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file setter.h
26 * @brief Type-safe field-model writer for VLink topics.
27 *
28 * @details
29 * @c Setter<ValueT, SecT> is the write side of the VLink field model.
30 * Each call to @c set() serialises the value and writes it to the transport
31 * as the new "latest value" for the topic. All @c Getter nodes on the same
32 * URL will be notified of the update.
33 *
34 * @par Field Model Overview
35 * @code
36 * Setter<T> Transport Back-end Getter<T>
37 * | | |
38 * |-- set(value) ----------> | |
39 * | serialize(value) | |
40 * | |-- latest-value delivery ----> |
41 * | | (overwrites previous) |--> callback(value)
42 * | | |--> get() => value
43 * @endcode
44 *
45 * @par Key Differences: Setter vs Publisher
46 * | Feature | @c Setter<T> | @c Publisher<T> |
47 * | ------------------------ | ------------------------------------ | ------------------------------ |
48 * | Transport role | @c kSetter | @c kPublisher |
49 * | Value retention | Last value re-sent to late getters | No re-send to late subs |
50 * | Sync callback on connect | Yes -- @c sync() callback | No |
51 * | Cross-transport use | As publisher: @c mark_as_publisher() | As setter: @c mark_as_setter() |
52 *
53 * @par Sync on Late Connect
54 * When a @c Getter connects after the @c Setter has already written, the transport
55 * fires the @c sync() callback registered internally. The @c Setter re-sends
56 * its cached value so the late @c Getter receives the current state immediately:
57 * @code
58 * Setter<int> s("shm://my_field");
59 * s.set(42); // Getter joins later and immediately receives 42
60 * @endcode
61 * @par Usage
62 * @code
63 * Setter<MyStruct> setter("dds://vehicle/gear");
64 * setter.set(MyStruct{3, "Drive"}); // write a new field value
65 * @endcode
66 *
67 * @tparam ValueT Value type. Must satisfy @c Serializer::is_supported().
68 * @tparam SecT Security mode; defaults to @c SecurityType::kWithoutSecurity.
69 */
70
71#pragma once
72
73#include <memory>
74#include <mutex>
75#include <optional>
76#include <string>
77
78#include "./impl/setter_impl.h"
79#include "./node.h"
80
81namespace vlink {
82
83/**
84 * @class Setter
85 * @brief Type-safe field writer for the VLink field communication model.
86 *
87 * @tparam ValueT Value type to write.
88 * @tparam SecT Security mode.
89 */
90template <typename ValueT, SecurityType SecT = SecurityType::kWithoutSecurity>
91class Setter : public Node<SetterImpl, SecT> {
92 public:
93 /** @brief Unique-pointer alias. */
94 using UniquePtr = std::unique_ptr<Setter<ValueT, SecT>>;
95
96 /** @brief Shared-pointer alias. */
97 using SharedPtr = std::shared_ptr<Setter<ValueT, SecT>>;
98
99 /** @brief Node role identifier (@c kSetter). */
100 static constexpr ImplType kImplType = kSetter;
101
102 /** @brief Serializer type resolved at compile time from @c ValueT. */
104
105 static_assert(Serializer::is_supported(kValueType), "<ValueT> is not a supported Serializer type.");
106
107 /**
108 * @brief Creates a @c Setter on the heap wrapped in a @c unique_ptr.
109 *
110 * @param url_str Field URL string (e.g. @c "shm://vehicle/gear").
111 * @param type @c kWithInit to call @c init() immediately (default).
112 * @return @c UniquePtr owning the new setter.
113 */
114 [[nodiscard]] static UniquePtr create_unique(const std::string& url_str, InitType type = InitType::kWithInit);
115
116 /**
117 * @brief Creates a @c Setter on the heap wrapped in a @c shared_ptr.
118 *
119 * @param url_str Field URL string.
120 * @param type @c kWithInit to call @c init() immediately (default).
121 * @return @c SharedPtr owning the new setter.
122 */
123 [[nodiscard]] static SharedPtr create_shared(const std::string& url_str, InitType type = InitType::kWithInit);
124
125 /**
126 * @brief Constructs a setter from a typed transport configuration object.
127 *
128 * @details
129 * Accepts any @c Conf-derived configuration. After @c init(), registers an
130 * internal @c sync() callback with the transport so that when a late @c Getter
131 * connects the cached value is re-sent automatically.
132 *
133 * @tparam ConfT @c Conf-derived configuration type.
134 * @param conf Populated configuration object.
135 * @param type @c kWithInit to call @c init() immediately (default).
136 */
137 // NOLINTNEXTLINE(modernize-use-constraints)
138 template <typename ConfT, typename = std::enable_if_t<std::is_base_of_v<Conf, ConfT>>>
139 explicit Setter(const ConfT& conf, InitType type = InitType::kWithInit);
140
141 /**
142 * @brief Constructs a setter from a URL string.
143 *
144 * @param url_str Field URL (e.g. @c "dds://vehicle/gear").
145 * @param type @c kWithInit to call @c init() immediately (default).
146 */
147 explicit Setter(const std::string& url_str, InitType type = InitType::kWithInit);
148
149 /**
150 * @brief Initializes the setter transport and registers the late-getter sync callback.
151 *
152 * @details
153 * Calls the base @c Node initialization, then installs a transport @c sync()
154 * callback that re-sends the cached latest value when a new @c Getter joins
155 * after this setter has already been written to.
156 *
157 * @return @c true on success, @c false if transport initialization fails.
158 */
159 bool init() override;
160
161 /**
162 * @brief Deinitializes the setter and releases the underlying transport resources.
163 *
164 * @details
165 * Delegates to the base @c Node deinitialization path. After deinitialization,
166 * the setter stops participating in transport sync and write operations until
167 * it is initialized again.
168 *
169 * @return @c true on success, @c false if the underlying transport reports failure.
170 */
171 bool deinit() override;
172
173 /**
174 * @brief Writes a new field value and notifies all connected @c Getter nodes.
175 *
176 * @details
177 * Caches @p value internally (guarded by mutex), then serializes and writes
178 * it to the transport. If security is enabled the serialized bytes are
179 * encrypted before transmission. The cached value is automatically re-sent
180 * when a late @c Getter connects.
181 *
182 * @param value The new field value to write.
183 */
184 void set(const ValueT& value);
185
186 /**
187 * @brief Changes this setter's role to @c kPublisher (event-emitter).
188 *
189 * @details
190 * Updates @c impl_->impl_type from @c kSetter to @c kPublisher so that
191 * event-model transport semantics are applied (no last-value retention).
192 * If called after @c init(), the extension is automatically reinitialised.
193 * Used when a @c Setter should behave as a plain publisher on transports
194 * that do not natively distinguish the two roles.
195 */
196 void mark_as_publisher();
197
198 private:
199 void write(const ValueT& value);
200
201 void write_bytes(const Bytes& data);
202
203 std::optional<ValueT> value_;
204 std::mutex mtx_;
205};
206
207/**
208 * @class SecuritySetter
209 * @brief Convenience alias for @c Setter with message security enabled.
210 *
211 * @details
212 * Equivalent to @c Setter<ValueT, SecurityType::kWithSecurity>. Encrypts
213 * each outgoing value using the configured security key or callbacks.
214 *
215 * @tparam ValueT Value type to write.
216 */
217template <typename ValueT>
218class SecuritySetter : public Setter<ValueT, SecurityType::kWithSecurity> {
219 public:
221};
222
223} // namespace vlink
224
Base CRTP template for all VLink communication nodes.
Abstract base class for all transport-specific setter (field writer) implementations.