WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
MultiSubscriber.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <span>
8#include <string_view>
9
11#include "ntcore_cpp.h"
12
13namespace nt {
14
15/**
16 * Subscribe to multiple topics based on one or more topic name prefixes. Can be
17 * used in combination with NetworkTableListenerPoller to listen for value
18 * changes across all matching topics.
19 */
20class MultiSubscriber final {
21 public:
22 MultiSubscriber() = default;
23
24 /**
25 * Create a multiple subscriber.
26 *
27 * @param inst instance
28 * @param prefixes topic name prefixes
29 * @param options subscriber options
30 */
32 std::span<const std::string_view> prefixes,
33 const PubSubOptions& options = kDefaultPubSubOptions)
34 : m_handle{::nt::SubscribeMultiple(inst.GetHandle(), prefixes, options)} {
35 }
36
39
40 MultiSubscriber(MultiSubscriber&& rhs) : m_handle{rhs.m_handle} {
41 rhs.m_handle = 0;
42 }
43
45 if (m_handle != 0) {
47 }
48 m_handle = rhs.m_handle;
49 rhs.m_handle = 0;
50 return *this;
51 }
52
54 if (m_handle != 0) {
56 }
57 }
58
59 /**
60 * Determines if the native handle is valid.
61 *
62 * @return True if the native handle is valid, false otherwise.
63 */
64 explicit operator bool() const { return m_handle != 0; }
65
66 /**
67 * Gets the native handle.
68 *
69 * @return Handle
70 */
71 NT_MultiSubscriber GetHandle() const { return m_handle; }
72
73 private:
74 NT_MultiSubscriber m_handle{0};
75};
76
77} // namespace nt
Subscribe to multiple topics based on one or more topic name prefixes.
Definition MultiSubscriber.h:20
~MultiSubscriber()
Definition MultiSubscriber.h:53
MultiSubscriber & operator=(MultiSubscriber &&rhs)
Definition MultiSubscriber.h:44
NT_MultiSubscriber GetHandle() const
Gets the native handle.
Definition MultiSubscriber.h:71
MultiSubscriber(MultiSubscriber &&rhs)
Definition MultiSubscriber.h:40
MultiSubscriber(const MultiSubscriber &)=delete
MultiSubscriber(NetworkTableInstance inst, std::span< const std::string_view > prefixes, const PubSubOptions &options=kDefaultPubSubOptions)
Create a multiple subscriber.
Definition MultiSubscriber.h:31
MultiSubscriber()=default
MultiSubscriber & operator=(const MultiSubscriber &)=delete
NetworkTables Instance.
Definition NetworkTableInstance.h:68
NT_MultiSubscriber SubscribeMultiple(NT_Inst inst, std::span< const std::string_view > prefixes, const PubSubOptions &options=kDefaultPubSubOptions)
Subscribes to multiple topics based on one or more topic name prefixes.
void UnsubscribeMultiple(NT_MultiSubscriber sub)
Unsubscribes a multi-subscriber.
NT_Handle NT_MultiSubscriber
Definition ntcore_c.h:41
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition ntcore_cpp.h:390
NetworkTables (ntcore) namespace.
Definition ntcore_cpp.h:36
NetworkTables publish/subscribe options.
Definition ntcore_cpp.h:305