VLink  2.0.0
A high-performance communication middleware
getter_impl.h
Go to the documentation of this file.
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 getter_impl.h
26  * @brief Transport-neutral base for field-model getter (latest-value reader) implementations.
27  *
28  * @details
29  * This is an internal implementation header used by the public @c Getter template;
30  * applications should depend on @c getter.h instead. @c GetterImpl extends
31  * @c NodeImpl with the field-model receive semantics: a single value is tracked
32  * per topic, and the getter is notified whenever the matching @c SetterImpl
33  * writes a new value. Optional latency / loss tracking mirrors the interface
34  * found on @c SubscriberImpl so that both receivers expose the same diagnostics.
35  *
36  * @par ImplType
37  * The constructor stamps @c impl_type with @c kGetter, which the discovery and
38  * recording layers use to label outputs originating from this node.
39  *
40  * @par Lifecycle
41  * - Constructed by the matching @c Conf::create_getter().
42  * - @c listen() is called by the public @c Getter once the user installs a
43  * callback; @c is_listened flips to @c true on success.
44  * - @c init() / @c deinit() inherited from @c NodeImpl bring the underlying
45  * transport up and tear it down.
46  * - @c set_latency_and_lost_enabled() may be toggled before or after listening.
47  *
48  * @par Role table
49  * | Capability | Provider |
50  * | --------------------------- | ----------------------------------------- |
51  * | Wire receive callback | Subclass override of @c listen() |
52  * | Latency / loss reporting | Subclass override of the optional getters |
53  * | Listen state flag | @c is_listened (set by @c Getter) |
54  *
55  * @par Internal API contract
56  * | Method | Default | Subclass duty |
57  * | ------------------------------------- | ----------------------------- | ----------------------- |
58  * | @c listen(MsgCallback&&) | Pure virtual | Bind transport callback |
59  * | @c set_latency_and_lost_enabled(bool) | No-op | Toggle tracking |
60  * | @c is_latency_and_lost_enabled() const| Returns @c false | Report tracking state |
61  * | @c get_latency() const | Returns @c 0 | Latest measured latency |
62  * | @c get_lost() const | Returns zero @c SampleLostInfo| Cumulative loss stats |
63  */
64 
65 #pragma once
66 
67 #include "./node_impl.h"
68 
69 namespace vlink {
70 
71 /**
72  * @class GetterImpl
73  * @brief Field-model receive base class shared by every transport-specific getter.
74  *
75  * @details
76  * Provides safe defaults for the optional latency / loss instrumentation so
77  * that backends without those signals can be plugged in without supplying
78  * empty overrides. Concrete backends override @c listen() to wire the
79  * transport receive path to the supplied @c MsgCallback.
80  */
82  public:
83  /**
84  * @brief Releases backend resources.
85  */
86  ~GetterImpl() override;
87 
88  /**
89  * @brief Installs the latest-value notification callback.
90  *
91  * @details
92  * Pure virtual. Backends bind @p callback to the transport receive path so
93  * the public @c Getter sees every value written by the matching @c Setter.
94  * The owning @c Getter sets @c is_listened to @c true once this method
95  * succeeds.
96  *
97  * @param callback Callable @c void(const Bytes&) invoked on each update.
98  * @return @c true on successful registration; @c false on error.
99  */
100  virtual bool listen(MsgCallback&& callback) = 0;
101 
102  /**
103  * @brief Enables or disables per-update latency and loss tracking.
104  *
105  * @details
106  * Default no-op. Backends that maintain timestamps and sequence numbers
107  * override the method to activate the instrumentation.
108  *
109  * @param enable @c true to start tracking; @c false to stop.
110  */
111  virtual void set_latency_and_lost_enabled(bool enable);
112 
113  /**
114  * @brief Reports whether tracking is currently enabled.
115  *
116  * @return @c true when the backend is collecting latency / loss data.
117  */
118  [[nodiscard]] virtual bool is_latency_and_lost_enabled() const;
119 
120  /**
121  * @brief Returns the most recent end-to-end latency measurement in nanoseconds.
122  *
123  * @details
124  * Only meaningful when @c is_latency_and_lost_enabled() returns @c true.
125  *
126  * @return Latency in nanoseconds; @c 0 if tracking is disabled or unsupported.
127  */
128  [[nodiscard]] virtual int64_t get_latency() const;
129 
130  /**
131  * @brief Returns cumulative delivered / lost counts for field updates.
132  *
133  * @return @c SampleLostInfo with @c total and @c lost counters.
134  */
135  [[nodiscard]] virtual SampleLostInfo get_lost() const;
136 
137  bool is_listened{false}; ///< @c true once @c listen() has been registered successfully.
138 
139  protected:
140  /**
141  * @brief Stamps the node as @c kGetter.
142  */
144 
145  private:
147 };
148 
149 } // namespace vlink
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Foundational base classes shared by every transport-backed VLink node.