[ntcore] Merge .inc files into headers (#7210)

This commit is contained in:
Peter Johnson
2024-10-14 22:42:58 -07:00
committed by GitHub
parent ee281ea448
commit 0bada2e102
41 changed files with 1720 additions and 3383 deletions

View File

@@ -8,6 +8,7 @@
#include <string>
#include <wpi/FastQueue.h>
#include <wpi/Logger.h>
#include <wpi/mutex.h>
#include "Message.h"
@@ -40,20 +41,81 @@ class ClientMessageQueueImpl final : public ClientMessageHandler,
bool empty() const { return m_queue.empty(); }
// ClientMessageQueue - calls to these read the queue
std::span<ClientMessage> ReadQueue(std::span<ClientMessage> out) final;
void ClearQueue() final;
std::span<ClientMessage> ReadQueue(std::span<ClientMessage> out) final {
std::scoped_lock lock{m_mutex};
size_t count = 0;
for (auto&& msg : out) {
if (!m_queue.try_dequeue(msg)) {
break;
}
if constexpr (MaxValueSize != 0) {
if (auto* val = std::get_if<ClientValueMsg>(&msg.contents)) {
m_valueSize.size -= sizeof(ClientMessage) + val->value.size();
m_valueSize.errored = false;
}
}
++count;
}
return out.subspan(0, count);
}
void ClearQueue() final {
std::scoped_lock lock{m_mutex};
ClientMessage msg;
while (m_queue.try_dequeue(msg)) {
}
if constexpr (MaxValueSize != 0) {
m_valueSize.size = 0;
m_valueSize.errored = false;
}
}
// ClientMessageHandler - calls to these append to the queue
void ClientPublish(int pubuid, std::string_view name,
std::string_view typeStr, const wpi::json& properties,
const PubSubOptionsImpl& options) final;
void ClientUnpublish(int pubuid) final;
const PubSubOptionsImpl& options) final {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{PublishMsg{
pubuid, std::string{name}, std::string{typeStr}, properties, options}});
}
void ClientUnpublish(int pubuid) final {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{UnpublishMsg{pubuid}});
}
void ClientSetProperties(std::string_view name,
const wpi::json& update) final;
const wpi::json& update) final {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{SetPropertiesMsg{std::string{name}, update}});
}
void ClientSubscribe(int subuid, std::span<const std::string> topicNames,
const PubSubOptionsImpl& options) final;
void ClientUnsubscribe(int subuid) final;
void ClientSetValue(int pubuid, const Value& value) final;
const PubSubOptionsImpl& options) final {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{
SubscribeMsg{subuid, {topicNames.begin(), topicNames.end()}, options}});
}
void ClientUnsubscribe(int subuid) final {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{UnsubscribeMsg{subuid}});
}
void ClientSetValue(int pubuid, const Value& value) final {
std::scoped_lock lock{m_mutex};
if constexpr (MaxValueSize != 0) {
m_valueSize.size += sizeof(ClientMessage) + value.size();
if (m_valueSize.size > MaxValueSize) {
if (!m_valueSize.errored) {
WPI_ERROR(m_logger, "NT: dropping value set due to memory limits");
m_valueSize.errored = true;
}
return; // avoid potential out of memory
}
}
m_queue.enqueue(ClientMessage{ClientValueMsg{pubuid, value}});
}
private:
wpi::FastQueue<ClientMessage, kBlockSize> m_queue{kBlockSize - 1};
@@ -83,5 +145,3 @@ using LocalClientMessageQueue =
using NetworkIncomingClientQueue = detail::ClientMessageQueueImpl<0, false>;
} // namespace nt::net
#include "ClientMessageQueue.inc"

View File

@@ -1,106 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <span>
#include <string>
#include <wpi/Logger.h>
#include "ClientMessageQueue.h"
namespace nt::net::detail {
template <size_t MaxValueSize, bool IsMutexed>
inline void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientPublish(
int pubuid, std::string_view name, std::string_view typeStr,
const wpi::json& properties, const PubSubOptionsImpl& options) {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{PublishMsg{
pubuid, std::string{name}, std::string{typeStr}, properties, options}});
}
template <size_t MaxValueSize, bool IsMutexed>
inline void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientUnpublish(
int pubuid) {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{UnpublishMsg{pubuid}});
}
template <size_t MaxValueSize, bool IsMutexed>
inline void
ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientSetProperties(
std::string_view name, const wpi::json& update) {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{SetPropertiesMsg{std::string{name}, update}});
}
template <size_t MaxValueSize, bool IsMutexed>
inline void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientSubscribe(
int subuid, std::span<const std::string> topicNames,
const PubSubOptionsImpl& options) {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{
SubscribeMsg{subuid, {topicNames.begin(), topicNames.end()}, options}});
}
template <size_t MaxValueSize, bool IsMutexed>
inline void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientUnsubscribe(
int subuid) {
std::scoped_lock lock{m_mutex};
m_queue.enqueue(ClientMessage{UnsubscribeMsg{subuid}});
}
template <size_t MaxValueSize, bool IsMutexed>
std::span<ClientMessage>
ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ReadQueue(
std::span<ClientMessage> out) {
std::scoped_lock lock{m_mutex};
size_t count = 0;
for (auto&& msg : out) {
if (!m_queue.try_dequeue(msg)) {
break;
}
if constexpr (MaxValueSize != 0) {
if (auto* val = std::get_if<ClientValueMsg>(&msg.contents)) {
m_valueSize.size -= sizeof(ClientMessage) + val->value.size();
m_valueSize.errored = false;
}
}
++count;
}
return out.subspan(0, count);
}
template <size_t MaxValueSize, bool IsMutexed>
void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClearQueue() {
std::scoped_lock lock{m_mutex};
ClientMessage msg;
while (m_queue.try_dequeue(msg)) {
}
if constexpr (MaxValueSize != 0) {
m_valueSize.size = 0;
m_valueSize.errored = false;
}
}
template <size_t MaxValueSize, bool IsMutexed>
void ClientMessageQueueImpl<MaxValueSize, IsMutexed>::ClientSetValue(
int pubuid, const Value& value) {
std::scoped_lock lock{m_mutex};
if constexpr (MaxValueSize != 0) {
m_valueSize.size += sizeof(ClientMessage) + value.size();
if (m_valueSize.size > MaxValueSize) {
if (!m_valueSize.errored) {
WPI_ERROR(m_logger, "NT: dropping value set due to memory limits");
m_valueSize.errored = true;
}
return; // avoid potential out of memory
}
}
m_queue.enqueue(ClientMessage{ClientValueMsg{pubuid, value}});
}
} // namespace nt::net::detail

View File

@@ -60,3 +60,7 @@ GenericEntry Topic::GetGenericEntry(std::string_view typeString,
return GenericEntry{::nt::GetEntry(
m_handle, ::nt::GetTypeFromString(typeString), typeString, options)};
}
void Publisher::anchor() {}
void Subscriber::anchor() {}

View File

@@ -9,10 +9,10 @@
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "networktables/Topic.h"
#include "ntcore_cpp.h"
namespace nt {
@@ -36,7 +36,7 @@ class GenericSubscriber : public Subscriber {
*
* @param handle Native handle
*/
explicit GenericSubscriber(NT_Subscriber handle);
explicit GenericSubscriber(NT_Subscriber handle) : Subscriber{handle} {}
/**
* Get the last published value.
@@ -44,7 +44,7 @@ class GenericSubscriber : public Subscriber {
*
* @return value
*/
ValueType Get() const;
ValueType Get() const { return ::nt::GetEntryValue(m_subHandle); }
/**
* Gets the entry's value as a boolean. If the entry does not exist or is of
@@ -53,7 +53,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
bool GetBoolean(bool defaultValue) const;
bool GetBoolean(bool defaultValue) const {
return ::nt::GetBoolean(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a integer. If the entry does not exist or is of
@@ -62,7 +64,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
int64_t GetInteger(int64_t defaultValue) const;
int64_t GetInteger(int64_t defaultValue) const {
return ::nt::GetInteger(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a float. If the entry does not exist or is of
@@ -71,7 +75,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
float GetFloat(float defaultValue) const;
float GetFloat(float defaultValue) const {
return ::nt::GetFloat(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a double. If the entry does not exist or is of
@@ -80,7 +86,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
double GetDouble(double defaultValue) const;
double GetDouble(double defaultValue) const {
return ::nt::GetDouble(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a string. If the entry does not exist or is of
@@ -89,7 +97,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
std::string GetString(std::string_view defaultValue) const;
std::string GetString(std::string_view defaultValue) const {
return ::nt::GetString(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a raw. If the entry does not exist or is of
@@ -98,7 +108,9 @@ class GenericSubscriber : public Subscriber {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const;
std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const {
return ::nt::GetRaw(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a boolean array. If the entry does not exist
@@ -114,7 +126,9 @@ class GenericSubscriber : public Subscriber {
* because std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const;
std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const {
return ::nt::GetBooleanArray(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a integer array. If the entry does not exist
@@ -127,7 +141,9 @@ class GenericSubscriber : public Subscriber {
* concern, use GetValue() instead.
*/
std::vector<int64_t> GetIntegerArray(
std::span<const int64_t> defaultValue) const;
std::span<const int64_t> defaultValue) const {
return ::nt::GetIntegerArray(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a float array. If the entry does not exist
@@ -139,7 +155,9 @@ class GenericSubscriber : public Subscriber {
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*/
std::vector<float> GetFloatArray(std::span<const float> defaultValue) const;
std::vector<float> GetFloatArray(std::span<const float> defaultValue) const {
return ::nt::GetFloatArray(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a double array. If the entry does not exist
@@ -152,7 +170,9 @@ class GenericSubscriber : public Subscriber {
* concern, use GetValue() instead.
*/
std::vector<double> GetDoubleArray(
std::span<const double> defaultValue) const;
std::span<const double> defaultValue) const {
return ::nt::GetDoubleArray(m_subHandle, defaultValue);
}
/**
* Gets the entry's value as a string array. If the entry does not exist
@@ -165,7 +185,9 @@ class GenericSubscriber : public Subscriber {
* concern, use GetValue() instead.
*/
std::vector<std::string> GetStringArray(
std::span<const std::string> defaultValue) const;
std::span<const std::string> defaultValue) const {
return ::nt::GetStringArray(m_subHandle, defaultValue);
}
/**
* Get an array of all value changes since the last call to ReadQueue.
@@ -177,14 +199,18 @@ class GenericSubscriber : public Subscriber {
* @return Array of timestamped values; empty array if no new changes have
* been published since the previous call.
*/
std::vector<TimestampedValueType> ReadQueue();
std::vector<TimestampedValueType> ReadQueue() {
return ::nt::ReadQueueValue(m_subHandle);
}
/**
* Get the corresponding topic.
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
};
/**
@@ -205,14 +231,14 @@ class GenericPublisher : public Publisher {
*
* @param handle Native handle
*/
explicit GenericPublisher(NT_Publisher handle);
explicit GenericPublisher(NT_Publisher handle) : Publisher{handle} {}
/**
* Publish a new value.
*
* @param value value to publish
*/
void Set(ParamType value);
void Set(ParamType value) { ::nt::SetEntryValue(m_pubHandle, value); }
/**
* Sets the entry's value.
@@ -221,7 +247,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBoolean(bool value, int64_t time = 0);
bool SetBoolean(bool value, int64_t time = 0) {
return nt::SetBoolean(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -230,7 +258,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetInteger(int64_t value, int64_t time = 0);
bool SetInteger(int64_t value, int64_t time = 0) {
return nt::SetInteger(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -239,7 +269,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetFloat(float value, int64_t time = 0);
bool SetFloat(float value, int64_t time = 0) {
return nt::SetFloat(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -248,7 +280,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetDouble(double value, int64_t time = 0);
bool SetDouble(double value, int64_t time = 0) {
return nt::SetDouble(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -257,7 +291,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetString(std::string_view value, int64_t time = 0);
bool SetString(std::string_view value, int64_t time = 0) {
return nt::SetString(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -266,7 +302,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetRaw(std::span<const uint8_t> value, int64_t time = 0);
bool SetRaw(std::span<const uint8_t> value, int64_t time = 0) {
return nt::SetRaw(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -275,7 +313,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::span<const bool> value, int64_t time = 0);
bool SetBooleanArray(std::span<const bool> value, int64_t time = 0) {
return SetEntryValue(m_pubHandle, Value::MakeBooleanArray(value, time));
}
/**
* Sets the entry's value.
@@ -284,7 +324,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::span<const int> value, int64_t time = 0);
bool SetBooleanArray(std::span<const int> value, int64_t time = 0) {
return nt::SetBooleanArray(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -293,7 +335,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0);
bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0) {
return nt::SetIntegerArray(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -302,7 +346,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetFloatArray(std::span<const float> value, int64_t time = 0);
bool SetFloatArray(std::span<const float> value, int64_t time = 0) {
return nt::SetFloatArray(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -311,7 +357,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetDoubleArray(std::span<const double> value, int64_t time = 0);
bool SetDoubleArray(std::span<const double> value, int64_t time = 0) {
return nt::SetDoubleArray(m_pubHandle, value, time);
}
/**
* Sets the entry's value.
@@ -320,7 +368,9 @@ class GenericPublisher : public Publisher {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetStringArray(std::span<const std::string> value, int64_t time = 0);
bool SetStringArray(std::span<const std::string> value, int64_t time = 0) {
return nt::SetStringArray(m_pubHandle, value, time);
}
/**
* Publish a default value.
@@ -329,7 +379,9 @@ class GenericPublisher : public Publisher {
*
* @param value value
*/
void SetDefault(ParamType value);
void SetDefault(ParamType value) {
::nt::SetDefaultEntryValue(m_pubHandle, value);
}
/**
* Sets the entry's value if it does not exist.
@@ -337,7 +389,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultBoolean(bool defaultValue);
bool SetDefaultBoolean(bool defaultValue) {
return nt::SetDefaultBoolean(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -345,7 +399,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultInteger(int64_t defaultValue);
bool SetDefaultInteger(int64_t defaultValue) {
return nt::SetDefaultInteger(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -353,7 +409,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultFloat(float defaultValue);
bool SetDefaultFloat(float defaultValue) {
return nt::SetDefaultFloat(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -361,7 +419,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultDouble(double defaultValue);
bool SetDefaultDouble(double defaultValue) {
return nt::SetDefaultDouble(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -369,7 +429,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultString(std::string_view defaultValue);
bool SetDefaultString(std::string_view defaultValue) {
return nt::SetDefaultString(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -377,7 +439,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultRaw(std::span<const uint8_t> defaultValue);
bool SetDefaultRaw(std::span<const uint8_t> defaultValue) {
return nt::SetDefaultRaw(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -385,7 +449,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultBooleanArray(std::span<const int> defaultValue);
bool SetDefaultBooleanArray(std::span<const int> defaultValue) {
return nt::SetDefaultBooleanArray(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -393,7 +459,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue);
bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue) {
return nt::SetDefaultIntegerArray(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -401,7 +469,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultFloatArray(std::span<const float> defaultValue);
bool SetDefaultFloatArray(std::span<const float> defaultValue) {
return nt::SetDefaultFloatArray(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -409,7 +479,9 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultDoubleArray(std::span<const double> defaultValue);
bool SetDefaultDoubleArray(std::span<const double> defaultValue) {
return nt::SetDefaultDoubleArray(m_pubHandle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -417,14 +489,18 @@ class GenericPublisher : public Publisher {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultStringArray(std::span<const std::string> defaultValue);
bool SetDefaultStringArray(std::span<const std::string> defaultValue) {
return nt::SetDefaultStringArray(m_pubHandle, defaultValue);
}
/**
* Get the corresponding topic.
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_pubHandle)};
}
};
/**
@@ -449,7 +525,8 @@ class GenericEntry final : public GenericSubscriber, public GenericPublisher {
*
* @param handle Native handle
*/
explicit GenericEntry(NT_Entry handle);
explicit GenericEntry(NT_Entry handle)
: GenericSubscriber{handle}, GenericPublisher{handle} {}
/**
* Determines if the native handle is valid.
@@ -470,14 +547,14 @@ class GenericEntry final : public GenericSubscriber, public GenericPublisher {
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
/**
* Stops publishing the entry if it's published.
*/
void Unpublish();
void Unpublish() { ::nt::Unpublish(m_pubHandle); }
};
} // namespace nt
#include "networktables/GenericEntry.inc"

View File

@@ -1,214 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include "networktables/GenericEntry.h"
#include "networktables/NetworkTableType.h"
#include "ntcore_cpp.h"
namespace nt {
inline GenericSubscriber::GenericSubscriber(NT_Subscriber handle)
: Subscriber{handle} {}
inline Value GenericSubscriber::Get() const {
return ::nt::GetEntryValue(m_subHandle);
}
inline bool GenericSubscriber::GetBoolean(bool defaultValue) const {
return ::nt::GetBoolean(m_subHandle, defaultValue);
}
inline int64_t GenericSubscriber::GetInteger(int64_t defaultValue) const {
return ::nt::GetInteger(m_subHandle, defaultValue);
}
inline float GenericSubscriber::GetFloat(float defaultValue) const {
return ::nt::GetFloat(m_subHandle, defaultValue);
}
inline double GenericSubscriber::GetDouble(double defaultValue) const {
return ::nt::GetDouble(m_subHandle, defaultValue);
}
inline std::string GenericSubscriber::GetString(
std::string_view defaultValue) const {
return ::nt::GetString(m_subHandle, defaultValue);
}
inline std::vector<uint8_t> GenericSubscriber::GetRaw(
std::span<const uint8_t> defaultValue) const {
return ::nt::GetRaw(m_subHandle, defaultValue);
}
inline std::vector<int> GenericSubscriber::GetBooleanArray(
std::span<const int> defaultValue) const {
return ::nt::GetBooleanArray(m_subHandle, defaultValue);
}
inline std::vector<int64_t> GenericSubscriber::GetIntegerArray(
std::span<const int64_t> defaultValue) const {
return ::nt::GetIntegerArray(m_subHandle, defaultValue);
}
inline std::vector<float> GenericSubscriber::GetFloatArray(
std::span<const float> defaultValue) const {
return ::nt::GetFloatArray(m_subHandle, defaultValue);
}
inline std::vector<double> GenericSubscriber::GetDoubleArray(
std::span<const double> defaultValue) const {
return ::nt::GetDoubleArray(m_subHandle, defaultValue);
}
inline std::vector<std::string> GenericSubscriber::GetStringArray(
std::span<const std::string> defaultValue) const {
return ::nt::GetStringArray(m_subHandle, defaultValue);
}
inline std::vector<Value> GenericSubscriber::ReadQueue() {
return ::nt::ReadQueueValue(m_subHandle);
}
inline Topic GenericSubscriber::GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
inline GenericPublisher::GenericPublisher(NT_Publisher handle)
: Publisher{handle} {}
inline void GenericPublisher::Set(const Value& value) {
::nt::SetEntryValue(m_pubHandle, value);
}
inline bool GenericPublisher::SetBoolean(bool value, int64_t time) {
return nt::SetBoolean(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetInteger(int64_t value, int64_t time) {
return nt::SetInteger(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetFloat(float value, int64_t time) {
return nt::SetFloat(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetDouble(double value, int64_t time) {
return nt::SetDouble(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetString(std::string_view value, int64_t time) {
return nt::SetString(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetRaw(std::span<const uint8_t> value,
int64_t time) {
return nt::SetRaw(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetBooleanArray(std::span<const bool> value,
int64_t time) {
return SetEntryValue(m_pubHandle, Value::MakeBooleanArray(value, time));
}
inline bool GenericPublisher::SetBooleanArray(std::span<const int> value,
int64_t time) {
return nt::SetBooleanArray(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetIntegerArray(std::span<const int64_t> value,
int64_t time) {
return nt::SetIntegerArray(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetFloatArray(std::span<const float> value,
int64_t time) {
return nt::SetFloatArray(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetDoubleArray(std::span<const double> value,
int64_t time) {
return nt::SetDoubleArray(m_pubHandle, value, time);
}
inline bool GenericPublisher::SetStringArray(std::span<const std::string> value,
int64_t time) {
return nt::SetStringArray(m_pubHandle, value, time);
}
inline void GenericPublisher::SetDefault(const Value& value) {
::nt::SetDefaultEntryValue(m_pubHandle, value);
}
inline bool GenericPublisher::SetDefaultBoolean(bool defaultValue) {
return nt::SetDefaultBoolean(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultInteger(int64_t defaultValue) {
return nt::SetDefaultInteger(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultFloat(float defaultValue) {
return nt::SetDefaultFloat(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultDouble(double defaultValue) {
return nt::SetDefaultDouble(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultString(std::string_view defaultValue) {
return nt::SetDefaultString(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultRaw(
std::span<const uint8_t> defaultValue) {
return nt::SetDefaultRaw(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultBooleanArray(
std::span<const int> defaultValue) {
return nt::SetDefaultBooleanArray(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultIntegerArray(
std::span<const int64_t> defaultValue) {
return nt::SetDefaultIntegerArray(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultFloatArray(
std::span<const float> defaultValue) {
return nt::SetDefaultFloatArray(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultDoubleArray(
std::span<const double> defaultValue) {
return nt::SetDefaultDoubleArray(m_pubHandle, defaultValue);
}
inline bool GenericPublisher::SetDefaultStringArray(
std::span<const std::string> defaultValue) {
return nt::SetDefaultStringArray(m_pubHandle, defaultValue);
}
inline Topic GenericPublisher::GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_pubHandle)};
}
inline GenericEntry::GenericEntry(NT_Entry handle)
: GenericSubscriber{handle}, GenericPublisher{handle} {}
inline Topic GenericEntry::GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
inline void GenericEntry::Unpublish() {
::nt::Unpublish(m_pubHandle);
}
} // namespace nt

View File

@@ -30,13 +30,31 @@ class MultiSubscriber final {
*/
MultiSubscriber(NetworkTableInstance inst,
std::span<const std::string_view> prefixes,
const PubSubOptions& options = kDefaultPubSubOptions);
const PubSubOptions& options = kDefaultPubSubOptions)
: m_handle{::nt::SubscribeMultiple(inst.GetHandle(), prefixes, options)} {
}
MultiSubscriber(const MultiSubscriber&) = delete;
MultiSubscriber& operator=(const MultiSubscriber&) = delete;
MultiSubscriber(MultiSubscriber&& rhs);
MultiSubscriber& operator=(MultiSubscriber&& rhs);
~MultiSubscriber();
MultiSubscriber(MultiSubscriber&& rhs) : m_handle{rhs.m_handle} {
rhs.m_handle = 0;
}
MultiSubscriber& operator=(MultiSubscriber&& rhs) {
if (m_handle != 0) {
::nt::UnsubscribeMultiple(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
~MultiSubscriber() {
if (m_handle != 0) {
::nt::UnsubscribeMultiple(m_handle);
}
}
/**
* Determines if the native handle is valid.
@@ -57,5 +75,3 @@ class MultiSubscriber final {
};
} // namespace nt
#include "MultiSubscriber.inc"

View File

@@ -1,36 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include "networktables/MultiSubscriber.h"
namespace nt {
inline MultiSubscriber::MultiSubscriber(
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
const PubSubOptions& options)
: m_handle{::nt::SubscribeMultiple(inst.GetHandle(), prefixes, options)} {}
inline MultiSubscriber::MultiSubscriber(MultiSubscriber&& rhs)
: m_handle{rhs.m_handle} {
rhs.m_handle = 0;
}
inline MultiSubscriber& MultiSubscriber::operator=(MultiSubscriber&& rhs) {
if (m_handle != 0) {
::nt::UnsubscribeMultiple(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
inline MultiSubscriber::~MultiSubscriber() {
if (m_handle != 0) {
::nt::UnsubscribeMultiple(m_handle);
}
}
} // namespace nt

View File

@@ -6,8 +6,6 @@
#include <stdint.h>
#include <initializer_list>
#include <memory>
#include <span>
#include <string>
#include <string_view>
@@ -15,7 +13,6 @@
#include "networktables/NetworkTableType.h"
#include "networktables/NetworkTableValue.h"
#include "ntcore_c.h"
#include "ntcore_cpp.h"
namespace nt {
@@ -36,14 +33,14 @@ class NetworkTableEntry final {
/**
* Construct invalid instance.
*/
NetworkTableEntry();
NetworkTableEntry() = default;
/**
* Construct from native handle.
*
* @param handle Native handle
*/
explicit NetworkTableEntry(NT_Entry handle);
explicit NetworkTableEntry(NT_Entry handle) : m_handle{handle} {}
/**
* Determines if the native handle is valid.
@@ -57,7 +54,7 @@ class NetworkTableEntry final {
*
* @return Native handle
*/
NT_Entry GetHandle() const;
NT_Entry GetHandle() const { return m_handle; }
/**
* Gets the instance for the entry.
@@ -71,28 +68,30 @@ class NetworkTableEntry final {
*
* @return True if the entry exists, false otherwise.
*/
bool Exists() const;
bool Exists() const { return GetEntryType(m_handle) != NT_UNASSIGNED; }
/**
* Gets the name of the entry (the key).
*
* @return the entry's name
*/
std::string GetName() const;
std::string GetName() const { return GetEntryName(m_handle); }
/**
* Gets the type of the entry.
*
* @return the entry's type
*/
NetworkTableType GetType() const;
NetworkTableType GetType() const {
return static_cast<NetworkTableType>(GetEntryType(m_handle));
}
/**
* Gets the last time the entry's value was changed.
*
* @return Entry last change time
*/
int64_t GetLastChange() const;
int64_t GetLastChange() const { return GetEntryLastChange(m_handle); }
/**
* Gets the entry's value. If the entry does not exist, returns an empty
@@ -100,7 +99,7 @@ class NetworkTableEntry final {
*
* @return the entry's value or an empty value if it does not exist.
*/
Value GetValue() const;
Value GetValue() const { return GetEntryValue(m_handle); }
/**
* Gets the entry's value as a boolean. If the entry does not exist or is of
@@ -109,7 +108,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
bool GetBoolean(bool defaultValue) const;
bool GetBoolean(bool defaultValue) const {
return nt::GetBoolean(m_handle, defaultValue);
}
/**
* Gets the entry's value as a integer. If the entry does not exist or is of
@@ -118,7 +119,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
int64_t GetInteger(int64_t defaultValue) const;
int64_t GetInteger(int64_t defaultValue) const {
return nt::GetInteger(m_handle, defaultValue);
}
/**
* Gets the entry's value as a float. If the entry does not exist or is of
@@ -127,7 +130,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
float GetFloat(float defaultValue) const;
float GetFloat(float defaultValue) const {
return nt::GetFloat(m_handle, defaultValue);
}
/**
* Gets the entry's value as a double. If the entry does not exist or is of
@@ -136,7 +141,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
double GetDouble(double defaultValue) const;
double GetDouble(double defaultValue) const {
return nt::GetDouble(m_handle, defaultValue);
}
/**
* Gets the entry's value as a string. If the entry does not exist or is of
@@ -145,7 +152,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
std::string GetString(std::string_view defaultValue) const;
std::string GetString(std::string_view defaultValue) const {
return nt::GetString(m_handle, defaultValue);
}
/**
* Gets the entry's value as a raw. If the entry does not exist or is of
@@ -154,7 +163,9 @@ class NetworkTableEntry final {
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*/
std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const;
std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const {
return nt::GetRaw(m_handle, defaultValue);
}
/**
* Gets the entry's value as a boolean array. If the entry does not exist
@@ -170,7 +181,9 @@ class NetworkTableEntry final {
* because std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const;
std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const {
return nt::GetBooleanArray(m_handle, defaultValue);
}
/**
* Gets the entry's value as a integer array. If the entry does not exist
@@ -183,7 +196,9 @@ class NetworkTableEntry final {
* concern, use GetValue() instead.
*/
std::vector<int64_t> GetIntegerArray(
std::span<const int64_t> defaultValue) const;
std::span<const int64_t> defaultValue) const {
return nt::GetIntegerArray(m_handle, defaultValue);
}
/**
* Gets the entry's value as a float array. If the entry does not exist
@@ -195,7 +210,9 @@ class NetworkTableEntry final {
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*/
std::vector<float> GetFloatArray(std::span<const float> defaultValue) const;
std::vector<float> GetFloatArray(std::span<const float> defaultValue) const {
return nt::GetFloatArray(m_handle, defaultValue);
}
/**
* Gets the entry's value as a double array. If the entry does not exist
@@ -208,7 +225,9 @@ class NetworkTableEntry final {
* concern, use GetValue() instead.
*/
std::vector<double> GetDoubleArray(
std::span<const double> defaultValue) const;
std::span<const double> defaultValue) const {
return nt::GetDoubleArray(m_handle, defaultValue);
}
/**
* Gets the entry's value as a string array. If the entry does not exist
@@ -221,7 +240,9 @@ class NetworkTableEntry final {
* concern, use GetValue() instead.
*/
std::vector<std::string> GetStringArray(
std::span<const std::string> defaultValue) const;
std::span<const std::string> defaultValue) const {
return nt::GetStringArray(m_handle, defaultValue);
}
/**
* Get an array of all value changes since the last call to ReadQueue.
@@ -231,7 +252,9 @@ class NetworkTableEntry final {
* @return Array of values; empty array if no new changes have been
* published since the previous call.
*/
std::vector<NetworkTableValue> ReadQueue();
std::vector<NetworkTableValue> ReadQueue() {
return nt::ReadQueueValue(m_handle);
}
/**
* Sets the entry's value if it does not exist.
@@ -239,7 +262,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultValue(const Value& defaultValue);
bool SetDefaultValue(const Value& defaultValue) {
return SetDefaultEntryValue(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -247,7 +272,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultBoolean(bool defaultValue);
bool SetDefaultBoolean(bool defaultValue) {
return nt::SetDefaultBoolean(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -255,7 +282,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultInteger(int64_t defaultValue);
bool SetDefaultInteger(int64_t defaultValue) {
return nt::SetDefaultInteger(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -263,7 +292,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultFloat(float defaultValue);
bool SetDefaultFloat(float defaultValue) {
return nt::SetDefaultFloat(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -271,7 +302,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultDouble(double defaultValue);
bool SetDefaultDouble(double defaultValue) {
return nt::SetDefaultDouble(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -279,7 +312,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultString(std::string_view defaultValue);
bool SetDefaultString(std::string_view defaultValue) {
return nt::SetDefaultString(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -287,7 +322,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultRaw(std::span<const uint8_t> defaultValue);
bool SetDefaultRaw(std::span<const uint8_t> defaultValue) {
return nt::SetDefaultRaw(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -295,7 +332,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultBooleanArray(std::span<const int> defaultValue);
bool SetDefaultBooleanArray(std::span<const int> defaultValue) {
return nt::SetDefaultBooleanArray(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -303,7 +342,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue);
bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue) {
return nt::SetDefaultIntegerArray(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -311,7 +352,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultFloatArray(std::span<const float> defaultValue);
bool SetDefaultFloatArray(std::span<const float> defaultValue) {
return nt::SetDefaultFloatArray(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -319,7 +362,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultDoubleArray(std::span<const double> defaultValue);
bool SetDefaultDoubleArray(std::span<const double> defaultValue) {
return nt::SetDefaultDoubleArray(m_handle, defaultValue);
}
/**
* Sets the entry's value if it does not exist.
@@ -327,7 +372,9 @@ class NetworkTableEntry final {
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultStringArray(std::span<const std::string> defaultValue);
bool SetDefaultStringArray(std::span<const std::string> defaultValue) {
return nt::SetDefaultStringArray(m_handle, defaultValue);
}
/**
* Sets the entry's value.
@@ -335,7 +382,7 @@ class NetworkTableEntry final {
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetValue(const Value& value);
bool SetValue(const Value& value) { return SetEntryValue(m_handle, value); }
/**
* Sets the entry's value.
@@ -344,7 +391,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBoolean(bool value, int64_t time = 0);
bool SetBoolean(bool value, int64_t time = 0) {
return nt::SetBoolean(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -353,7 +402,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetInteger(int64_t value, int64_t time = 0);
bool SetInteger(int64_t value, int64_t time = 0) {
return nt::SetInteger(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -362,7 +413,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetFloat(float value, int64_t time = 0);
bool SetFloat(float value, int64_t time = 0) {
return nt::SetFloat(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -371,7 +424,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetDouble(double value, int64_t time = 0);
bool SetDouble(double value, int64_t time = 0) {
return nt::SetDouble(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -380,7 +435,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetString(std::string_view value, int64_t time = 0);
bool SetString(std::string_view value, int64_t time = 0) {
return nt::SetString(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -389,7 +446,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetRaw(std::span<const uint8_t> value, int64_t time = 0);
bool SetRaw(std::span<const uint8_t> value, int64_t time = 0) {
return nt::SetRaw(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -398,7 +457,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::span<const bool> value, int64_t time = 0);
bool SetBooleanArray(std::span<const bool> value, int64_t time = 0) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value, time));
}
/**
* Sets the entry's value.
@@ -407,7 +468,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::span<const int> value, int64_t time = 0);
bool SetBooleanArray(std::span<const int> value, int64_t time = 0) {
return nt::SetBooleanArray(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -416,7 +479,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0);
bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0) {
return nt::SetIntegerArray(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -425,7 +490,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetFloatArray(std::span<const float> value, int64_t time = 0);
bool SetFloatArray(std::span<const float> value, int64_t time = 0) {
return nt::SetFloatArray(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -434,7 +501,9 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetDoubleArray(std::span<const double> value, int64_t time = 0);
bool SetDoubleArray(std::span<const double> value, int64_t time = 0) {
return nt::SetDoubleArray(m_handle, value, time);
}
/**
* Sets the entry's value.
@@ -443,29 +512,37 @@ class NetworkTableEntry final {
* @param time the timestamp to set (0 = nt::Now())
* @return False if the entry exists with a different type
*/
bool SetStringArray(std::span<const std::string> value, int64_t time = 0);
bool SetStringArray(std::span<const std::string> value, int64_t time = 0) {
return nt::SetStringArray(m_handle, value, time);
}
/**
* Make value persistent through program restarts.
*/
void SetPersistent();
void SetPersistent() {
nt::SetTopicPersistent(nt::GetTopicFromHandle(m_handle), true);
}
/**
* Stop making value persistent through program restarts.
*/
void ClearPersistent();
void ClearPersistent() {
nt::SetTopicPersistent(nt::GetTopicFromHandle(m_handle), false);
}
/**
* Returns whether the value is persistent through program restarts.
*
* @return True if the value is persistent.
*/
bool IsPersistent() const;
bool IsPersistent() const {
return nt::GetTopicPersistent(nt::GetTopicFromHandle(m_handle));
}
/**
* Stops publishing the entry if it's been published.
*/
void Unpublish();
void Unpublish() { return nt::Unpublish(m_handle); }
/**
* Gets the entry's topic.
@@ -486,5 +563,3 @@ class NetworkTableEntry final {
};
} // namespace nt
#include "networktables/NetworkTableEntry.inc"

View File

@@ -1,231 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "networktables/NetworkTableEntry.h"
#include "ntcore_cpp.h"
#include "ntcore_cpp_types.h"
namespace nt {
inline NetworkTableEntry::NetworkTableEntry() {}
inline NetworkTableEntry::NetworkTableEntry(NT_Entry handle)
: m_handle{handle} {}
inline NT_Entry NetworkTableEntry::GetHandle() const {
return m_handle;
}
inline bool NetworkTableEntry::Exists() const {
return GetEntryType(m_handle) != NT_UNASSIGNED;
}
inline std::string NetworkTableEntry::GetName() const {
return GetEntryName(m_handle);
}
inline NetworkTableType NetworkTableEntry::GetType() const {
return static_cast<NetworkTableType>(GetEntryType(m_handle));
}
inline int64_t NetworkTableEntry::GetLastChange() const {
return GetEntryLastChange(m_handle);
}
inline Value NetworkTableEntry::GetValue() const {
return GetEntryValue(m_handle);
}
inline bool NetworkTableEntry::GetBoolean(bool defaultValue) const {
return nt::GetBoolean(m_handle, defaultValue);
}
inline int64_t NetworkTableEntry::GetInteger(int64_t defaultValue) const {
return nt::GetInteger(m_handle, defaultValue);
}
inline float NetworkTableEntry::GetFloat(float defaultValue) const {
return nt::GetFloat(m_handle, defaultValue);
}
inline double NetworkTableEntry::GetDouble(double defaultValue) const {
return nt::GetDouble(m_handle, defaultValue);
}
inline std::string NetworkTableEntry::GetString(
std::string_view defaultValue) const {
return nt::GetString(m_handle, defaultValue);
}
inline std::vector<uint8_t> NetworkTableEntry::GetRaw(
std::span<const uint8_t> defaultValue) const {
return nt::GetRaw(m_handle, defaultValue);
}
inline std::vector<int> NetworkTableEntry::GetBooleanArray(
std::span<const int> defaultValue) const {
return nt::GetBooleanArray(m_handle, defaultValue);
}
inline std::vector<int64_t> NetworkTableEntry::GetIntegerArray(
std::span<const int64_t> defaultValue) const {
return nt::GetIntegerArray(m_handle, defaultValue);
}
inline std::vector<float> NetworkTableEntry::GetFloatArray(
std::span<const float> defaultValue) const {
return nt::GetFloatArray(m_handle, defaultValue);
}
inline std::vector<double> NetworkTableEntry::GetDoubleArray(
std::span<const double> defaultValue) const {
return nt::GetDoubleArray(m_handle, defaultValue);
}
inline std::vector<std::string> NetworkTableEntry::GetStringArray(
std::span<const std::string> defaultValue) const {
return nt::GetStringArray(m_handle, defaultValue);
}
inline std::vector<NetworkTableValue> NetworkTableEntry::ReadQueue() {
return nt::ReadQueueValue(m_handle);
}
inline bool NetworkTableEntry::SetDefaultValue(const Value& defaultValue) {
return SetDefaultEntryValue(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultBoolean(bool defaultValue) {
return nt::SetDefaultBoolean(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultInteger(int64_t defaultValue) {
return nt::SetDefaultInteger(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultFloat(float defaultValue) {
return nt::SetDefaultFloat(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultDouble(double defaultValue) {
return nt::SetDefaultDouble(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultString(std::string_view defaultValue) {
return nt::SetDefaultString(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultRaw(
std::span<const uint8_t> defaultValue) {
return nt::SetDefaultRaw(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultBooleanArray(
std::span<const int> defaultValue) {
return nt::SetDefaultBooleanArray(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultIntegerArray(
std::span<const int64_t> defaultValue) {
return nt::SetDefaultIntegerArray(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultFloatArray(
std::span<const float> defaultValue) {
return nt::SetDefaultFloatArray(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultDoubleArray(
std::span<const double> defaultValue) {
return nt::SetDefaultDoubleArray(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetDefaultStringArray(
std::span<const std::string> defaultValue) {
return nt::SetDefaultStringArray(m_handle, defaultValue);
}
inline bool NetworkTableEntry::SetValue(const Value& value) {
return SetEntryValue(m_handle, value);
}
inline bool NetworkTableEntry::SetBoolean(bool value, int64_t time) {
return nt::SetBoolean(m_handle, value, time);
}
inline bool NetworkTableEntry::SetInteger(int64_t value, int64_t time) {
return nt::SetInteger(m_handle, value, time);
}
inline bool NetworkTableEntry::SetFloat(float value, int64_t time) {
return nt::SetFloat(m_handle, value, time);
}
inline bool NetworkTableEntry::SetDouble(double value, int64_t time) {
return nt::SetDouble(m_handle, value, time);
}
inline bool NetworkTableEntry::SetString(std::string_view value, int64_t time) {
return nt::SetString(m_handle, value, time);
}
inline bool NetworkTableEntry::SetRaw(std::span<const uint8_t> value,
int64_t time) {
return nt::SetRaw(m_handle, value, time);
}
inline bool NetworkTableEntry::SetBooleanArray(std::span<const bool> value,
int64_t time) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value, time));
}
inline bool NetworkTableEntry::SetBooleanArray(std::span<const int> value,
int64_t time) {
return nt::SetBooleanArray(m_handle, value, time);
}
inline bool NetworkTableEntry::SetIntegerArray(std::span<const int64_t> value,
int64_t time) {
return nt::SetIntegerArray(m_handle, value, time);
}
inline bool NetworkTableEntry::SetFloatArray(std::span<const float> value,
int64_t time) {
return nt::SetFloatArray(m_handle, value, time);
}
inline bool NetworkTableEntry::SetDoubleArray(std::span<const double> value,
int64_t time) {
return nt::SetDoubleArray(m_handle, value, time);
}
inline bool NetworkTableEntry::SetStringArray(
std::span<const std::string> value, int64_t time) {
return nt::SetStringArray(m_handle, value, time);
}
inline void NetworkTableEntry::SetPersistent() {
nt::SetTopicPersistent(nt::GetTopicFromHandle(m_handle), true);
}
inline void NetworkTableEntry::ClearPersistent() {
nt::SetTopicPersistent(nt::GetTopicFromHandle(m_handle), false);
}
inline bool NetworkTableEntry::IsPersistent() const {
return nt::GetTopicPersistent(nt::GetTopicFromHandle(m_handle));
}
inline void NetworkTableEntry::Unpublish() {
return nt::Unpublish(m_handle);
}
} // namespace nt

View File

@@ -4,11 +4,9 @@
#pragma once
#include <functional>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@@ -109,14 +107,14 @@ class NetworkTableInstance final {
/**
* Construct invalid instance.
*/
NetworkTableInstance() noexcept;
NetworkTableInstance() noexcept = default;
/**
* Construct from native handle.
*
* @param inst Native handle
* @param handle Native handle
*/
explicit NetworkTableInstance(NT_Inst inst) noexcept;
explicit NetworkTableInstance(NT_Inst handle) noexcept : m_handle{handle} {}
/**
* Determines if the native handle is valid.
@@ -130,28 +128,37 @@ class NetworkTableInstance final {
*
* @return Global default instance
*/
static NetworkTableInstance GetDefault();
static NetworkTableInstance GetDefault() {
return NetworkTableInstance{GetDefaultInstance()};
}
/**
* Create an instance.
*
* @return Newly created instance
*/
static NetworkTableInstance Create();
static NetworkTableInstance Create() {
return NetworkTableInstance{CreateInstance()};
}
/**
* Destroys an instance (note: this has global effect).
*
* @param inst Instance
*/
static void Destroy(NetworkTableInstance& inst);
static void Destroy(NetworkTableInstance& inst) {
if (inst.m_handle != 0) {
DestroyInstance(inst.m_handle);
inst.m_handle = 0;
}
}
/**
* Gets the native handle for the entry.
*
* @return Native handle
*/
NT_Inst GetHandle() const;
NT_Inst GetHandle() const { return m_handle; }
/**
* Gets a "generic" (untyped) topic.
@@ -256,7 +263,9 @@ class NetworkTableInstance final {
* @return Topic
*/
template <wpi::ProtobufSerializable T>
ProtobufTopic<T> GetProtobufTopic(std::string_view name) const;
ProtobufTopic<T> GetProtobufTopic(std::string_view name) const {
return ProtobufTopic<T>{GetTopic(name)};
}
/**
* Gets a raw struct serialized value topic.
@@ -267,7 +276,9 @@ class NetworkTableInstance final {
*/
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
StructTopic<T, I...> GetStructTopic(std::string_view name, I... info) const;
StructTopic<T, I...> GetStructTopic(std::string_view name, I... info) const {
return StructTopic<T, I...>{GetTopic(name), std::move(info)...};
}
/**
* Gets a raw struct serialized array topic.
@@ -279,7 +290,9 @@ class NetworkTableInstance final {
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
StructArrayTopic<T, I...> GetStructArrayTopic(std::string_view name,
I... info) const;
I... info) const {
return StructArrayTopic<T, I...>{GetTopic(name), std::move(info)...};
}
/**
* Get Published Topics.
@@ -288,7 +301,10 @@ class NetworkTableInstance final {
*
* @return Array of topics.
*/
std::vector<Topic> GetTopics();
std::vector<Topic> GetTopics() {
auto handles = ::nt::GetTopics(m_handle, "", 0);
return {handles.begin(), handles.end()};
}
/**
* Get Published Topics.
@@ -300,7 +316,10 @@ class NetworkTableInstance final {
* starts with this string are returned
* @return Array of topics.
*/
std::vector<Topic> GetTopics(std::string_view prefix);
std::vector<Topic> GetTopics(std::string_view prefix) {
auto handles = ::nt::GetTopics(m_handle, prefix, 0);
return {handles.begin(), handles.end()};
}
/**
* Get Published Topics.
@@ -314,7 +333,10 @@ class NetworkTableInstance final {
* as a "don't care"
* @return Array of topics.
*/
std::vector<Topic> GetTopics(std::string_view prefix, unsigned int types);
std::vector<Topic> GetTopics(std::string_view prefix, unsigned int types) {
auto handles = ::nt::GetTopics(m_handle, prefix, types);
return {handles.begin(), handles.end()};
}
/**
* Get Published Topics.
@@ -328,7 +350,10 @@ class NetworkTableInstance final {
* @return Array of topic handles.
*/
std::vector<Topic> GetTopics(std::string_view prefix,
std::span<std::string_view> types);
std::span<std::string_view> types) {
auto handles = ::nt::GetTopics(m_handle, prefix, types);
return {handles.begin(), handles.end()};
}
/**
* Get Topic Information about multiple topics.
@@ -337,7 +362,9 @@ class NetworkTableInstance final {
*
* @return Array of topic information.
*/
std::vector<TopicInfo> GetTopicInfo();
std::vector<TopicInfo> GetTopicInfo() {
return ::nt::GetTopicInfo(m_handle, "", 0);
}
/**
* Get Topic Information about multiple topics.
@@ -350,7 +377,9 @@ class NetworkTableInstance final {
* starts with this string are returned
* @return Array of topic information.
*/
std::vector<TopicInfo> GetTopicInfo(std::string_view prefix);
std::vector<TopicInfo> GetTopicInfo(std::string_view prefix) {
return ::nt::GetTopicInfo(m_handle, prefix, 0);
}
/**
* Get Topic Information about multiple topics.
@@ -366,7 +395,9 @@ class NetworkTableInstance final {
* @return Array of topic information.
*/
std::vector<TopicInfo> GetTopicInfo(std::string_view prefix,
unsigned int types);
unsigned int types) {
return ::nt::GetTopicInfo(m_handle, prefix, types);
}
/**
* Get Topic Information about multiple topics.
@@ -381,7 +412,9 @@ class NetworkTableInstance final {
* @return Array of topic information.
*/
std::vector<TopicInfo> GetTopicInfo(std::string_view prefix,
std::span<std::string_view> types);
std::span<std::string_view> types) {
return ::nt::GetTopicInfo(m_handle, prefix, types);
}
/**
* Gets the entry for a key.
@@ -389,7 +422,9 @@ class NetworkTableInstance final {
* @param name Key
* @return Network table entry.
*/
NetworkTableEntry GetEntry(std::string_view name);
NetworkTableEntry GetEntry(std::string_view name) {
return NetworkTableEntry{::nt::GetEntry(m_handle, name)};
}
/**
* Gets the table with the specified key.
@@ -409,7 +444,9 @@ class NetworkTableInstance final {
*
* @param listener Listener handle to remove
*/
static void RemoveListener(NT_Listener listener);
static void RemoveListener(NT_Listener listener) {
::nt::RemoveListener(listener);
}
/**
* Wait for the listener queue to be empty. This is primarily
@@ -421,7 +458,9 @@ class NetworkTableInstance final {
* a negative value to block indefinitely
* @return False if timed out, otherwise true.
*/
bool WaitForListenerQueue(double timeout);
bool WaitForListenerQueue(double timeout) {
return ::nt::WaitForListenerQueue(m_handle, timeout);
}
/**
* Add a connection listener. The callback function is called asynchronously
@@ -433,7 +472,12 @@ class NetworkTableInstance final {
* @return Listener handle
*/
NT_Listener AddConnectionListener(bool immediate_notify,
ListenerCallback callback) const;
ListenerCallback callback) const {
return ::nt::AddListener(
m_handle,
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(callback));
}
/**
* Add a time synchronization listener. The callback function is called
@@ -447,7 +491,12 @@ class NetworkTableInstance final {
* @return Listener handle
*/
NT_Listener AddTimeSyncListener(bool immediate_notify,
ListenerCallback callback) const;
ListenerCallback callback) const {
return ::nt::AddListener(
m_handle,
NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(callback));
}
/**
* Add a listener for changes on a particular topic. The callback
@@ -523,7 +572,10 @@ class NetworkTableInstance final {
* @return Listener handle
*/
NT_Listener AddListener(std::span<const std::string_view> prefixes,
int eventMask, ListenerCallback listener);
int eventMask, ListenerCallback listener) {
return ::nt::AddListener(m_handle, prefixes, eventMask,
std::move(listener));
}
/** @} */
@@ -537,20 +589,20 @@ class NetworkTableInstance final {
*
* @return Bitmask of NetworkMode.
*/
unsigned int GetNetworkMode() const;
unsigned int GetNetworkMode() const { return ::nt::GetNetworkMode(m_handle); }
/**
* Starts local-only operation. Prevents calls to StartServer or StartClient
* from taking effect. Has no effect if StartServer or StartClient
* has already been called.
*/
void StartLocal();
void StartLocal() { ::nt::StartLocal(m_handle); }
/**
* Stops local-only operation. StartServer or StartClient can be called after
* this call to start a server or client.
*/
void StopLocal();
void StopLocal() { ::nt::StopLocal(m_handle); }
/**
* Starts a server using the specified filename, listening address, and port.
@@ -565,12 +617,14 @@ class NetworkTableInstance final {
void StartServer(std::string_view persist_filename = "networktables.json",
const char* listen_address = "",
unsigned int port3 = kDefaultPort3,
unsigned int port4 = kDefaultPort4);
unsigned int port4 = kDefaultPort4) {
::nt::StartServer(m_handle, persist_filename, listen_address, port3, port4);
}
/**
* Stops the server if it is running.
*/
void StopServer();
void StopServer() { ::nt::StopServer(m_handle); }
/**
* Starts a NT3 client. Use SetServer or SetServerTeam to set the server name
@@ -578,7 +632,9 @@ class NetworkTableInstance final {
*
* @param identity network identity to advertise (cannot be empty string)
*/
void StartClient3(std::string_view identity);
void StartClient3(std::string_view identity) {
::nt::StartClient3(m_handle, identity);
}
/**
* Starts a NT4 client. Use SetServer or SetServerTeam to set the server name
@@ -586,12 +642,14 @@ class NetworkTableInstance final {
*
* @param identity network identity to advertise (cannot be empty string)
*/
void StartClient4(std::string_view identity);
void StartClient4(std::string_view identity) {
::nt::StartClient4(m_handle, identity);
}
/**
* Stops the client if it is running.
*/
void StopClient();
void StopClient() { ::nt::StopClient(m_handle); }
/**
* Sets server address and port for client (without restarting client).
@@ -599,7 +657,9 @@ class NetworkTableInstance final {
* @param server_name server name (UTF-8 string)
* @param port port to communicate over (0 = default)
*/
void SetServer(std::string_view server_name, unsigned int port = 0);
void SetServer(std::string_view server_name, unsigned int port = 0) {
::nt::SetServer(m_handle, server_name, port);
}
/**
* Sets server addresses and ports for client (without restarting client).
@@ -608,7 +668,9 @@ class NetworkTableInstance final {
* @param servers array of server address and port pairs
*/
void SetServer(
std::span<const std::pair<std::string_view, unsigned int>> servers);
std::span<const std::pair<std::string_view, unsigned int>> servers) {
::nt::SetServer(m_handle, servers);
}
/**
* Sets server addresses and port for client (without restarting client).
@@ -627,13 +689,15 @@ class NetworkTableInstance final {
* @param team team number
* @param port port to communicate over (0 = default)
*/
void SetServerTeam(unsigned int team, unsigned int port = 0);
void SetServerTeam(unsigned int team, unsigned int port = 0) {
::nt::SetServerTeam(m_handle, team, port);
}
/**
* Disconnects the client if it's running and connected. This will
* automatically start reconnection attempts to the current server list.
*/
void Disconnect();
void Disconnect() { ::nt::Disconnect(m_handle); }
/**
* Starts requesting server address from Driver Station.
@@ -642,18 +706,20 @@ class NetworkTableInstance final {
*
* @param port server port to use in combination with IP from DS (0 = default)
*/
void StartDSClient(unsigned int port = 0);
void StartDSClient(unsigned int port = 0) {
::nt::StartDSClient(m_handle, port);
}
/**
* Stops requesting server address from Driver Station.
*/
void StopDSClient();
void StopDSClient() { ::nt::StopDSClient(m_handle); }
/**
* Flushes all updated values immediately to the local client/server. This
* does not flush to the network.
*/
void FlushLocal() const;
void FlushLocal() const { ::nt::FlushLocal(m_handle); }
/**
* Flushes all updated values immediately to the network.
@@ -661,7 +727,7 @@ class NetworkTableInstance final {
* This is primarily useful for synchronizing network updates with
* user code.
*/
void Flush() const;
void Flush() const { ::nt::Flush(m_handle); }
/**
* Get information on the currently established network connections.
@@ -669,14 +735,16 @@ class NetworkTableInstance final {
*
* @return array of connection information
*/
std::vector<ConnectionInfo> GetConnections() const;
std::vector<ConnectionInfo> GetConnections() const {
return ::nt::GetConnections(m_handle);
}
/**
* Return whether or not the instance is connected to another node.
*
* @return True if connected.
*/
bool IsConnected() const;
bool IsConnected() const { return ::nt::IsConnected(m_handle); }
/**
* Get the time offset between server time and local time. Add this value to
@@ -689,7 +757,9 @@ class NetworkTableInstance final {
*
* @return Time offset in microseconds (optional)
*/
std::optional<int64_t> GetServerTimeOffset() const;
std::optional<int64_t> GetServerTimeOffset() const {
return ::nt::GetServerTimeOffset(m_handle);
}
/** @} */
@@ -710,14 +780,18 @@ class NetworkTableInstance final {
*/
NT_DataLogger StartEntryDataLog(wpi::log::DataLog& log,
std::string_view prefix,
std::string_view logPrefix);
std::string_view logPrefix) {
return ::nt::StartEntryDataLog(m_handle, log, prefix, logPrefix);
}
/**
* Stops logging entry changes to a DataLog.
*
* @param logger data logger handle
*/
static void StopEntryDataLog(NT_DataLogger logger);
static void StopEntryDataLog(NT_DataLogger logger) {
::nt::StopEntryDataLog(logger);
}
/**
* Starts logging connection changes to a DataLog.
@@ -728,14 +802,18 @@ class NetworkTableInstance final {
* @return Data logger handle
*/
NT_ConnectionDataLogger StartConnectionDataLog(wpi::log::DataLog& log,
std::string_view name);
std::string_view name) {
return ::nt::StartConnectionDataLog(m_handle, log, name);
}
/**
* Stops logging connection changes to a DataLog.
*
* @param logger data logger handle
*/
static void StopConnectionDataLog(NT_ConnectionDataLogger logger);
static void StopConnectionDataLog(NT_ConnectionDataLogger logger) {
::nt::StopConnectionDataLog(logger);
}
/** @} */
@@ -757,7 +835,9 @@ class NetworkTableInstance final {
* @return Listener handle
*/
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel,
ListenerCallback func);
ListenerCallback func) {
return ::nt::AddLogger(m_handle, minLevel, maxLevel, std::move(func));
}
/** @} */
@@ -775,7 +855,9 @@ class NetworkTableInstance final {
* schema)
* @return True if schema already registered
*/
bool HasSchema(std::string_view name) const;
bool HasSchema(std::string_view name) const {
return ::nt::HasSchema(m_handle, name);
}
/**
* Registers a data schema. Data schemas provide information for how a
@@ -792,7 +874,9 @@ class NetworkTableInstance final {
* @param schema Schema data
*/
void AddSchema(std::string_view name, std::string_view type,
std::span<const uint8_t> schema);
std::span<const uint8_t> schema) {
::nt::AddSchema(m_handle, name, type, schema);
}
/**
* Registers a data schema. Data schemas provide information for how a
@@ -809,7 +893,15 @@ class NetworkTableInstance final {
* @param schema Schema data
*/
void AddSchema(std::string_view name, std::string_view type,
std::string_view schema);
std::string_view schema) {
::nt::AddSchema(m_handle, name, type, schema);
}
// Suppress unused-lambda-capture warning on AddSchema() call
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-lambda-capture"
#endif
/**
* Registers a protobuf schema. Duplicate calls to this function with the same
@@ -819,7 +911,13 @@ class NetworkTableInstance final {
* @param msg protobuf message
*/
template <wpi::ProtobufSerializable T>
void AddProtobufSchema(wpi::ProtobufMessage<T>& msg);
void AddProtobufSchema(wpi::ProtobufMessage<T>& msg) {
msg.ForEachProtobufDescriptor(
[this](auto typeString) { return HasSchema(typeString); },
[this](auto typeString, auto schema) {
AddSchema(typeString, "proto:FileDescriptorProto", schema);
});
}
/**
* Registers a struct schema. Duplicate calls to this function with the same
@@ -830,7 +928,17 @@ class NetworkTableInstance final {
*/
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
void AddStructSchema(const I&... info);
void AddStructSchema(const I&... info) {
wpi::ForEachStructSchema<T>(
[this](auto typeString, auto schema) {
AddSchema(typeString, "structschema", schema);
},
info...);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
/**
* Equality operator. Returns true if both instances refer to the same
@@ -844,5 +952,3 @@ class NetworkTableInstance final {
};
} // namespace nt
#include "networktables/NetworkTableInstance.inc"

View File

@@ -1,291 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <string_view>
#include <utility>
#include <vector>
#include "networktables/NetworkTableInstance.h"
#include "networktables/Topic.h"
#include "ntcore_c.h"
namespace nt {
inline NetworkTableInstance::NetworkTableInstance() noexcept {}
inline NetworkTableInstance::NetworkTableInstance(NT_Inst handle) noexcept
: m_handle{handle} {}
inline NetworkTableInstance NetworkTableInstance::GetDefault() {
return NetworkTableInstance{GetDefaultInstance()};
}
inline NetworkTableInstance NetworkTableInstance::Create() {
return NetworkTableInstance{CreateInstance()};
}
inline void NetworkTableInstance::Destroy(NetworkTableInstance& inst) {
if (inst.m_handle != 0) {
DestroyInstance(inst.m_handle);
inst.m_handle = 0;
}
}
inline NT_Inst NetworkTableInstance::GetHandle() const {
return m_handle;
}
template <wpi::ProtobufSerializable T>
inline ProtobufTopic<T> NetworkTableInstance::GetProtobufTopic(
std::string_view name) const {
return ProtobufTopic<T>{GetTopic(name)};
}
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
inline StructTopic<T, I...> NetworkTableInstance::GetStructTopic(
std::string_view name, I... info) const {
return StructTopic<T, I...>{GetTopic(name), std::move(info)...};
}
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
inline StructArrayTopic<T, I...> NetworkTableInstance::GetStructArrayTopic(
std::string_view name, I... info) const {
return StructArrayTopic<T, I...>{GetTopic(name), std::move(info)...};
}
inline std::vector<Topic> NetworkTableInstance::GetTopics() {
auto handles = ::nt::GetTopics(m_handle, "", 0);
return {handles.begin(), handles.end()};
}
inline std::vector<Topic> NetworkTableInstance::GetTopics(
std::string_view prefix) {
auto handles = ::nt::GetTopics(m_handle, prefix, 0);
return {handles.begin(), handles.end()};
}
inline std::vector<Topic> NetworkTableInstance::GetTopics(
std::string_view prefix, unsigned int types) {
auto handles = ::nt::GetTopics(m_handle, prefix, types);
return {handles.begin(), handles.end()};
}
inline std::vector<Topic> NetworkTableInstance::GetTopics(
std::string_view prefix, std::span<std::string_view> types) {
auto handles = ::nt::GetTopics(m_handle, prefix, types);
return {handles.begin(), handles.end()};
}
inline std::vector<TopicInfo> NetworkTableInstance::GetTopicInfo() {
return ::nt::GetTopicInfo(m_handle, "", 0);
}
inline std::vector<TopicInfo> NetworkTableInstance::GetTopicInfo(
std::string_view prefix) {
return ::nt::GetTopicInfo(m_handle, prefix, 0);
}
inline std::vector<TopicInfo> NetworkTableInstance::GetTopicInfo(
std::string_view prefix, unsigned int types) {
return ::nt::GetTopicInfo(m_handle, prefix, types);
}
inline std::vector<TopicInfo> NetworkTableInstance::GetTopicInfo(
std::string_view prefix, std::span<std::string_view> types) {
return ::nt::GetTopicInfo(m_handle, prefix, types);
}
inline NetworkTableEntry NetworkTableInstance::GetEntry(std::string_view name) {
return NetworkTableEntry{::nt::GetEntry(m_handle, name)};
}
inline bool NetworkTableInstance::WaitForListenerQueue(double timeout) {
return ::nt::WaitForListenerQueue(m_handle, timeout);
}
inline void NetworkTableInstance::RemoveListener(NT_Listener listener) {
::nt::RemoveListener(listener);
}
inline NT_Listener NetworkTableInstance::AddConnectionListener(
bool immediate_notify, ListenerCallback callback) const {
return ::nt::AddListener(
m_handle,
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(callback));
}
inline NT_Listener NetworkTableInstance::AddTimeSyncListener(
bool immediate_notify, ListenerCallback callback) const {
return ::nt::AddListener(
m_handle, NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(callback));
}
inline NT_Listener NetworkTableInstance::AddListener(
std::span<const std::string_view> prefixes, int eventMask,
ListenerCallback listener) {
return ::nt::AddListener(m_handle, prefixes, eventMask, std::move(listener));
}
inline unsigned int NetworkTableInstance::GetNetworkMode() const {
return ::nt::GetNetworkMode(m_handle);
}
inline void NetworkTableInstance::StartLocal() {
::nt::StartLocal(m_handle);
}
inline void NetworkTableInstance::StopLocal() {
::nt::StopLocal(m_handle);
}
inline void NetworkTableInstance::StartServer(std::string_view persist_filename,
const char* listen_address,
unsigned int port3,
unsigned int port4) {
::nt::StartServer(m_handle, persist_filename, listen_address, port3, port4);
}
inline void NetworkTableInstance::StopServer() {
::nt::StopServer(m_handle);
}
inline void NetworkTableInstance::StartClient3(std::string_view identity) {
::nt::StartClient3(m_handle, identity);
}
inline void NetworkTableInstance::StartClient4(std::string_view identity) {
::nt::StartClient4(m_handle, identity);
}
inline void NetworkTableInstance::StopClient() {
::nt::StopClient(m_handle);
}
inline void NetworkTableInstance::SetServer(std::string_view server_name,
unsigned int port) {
::nt::SetServer(m_handle, server_name, port);
}
inline void NetworkTableInstance::SetServer(
std::span<const std::pair<std::string_view, unsigned int>> servers) {
::nt::SetServer(m_handle, servers);
}
inline void NetworkTableInstance::SetServerTeam(unsigned int team,
unsigned int port) {
::nt::SetServerTeam(m_handle, team, port);
}
inline void NetworkTableInstance::Disconnect() {
::nt::Disconnect(m_handle);
}
inline void NetworkTableInstance::StartDSClient(unsigned int port) {
::nt::StartDSClient(m_handle, port);
}
inline void NetworkTableInstance::StopDSClient() {
::nt::StopDSClient(m_handle);
}
inline void NetworkTableInstance::FlushLocal() const {
::nt::FlushLocal(m_handle);
}
inline void NetworkTableInstance::Flush() const {
::nt::Flush(m_handle);
}
inline std::vector<ConnectionInfo> NetworkTableInstance::GetConnections()
const {
return ::nt::GetConnections(m_handle);
}
inline bool NetworkTableInstance::IsConnected() const {
return ::nt::IsConnected(m_handle);
}
inline std::optional<int64_t> NetworkTableInstance::GetServerTimeOffset()
const {
return ::nt::GetServerTimeOffset(m_handle);
}
inline NT_DataLogger NetworkTableInstance::StartEntryDataLog(
wpi::log::DataLog& log, std::string_view prefix,
std::string_view logPrefix) {
return ::nt::StartEntryDataLog(m_handle, log, prefix, logPrefix);
}
inline void NetworkTableInstance::StopEntryDataLog(NT_DataLogger logger) {
::nt::StopEntryDataLog(logger);
}
inline NT_ConnectionDataLogger NetworkTableInstance::StartConnectionDataLog(
wpi::log::DataLog& log, std::string_view name) {
return ::nt::StartConnectionDataLog(m_handle, log, name);
}
inline void NetworkTableInstance::StopConnectionDataLog(
NT_ConnectionDataLogger logger) {
::nt::StopConnectionDataLog(logger);
}
inline NT_Listener NetworkTableInstance::AddLogger(unsigned int min_level,
unsigned int max_level,
ListenerCallback func) {
return ::nt::AddLogger(m_handle, min_level, max_level, std::move(func));
}
inline bool NetworkTableInstance::HasSchema(std::string_view name) const {
return ::nt::HasSchema(m_handle, name);
}
inline void NetworkTableInstance::AddSchema(std::string_view name,
std::string_view type,
std::span<const uint8_t> schema) {
::nt::AddSchema(m_handle, name, type, schema);
}
inline void NetworkTableInstance::AddSchema(std::string_view name,
std::string_view type,
std::string_view schema) {
::nt::AddSchema(m_handle, name, type, schema);
}
// Suppress unused-lambda-capture warning on AddSchema() call
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-lambda-capture"
#endif
template <wpi::ProtobufSerializable T>
void NetworkTableInstance::AddProtobufSchema(wpi::ProtobufMessage<T>& msg) {
msg.ForEachProtobufDescriptor(
[this](auto typeString) { return HasSchema(typeString); },
[this](auto typeString, auto schema) {
AddSchema(typeString, "proto:FileDescriptorProto", schema);
});
}
template <typename T, typename... I>
requires wpi::StructSerializable<T, I...>
void NetworkTableInstance::AddStructSchema(const I&... info) {
wpi::ForEachStructSchema<T>(
[this](auto typeString, auto schema) {
AddSchema(typeString, "structschema", schema);
},
info...);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} // namespace nt

View File

@@ -4,11 +4,15 @@
#pragma once
#include <functional>
#include <span>
#include <string_view>
#include <utility>
#include <vector>
#include "networktables/MultiSubscriber.h"
#include "networktables/NetworkTableEntry.h"
#include "networktables/NetworkTableInstance.h"
#include "networktables/Topic.h"
#include "ntcore_cpp.h"
namespace nt {
@@ -42,7 +46,10 @@ class NetworkTableListener final {
*/
static NetworkTableListener CreateListener(
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
unsigned int mask, ListenerCallback listener);
unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{::nt::AddListener(inst.GetHandle(), prefixes,
mask, std::move(listener))};
}
/**
* Create a listener for changes on a particular topic. This creates a
@@ -54,7 +61,10 @@ class NetworkTableListener final {
* @return Listener
*/
static NetworkTableListener CreateListener(Topic topic, unsigned int mask,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{
nt::AddListener(topic.GetHandle(), mask, std::move(listener))};
}
/**
* Create a listener for topic changes on a subscriber. This does NOT keep the
@@ -67,7 +77,10 @@ class NetworkTableListener final {
*/
static NetworkTableListener CreateListener(Subscriber& subscriber,
unsigned int mask,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
}
/**
* Create a listener for topic changes on a subscriber. This does NOT keep the
@@ -80,7 +93,10 @@ class NetworkTableListener final {
*/
static NetworkTableListener CreateListener(MultiSubscriber& subscriber,
unsigned int mask,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
}
/**
* Create a listener for topic changes on an entry.
@@ -92,7 +108,10 @@ class NetworkTableListener final {
*/
static NetworkTableListener CreateListener(NetworkTableEntry& entry,
unsigned int mask,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(entry.GetHandle(), mask, std::move(listener))};
}
/**
* Create a connection listener.
@@ -104,7 +123,12 @@ class NetworkTableListener final {
*/
static NetworkTableListener CreateConnectionListener(
NetworkTableInstance inst, bool immediate_notify,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{::nt::AddListener(
inst.GetHandle(),
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(listener))};
}
/**
* Create a time synchronization listener.
@@ -115,9 +139,14 @@ class NetworkTableListener final {
* @param listener listener function
* @return Listener
*/
static NetworkTableListener CreateTimeSyncListener(NetworkTableInstance inst,
bool immediate_notify,
ListenerCallback listener);
static NetworkTableListener CreateTimeSyncListener(
NetworkTableInstance inst, bool immediate_notify,
ListenerCallback listener) {
return NetworkTableListener{::nt::AddListener(
inst.GetHandle(),
NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(listener))};
}
/**
* Create a listener for log messages. By default, log messages are sent to
@@ -136,13 +165,32 @@ class NetworkTableListener final {
static NetworkTableListener CreateLogger(NetworkTableInstance inst,
unsigned int minLevel,
unsigned int maxLevel,
ListenerCallback listener);
ListenerCallback listener) {
return NetworkTableListener{::nt::AddLogger(inst.GetHandle(), minLevel,
maxLevel, std::move(listener))};
}
NetworkTableListener(const NetworkTableListener&) = delete;
NetworkTableListener& operator=(const NetworkTableListener&) = delete;
NetworkTableListener(NetworkTableListener&& rhs);
NetworkTableListener& operator=(NetworkTableListener&& rhs);
~NetworkTableListener();
NetworkTableListener(NetworkTableListener&& rhs) : m_handle(rhs.m_handle) {
rhs.m_handle = 0;
}
NetworkTableListener& operator=(NetworkTableListener&& rhs) {
if (m_handle != 0) {
nt::RemoveListener(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
~NetworkTableListener() {
if (m_handle != 0) {
nt::RemoveListener(m_handle);
}
}
explicit operator bool() const { return m_handle != 0; }
@@ -163,7 +211,13 @@ class NetworkTableListener final {
* a negative value to block indefinitely
* @return False if timed out, otherwise true.
*/
bool WaitForQueue(double timeout);
bool WaitForQueue(double timeout) {
if (m_handle != 0) {
return nt::WaitForListenerQueue(m_handle, timeout);
} else {
return false;
}
}
private:
explicit NetworkTableListener(NT_Listener handle) : m_handle{handle} {}
@@ -185,14 +239,32 @@ class NetworkTableListenerPoller final {
*
* @param inst Instance
*/
explicit NetworkTableListenerPoller(NetworkTableInstance inst);
explicit NetworkTableListenerPoller(NetworkTableInstance inst)
: m_handle(nt::CreateListenerPoller(inst.GetHandle())) {}
NetworkTableListenerPoller(const NetworkTableListenerPoller&) = delete;
NetworkTableListenerPoller& operator=(const NetworkTableListenerPoller&) =
delete;
NetworkTableListenerPoller(NetworkTableListenerPoller&& rhs);
NetworkTableListenerPoller& operator=(NetworkTableListenerPoller&& rhs);
~NetworkTableListenerPoller();
NetworkTableListenerPoller(NetworkTableListenerPoller&& rhs)
: m_handle(rhs.m_handle) {
rhs.m_handle = 0;
}
NetworkTableListenerPoller& operator=(NetworkTableListenerPoller&& rhs) {
if (m_handle != 0) {
nt::DestroyListenerPoller(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
~NetworkTableListenerPoller() {
if (m_handle != 0) {
nt::DestroyListenerPoller(m_handle);
}
}
explicit operator bool() const { return m_handle != 0; }
@@ -213,7 +285,9 @@ class NetworkTableListenerPoller final {
* @return Listener handle
*/
NT_Listener AddListener(std::span<const std::string_view> prefixes,
unsigned int mask);
unsigned int mask) {
return nt::AddPolledListener(m_handle, prefixes, mask);
}
/**
* Start listening to changes to a particular topic. This creates a
@@ -223,7 +297,9 @@ class NetworkTableListenerPoller final {
* @param mask Bitmask of EventFlags values
* @return Listener handle
*/
NT_Listener AddListener(Topic topic, unsigned int mask);
NT_Listener AddListener(Topic topic, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, topic.GetHandle(), mask);
}
/**
* Start listening to topic changes on a subscriber. This does NOT keep the
@@ -233,7 +309,9 @@ class NetworkTableListenerPoller final {
* @param mask Bitmask of EventFlags values
* @return Listener handle
*/
NT_Listener AddListener(Subscriber& subscriber, unsigned int mask);
NT_Listener AddListener(Subscriber& subscriber, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
}
/**
* Start listening to topic changes on a subscriber. This does NOT keep the
@@ -243,7 +321,9 @@ class NetworkTableListenerPoller final {
* @param mask Bitmask of EventFlags values
* @return Listener handle
*/
NT_Listener AddListener(MultiSubscriber& subscriber, unsigned int mask);
NT_Listener AddListener(MultiSubscriber& subscriber, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
}
/**
* Start listening to topic changes on an entry.
@@ -252,7 +332,9 @@ class NetworkTableListenerPoller final {
* @param mask Bitmask of EventFlags values
* @return Listener handle
*/
NT_Listener AddListener(NetworkTableEntry& entry, unsigned int mask);
NT_Listener AddListener(NetworkTableEntry& entry, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, entry.GetHandle(), mask);
}
/**
* Add a connection listener. The callback function is called asynchronously
@@ -262,7 +344,11 @@ class NetworkTableListenerPoller final {
* @param immediate_notify notify listener of all existing connections
* @return Listener handle
*/
NT_Listener AddConnectionListener(bool immediate_notify);
NT_Listener AddConnectionListener(bool immediate_notify) {
return ::nt::AddPolledListener(
m_handle, ::nt::GetInstanceFromHandle(m_handle),
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
}
/**
* Add a time synchronization listener. The callback function is called
@@ -274,7 +360,11 @@ class NetworkTableListenerPoller final {
* value
* @return Listener handle
*/
NT_Listener AddTimeSyncListener(bool immediate_notify);
NT_Listener AddTimeSyncListener(bool immediate_notify) {
return ::nt::AddPolledListener(
m_handle, ::nt::GetInstanceFromHandle(m_handle),
NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
}
/**
* Add logger callback function. By default, log messages are sent to stderr;
@@ -287,26 +377,26 @@ class NetworkTableListenerPoller final {
* @param maxLevel maximum log level
* @return Listener handle
*/
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel);
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel) {
return ::nt::AddPolledLogger(m_handle, minLevel, maxLevel);
}
/**
* Remove a listener.
*
* @param listener Listener handle
*/
void RemoveListener(NT_Listener listener);
void RemoveListener(NT_Listener listener) { ::nt::RemoveListener(listener); }
/**
* Read events.
*
* @return Events since the previous call to ReadQueue()
*/
std::vector<Event> ReadQueue();
std::vector<Event> ReadQueue() { return ::nt::ReadListenerQueue(m_handle); }
private:
NT_ListenerPoller m_handle{0};
};
} // namespace nt
#include "NetworkTableListener.inc"

View File

@@ -1,184 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <span>
#include <string_view>
#include <utility>
#include <vector>
#include "networktables/MultiSubscriber.h"
#include "networktables/NetworkTableEntry.h"
#include "networktables/NetworkTableInstance.h"
#include "networktables/NetworkTableListener.h"
#include "networktables/Topic.h"
#include "ntcore_cpp.h"
namespace nt {
inline NetworkTableListener NetworkTableListener::CreateListener(
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(inst.GetHandle(), prefixes, mask, std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateListener(
Topic topic, unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{
nt::AddListener(topic.GetHandle(), mask, std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateListener(
Subscriber& subscriber, unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateListener(
MultiSubscriber& subscriber, unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateListener(
NetworkTableEntry& entry, unsigned int mask, ListenerCallback listener) {
return NetworkTableListener{
::nt::AddListener(entry.GetHandle(), mask, std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateConnectionListener(
NetworkTableInstance inst, bool immediate_notify,
ListenerCallback listener) {
return NetworkTableListener{::nt::AddListener(
inst.GetHandle(),
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateTimeSyncListener(
NetworkTableInstance inst, bool immediate_notify,
ListenerCallback listener) {
return NetworkTableListener{::nt::AddListener(
inst.GetHandle(),
NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
std::move(listener))};
}
inline NetworkTableListener NetworkTableListener::CreateLogger(
NetworkTableInstance inst, unsigned int minLevel, unsigned int maxLevel,
ListenerCallback listener) {
return NetworkTableListener{::nt::AddLogger(inst.GetHandle(), minLevel,
maxLevel, std::move(listener))};
}
inline NetworkTableListener::NetworkTableListener(NetworkTableListener&& rhs)
: m_handle(rhs.m_handle) {
rhs.m_handle = 0;
}
inline NetworkTableListener& NetworkTableListener::operator=(
NetworkTableListener&& rhs) {
if (m_handle != 0) {
nt::RemoveListener(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
inline NetworkTableListener::~NetworkTableListener() {
if (m_handle != 0) {
nt::RemoveListener(m_handle);
}
}
inline bool NetworkTableListener::WaitForQueue(double timeout) {
if (m_handle != 0) {
return nt::WaitForListenerQueue(m_handle, timeout);
} else {
return false;
}
}
inline NetworkTableListenerPoller::NetworkTableListenerPoller(
NetworkTableInstance inst)
: m_handle(nt::CreateListenerPoller(inst.GetHandle())) {}
inline NetworkTableListenerPoller::NetworkTableListenerPoller(
NetworkTableListenerPoller&& rhs)
: m_handle(rhs.m_handle) {
rhs.m_handle = 0;
}
inline NetworkTableListenerPoller& NetworkTableListenerPoller::operator=(
NetworkTableListenerPoller&& rhs) {
if (m_handle != 0) {
nt::DestroyListenerPoller(m_handle);
}
m_handle = rhs.m_handle;
rhs.m_handle = 0;
return *this;
}
inline NetworkTableListenerPoller::~NetworkTableListenerPoller() {
if (m_handle != 0) {
nt::DestroyListenerPoller(m_handle);
}
}
inline NT_Listener NetworkTableListenerPoller::AddListener(
std::span<const std::string_view> prefixes, unsigned int mask) {
return nt::AddPolledListener(m_handle, prefixes, mask);
}
inline NT_Listener NetworkTableListenerPoller::AddListener(Topic topic,
unsigned int mask) {
return ::nt::AddPolledListener(m_handle, topic.GetHandle(), mask);
}
inline NT_Listener NetworkTableListenerPoller::AddListener(
Subscriber& subscriber, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
}
inline NT_Listener NetworkTableListenerPoller::AddListener(
MultiSubscriber& subscriber, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
}
inline NT_Listener NetworkTableListenerPoller::AddListener(
NetworkTableEntry& entry, unsigned int mask) {
return ::nt::AddPolledListener(m_handle, entry.GetHandle(), mask);
}
inline NT_Listener NetworkTableListenerPoller::AddConnectionListener(
bool immediate_notify) {
return ::nt::AddPolledListener(
m_handle, ::nt::GetInstanceFromHandle(m_handle),
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
}
inline NT_Listener NetworkTableListenerPoller::AddTimeSyncListener(
bool immediate_notify) {
return ::nt::AddPolledListener(
m_handle, ::nt::GetInstanceFromHandle(m_handle),
NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
}
inline NT_Listener NetworkTableListenerPoller::AddLogger(
unsigned int minLevel, unsigned int maxLevel) {
return ::nt::AddPolledLogger(m_handle, minLevel, maxLevel);
}
inline void NetworkTableListenerPoller::RemoveListener(NT_Listener listener) {
::nt::RemoveListener(listener);
}
inline std::vector<Event> NetworkTableListenerPoller::ReadQueue() {
return ::nt::ReadListenerQueue(m_handle);
}
} // namespace nt

View File

@@ -56,14 +56,16 @@ class Topic {
*
* @return the topic's name
*/
std::string GetName() const;
std::string GetName() const { return ::nt::GetTopicName(m_handle); }
/**
* Gets the type of the topic.
*
* @return the topic's type
*/
NetworkTableType GetType() const;
NetworkTableType GetType() const {
return static_cast<NetworkTableType>(::nt::GetTopicType(m_handle));
}
/**
* Gets the type string of the topic. This may have more information
@@ -71,28 +73,34 @@ class Topic {
*
* @return the topic's type
*/
std::string GetTypeString() const;
std::string GetTypeString() const {
return ::nt::GetTopicTypeString(m_handle);
}
/**
* Make value persistent through server restarts.
*
* @param persistent True for persistent, false for not persistent.
*/
void SetPersistent(bool persistent);
void SetPersistent(bool persistent) {
::nt::SetTopicPersistent(m_handle, persistent);
}
/**
* Returns whether the value is persistent through server restarts.
*
* @return True if the value is persistent.
*/
bool IsPersistent() const;
bool IsPersistent() const { return ::nt::GetTopicPersistent(m_handle); }
/**
* Make the server retain the topic even when there are no publishers.
*
* @param retained True for retained, false for not retained.
*/
void SetRetained(bool retained);
void SetRetained(bool retained) {
::nt::SetTopicRetained(m_handle, retained);
}
/**
* Returns whether the topic is retained by server when there are no
@@ -100,7 +108,7 @@ class Topic {
*
* @return True if the topic is retained.
*/
bool IsRetained() const;
bool IsRetained() const { return ::nt::GetTopicRetained(m_handle); }
/**
* Allow storage of the topic's last value, allowing the value to be read (and
@@ -108,21 +116,21 @@ class Topic {
*
* @param cached True for cached, false for not cached.
*/
void SetCached(bool cached);
void SetCached(bool cached) { ::nt::SetTopicCached(m_handle, cached); }
/**
* Returns whether the topic's last value is stored.
*
* @return True if the topic is cached.
*/
bool IsCached() const;
bool IsCached() const { return ::nt::GetTopicCached(m_handle); }
/**
* Determines if the topic is currently being published.
*
* @return True if the topic exists, false otherwise.
*/
bool Exists() const;
bool Exists() const { return nt::GetTopicExists(m_handle); }
/**
* Gets the current value of a property (as a JSON object).
@@ -145,7 +153,9 @@ class Topic {
*
* @param name property name
*/
void DeleteProperty(std::string_view name);
void DeleteProperty(std::string_view name) {
::nt::DeleteTopicProperty(m_handle, name);
}
/**
* Gets all topic properties as a JSON object. Each key in the object
@@ -164,14 +174,16 @@ class Topic {
* @param properties JSON object with keys to add/update/delete
* @return False if properties is not an object
*/
bool SetProperties(const wpi::json& properties);
bool SetProperties(const wpi::json& properties) {
return ::nt::SetTopicProperties(m_handle, properties);
}
/**
* Gets combined information about the topic.
*
* @return Topic information
*/
TopicInfo GetInfo() const;
TopicInfo GetInfo() const { return ::nt::GetTopicInfo(m_handle); }
/**
* Create a new subscriber to the topic.
@@ -308,13 +320,23 @@ class Topic {
/** NetworkTables subscriber. */
class Subscriber {
public:
virtual ~Subscriber();
virtual ~Subscriber() { ::nt::Release(m_subHandle); }
Subscriber(const Subscriber&) = delete;
Subscriber& operator=(const Subscriber&) = delete;
Subscriber(Subscriber&&);
Subscriber& operator=(Subscriber&&);
Subscriber(Subscriber&& rhs) : m_subHandle{rhs.m_subHandle} {
rhs.m_subHandle = 0;
}
Subscriber& operator=(Subscriber&& rhs) {
if (m_subHandle != 0) {
::nt::Release(m_subHandle);
}
m_subHandle = rhs.m_subHandle;
rhs.m_subHandle = 0;
return *this;
}
/**
* Determines if the native handle is valid.
@@ -335,7 +357,7 @@ class Subscriber {
*
* @return True if the topic exists, false otherwise.
*/
bool Exists() const;
bool Exists() const { return nt::GetTopicExists(m_subHandle); }
/**
* Gets the last time the value was changed.
@@ -344,32 +366,49 @@ class Subscriber {
*
* @return Topic last change time
*/
int64_t GetLastChange() const;
int64_t GetLastChange() const {
return ::nt::GetEntryLastChange(m_subHandle);
}
/**
* Gets the subscribed-to topic.
*
* @return Topic
*/
Topic GetTopic() const;
Topic GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
protected:
Subscriber() = default;
explicit Subscriber(NT_Subscriber handle) : m_subHandle{handle} {}
NT_Subscriber m_subHandle{0};
private:
void anchor();
};
/** NetworkTables publisher. */
class Publisher {
public:
virtual ~Publisher();
virtual ~Publisher() { ::nt::Release(m_pubHandle); }
Publisher(const Publisher&) = delete;
Publisher& operator=(const Publisher&) = delete;
Publisher(Publisher&&);
Publisher& operator=(Publisher&&);
Publisher(Publisher&& rhs) : m_pubHandle{rhs.m_pubHandle} {
rhs.m_pubHandle = 0;
}
Publisher& operator=(Publisher&& rhs) {
if (m_pubHandle != 0) {
::nt::Release(m_pubHandle);
}
m_pubHandle = rhs.m_pubHandle;
rhs.m_pubHandle = 0;
return *this;
}
/**
* Determines if the native handle is valid.
@@ -390,7 +429,9 @@ class Publisher {
*
* @return Topic
*/
Topic GetTopic() const;
Topic GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_pubHandle)};
}
protected:
Publisher() = default;
@@ -398,8 +439,9 @@ class Publisher {
/// NetworkTables handle.
NT_Publisher m_pubHandle{0};
private:
void anchor();
};
} // namespace nt
#include "networktables/Topic.inc"

View File

@@ -1,118 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <string>
#include "networktables/NetworkTableType.h"
#include "networktables/Topic.h"
#include "ntcore_c.h"
#include "ntcore_cpp.h"
namespace nt {
inline std::string Topic::GetName() const {
return ::nt::GetTopicName(m_handle);
}
inline NetworkTableType Topic::GetType() const {
return static_cast<NetworkTableType>(::nt::GetTopicType(m_handle));
}
inline std::string Topic::GetTypeString() const {
return ::nt::GetTopicTypeString(m_handle);
}
inline void Topic::SetPersistent(bool persistent) {
::nt::SetTopicPersistent(m_handle, persistent);
}
inline bool Topic::IsPersistent() const {
return ::nt::GetTopicPersistent(m_handle);
}
inline void Topic::SetRetained(bool retained) {
::nt::SetTopicRetained(m_handle, retained);
}
inline bool Topic::IsRetained() const {
return ::nt::GetTopicRetained(m_handle);
}
inline void Topic::SetCached(bool cached) {
::nt::SetTopicCached(m_handle, cached);
}
inline bool Topic::IsCached() const {
return ::nt::GetTopicCached(m_handle);
}
inline bool Topic::Exists() const {
return nt::GetTopicExists(m_handle);
}
inline void Topic::DeleteProperty(std::string_view name) {
::nt::DeleteTopicProperty(m_handle, name);
}
inline bool Topic::SetProperties(const wpi::json& properties) {
return ::nt::SetTopicProperties(m_handle, properties);
}
inline TopicInfo Topic::GetInfo() const {
return ::nt::GetTopicInfo(m_handle);
}
inline Subscriber::~Subscriber() {
::nt::Release(m_subHandle);
}
inline Subscriber::Subscriber(Subscriber&& rhs) : m_subHandle{rhs.m_subHandle} {
rhs.m_subHandle = 0;
}
inline Subscriber& Subscriber::operator=(Subscriber&& rhs) {
if (m_subHandle != 0) {
::nt::Release(m_subHandle);
}
m_subHandle = rhs.m_subHandle;
rhs.m_subHandle = 0;
return *this;
}
inline bool Subscriber::Exists() const {
return nt::GetTopicExists(m_subHandle);
}
inline int64_t Subscriber::GetLastChange() const {
return ::nt::GetEntryLastChange(m_subHandle);
}
inline Topic Subscriber::GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_subHandle)};
}
inline Publisher::~Publisher() {
::nt::Release(m_pubHandle);
}
inline Publisher::Publisher(Publisher&& rhs) : m_pubHandle{rhs.m_pubHandle} {
rhs.m_pubHandle = 0;
}
inline Publisher& Publisher::operator=(Publisher&& rhs) {
if (m_pubHandle != 0) {
::nt::Release(m_pubHandle);
}
m_pubHandle = rhs.m_pubHandle;
rhs.m_pubHandle = 0;
return *this;
}
inline Topic Publisher::GetTopic() const {
return Topic{::nt::GetTopicFromHandle(m_pubHandle)};
}
} // namespace nt

View File

@@ -6,11 +6,10 @@
#include <stdint.h>
#include <span>
#include <string_view>
#include <vector>
#include <wpi/json_fwd.h>
#include <wpi/json.h>
#include "networktables/Topic.h"
#include "ntcore_cpp.h"
@@ -69,7 +68,8 @@ class UnitSubscriber : public Subscriber {
* @param handle Native handle
* @param defaultValue Default value
*/
UnitSubscriber(NT_Subscriber handle, ParamType defaultValue);
UnitSubscriber(NT_Subscriber handle, ParamType defaultValue)
: Subscriber{handle}, m_defaultValue{defaultValue} {}
/**
* Get the last published value.
@@ -77,7 +77,7 @@ class UnitSubscriber : public Subscriber {
*
* @return value
*/
ValueType Get() const;
ValueType Get() const { return Get(m_defaultValue); }
/**
* Get the last published value.
@@ -86,7 +86,9 @@ class UnitSubscriber : public Subscriber {
* @param defaultValue default value to return if no value has been published
* @return value
*/
ValueType Get(ParamType defaultValue) const;
ValueType Get(ParamType defaultValue) const {
return T{::nt::GetDouble(m_subHandle, defaultValue.value())};
}
/**
* Get the last published value along with its timestamp
@@ -95,7 +97,7 @@ class UnitSubscriber : public Subscriber {
*
* @return timestamped value
*/
TimestampedValueType GetAtomic() const;
TimestampedValueType GetAtomic() const { return GetAtomic(m_defaultValue); }
/**
* Get the last published value along with its timestamp.
@@ -105,7 +107,10 @@ class UnitSubscriber : public Subscriber {
* @param defaultValue default value to return if no value has been published
* @return timestamped value
*/
TimestampedValueType GetAtomic(ParamType defaultValue) const;
TimestampedValueType GetAtomic(ParamType defaultValue) const {
auto doubleVal = ::nt::GetAtomicDouble(m_subHandle, defaultValue.value());
return {doubleVal.time, doubleVal.serverTime, doubleVal.value};
}
/**
* Get an array of all value changes since the last call to ReadQueue.
@@ -117,14 +122,23 @@ class UnitSubscriber : public Subscriber {
* @return Array of timestamped values; empty array if no new changes have
* been published since the previous call.
*/
std::vector<TimestampedValueType> ReadQueue();
std::vector<TimestampedValueType> ReadQueue() {
std::vector<TimestampedUnit<T>> vals;
auto doubleVals = ::nt::ReadQueueDouble(m_subHandle);
vals.reserve(doubleVals.size());
for (auto&& val : doubleVals) {
vals.emplace_back(val.time, val.serverTime, val.value);
}
}
/**
* Get the corresponding topic.
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(m_subHandle)};
}
private:
ValueType m_defaultValue;
@@ -152,7 +166,7 @@ class UnitPublisher : public Publisher {
*
* @param handle Native handle
*/
explicit UnitPublisher(NT_Publisher handle);
explicit UnitPublisher(NT_Publisher handle) : Publisher{handle} {}
/**
* Publish a new value.
@@ -160,7 +174,9 @@ class UnitPublisher : public Publisher {
* @param value value to publish
* @param time timestamp; 0 indicates current NT time should be used
*/
void Set(ParamType value, int64_t time = 0);
void Set(ParamType value, int64_t time = 0) {
::nt::SetDouble(m_pubHandle, value.value(), time);
}
/**
* Publish a default value.
@@ -169,14 +185,18 @@ class UnitPublisher : public Publisher {
*
* @param value value
*/
void SetDefault(ParamType value);
void SetDefault(ParamType value) {
::nt::SetDefaultDouble(m_pubHandle, value.value());
}
/**
* Get the corresponding topic.
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(m_pubHandle)};
}
};
/**
@@ -206,7 +226,8 @@ class UnitEntry final : public UnitSubscriber<T>, public UnitPublisher<T> {
* @param handle Native handle
* @param defaultValue Default value
*/
UnitEntry(NT_Entry handle, ParamType defaultValue);
UnitEntry(NT_Entry handle, ParamType defaultValue)
: UnitSubscriber<T>{handle, defaultValue}, UnitPublisher<T>{handle} {}
/**
* Determines if the native handle is valid.
@@ -227,12 +248,14 @@ class UnitEntry final : public UnitSubscriber<T>, public UnitPublisher<T> {
*
* @return Topic
*/
TopicType GetTopic() const;
TopicType GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(this->m_subHandle)};
}
/**
* Stops publishing the entry if it's published.
*/
void Unpublish();
void Unpublish() { ::nt::Unpublish(this->m_pubHandle); }
};
/**
@@ -276,7 +299,7 @@ class UnitTopic final : public Topic {
*
* @return True if unit matches, false if not matching or topic not published.
*/
bool IsMatchingUnit() const;
bool IsMatchingUnit() const { return GetProperty("unit") == T{}.name(); }
/**
* Create a new subscriber to the topic.
@@ -296,7 +319,10 @@ class UnitTopic final : public Topic {
[[nodiscard]]
SubscriberType Subscribe(
ParamType defaultValue,
const PubSubOptions& options = kDefaultPubSubOptions);
const PubSubOptions& options = kDefaultPubSubOptions) {
return UnitSubscriber<T>{
::nt::Subscribe(m_handle, NT_DOUBLE, "double", options), defaultValue};
}
/**
* Create a new subscriber to the topic, with specific type string.
@@ -317,7 +343,11 @@ class UnitTopic final : public Topic {
[[nodiscard]]
SubscriberType SubscribeEx(
std::string_view typeString, ParamType defaultValue,
const PubSubOptions& options = kDefaultPubSubOptions);
const PubSubOptions& options = kDefaultPubSubOptions) {
return UnitSubscriber<T>{
::nt::Subscribe(m_handle, NT_DOUBLE, typeString, options),
defaultValue};
}
/**
* Create a new publisher to the topic.
@@ -335,7 +365,10 @@ class UnitTopic final : public Topic {
* @return publisher
*/
[[nodiscard]]
PublisherType Publish(const PubSubOptions& options = kDefaultPubSubOptions);
PublisherType Publish(const PubSubOptions& options = kDefaultPubSubOptions) {
return UnitPublisher<T>{::nt::PublishEx(m_handle, NT_DOUBLE, "double",
{{"unit", T{}.name()}}, options)};
}
/**
* Create a new publisher to the topic, with type string and initial
@@ -356,9 +389,14 @@ class UnitTopic final : public Topic {
* @return publisher
*/
[[nodiscard]]
PublisherType PublishEx(std::string_view typeString,
const wpi::json& properties,
const PubSubOptions& options = kDefaultPubSubOptions);
PublisherType PublishEx(
std::string_view typeString, const wpi::json& properties,
const PubSubOptions& options = kDefaultPubSubOptions) {
wpi::json props = properties;
props["unit"] = T{}.name();
return UnitPublisher<T>{
::nt::PublishEx(m_handle, NT_DOUBLE, typeString, props, options)};
}
/**
* Create a new entry for the topic.
@@ -382,7 +420,10 @@ class UnitTopic final : public Topic {
*/
[[nodiscard]]
EntryType GetEntry(ParamType defaultValue,
const PubSubOptions& options = kDefaultPubSubOptions);
const PubSubOptions& options = kDefaultPubSubOptions) {
return UnitEntry<T>{::nt::GetEntry(m_handle, NT_DOUBLE, "double", options),
defaultValue};
}
/**
* Create a new entry for the topic, with specific type string.
@@ -407,9 +448,10 @@ class UnitTopic final : public Topic {
*/
[[nodiscard]]
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
const PubSubOptions& options = kDefaultPubSubOptions);
const PubSubOptions& options = kDefaultPubSubOptions) {
return UnitEntry<T>{
::nt::GetEntry(m_handle, NT_DOUBLE, typeString, options), defaultValue};
}
};
} // namespace nt
#include "networktables/UnitTopic.inc"

View File

@@ -1,140 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <vector>
#include <wpi/json.h>
#include "networktables/NetworkTableType.h"
#include "networktables/UnitTopic.h"
#include "ntcore_cpp.h"
namespace nt {
template <typename T>
inline UnitSubscriber<T>::UnitSubscriber(NT_Subscriber handle, T defaultValue)
: Subscriber{handle}, m_defaultValue{defaultValue} {}
template <typename T>
inline T UnitSubscriber<T>::Get() const {
return Get(m_defaultValue);
}
template <typename T>
inline T UnitSubscriber<T>::Get(T defaultValue) const {
return T{::nt::GetDouble(m_subHandle, defaultValue.value())};
}
template <typename T>
inline TimestampedUnit<T> UnitSubscriber<T>::GetAtomic() const {
return GetAtomic(m_defaultValue);
}
template <typename T>
inline TimestampedUnit<T> UnitSubscriber<T>::GetAtomic(T defaultValue) const {
auto doubleVal = ::nt::GetAtomicDouble(m_subHandle, defaultValue.value());
return {doubleVal.time, doubleVal.serverTime, doubleVal.value};
}
template <typename T>
inline std::vector<TimestampedUnit<T>> UnitSubscriber<T>::ReadQueue() {
std::vector<TimestampedUnit<T>> vals;
auto doubleVals = ::nt::ReadQueueDouble(m_subHandle);
vals.reserve(doubleVals.size());
for (auto&& val : doubleVals) {
vals.emplace_back(val.time, val.serverTime, val.value);
}
}
template <typename T>
inline UnitTopic<T> UnitSubscriber<T>::GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(m_subHandle)};
}
template <typename T>
inline UnitPublisher<T>::UnitPublisher(NT_Publisher handle)
: Publisher{handle} {}
template <typename T>
inline void UnitPublisher<T>::Set(T value, int64_t time) {
::nt::SetDouble(m_pubHandle, value.value(), time);
}
template <typename T>
inline void UnitPublisher<T>::SetDefault(T value) {
::nt::SetDefaultDouble(m_pubHandle, value.value());
}
template <typename T>
inline UnitTopic<T> UnitPublisher<T>::GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(m_pubHandle)};
}
template <typename T>
inline UnitEntry<T>::UnitEntry(NT_Entry handle, T defaultValue)
: UnitSubscriber<T>{handle, defaultValue}, UnitPublisher<T>{handle} {}
template <typename T>
inline UnitTopic<T> UnitEntry<T>::GetTopic() const {
return UnitTopic<T>{::nt::GetTopicFromHandle(this->m_subHandle)};
}
template <typename T>
inline void UnitEntry<T>::Unpublish() {
::nt::Unpublish(this->m_pubHandle);
}
template <typename T>
inline bool UnitTopic<T>::IsMatchingUnit() const {
return GetProperty("unit") == T{}.name();
}
template <typename T>
inline UnitSubscriber<T> UnitTopic<T>::Subscribe(T defaultValue,
const PubSubOptions& options) {
return UnitSubscriber<T>{
::nt::Subscribe(m_handle, NT_DOUBLE, "double", options), defaultValue};
}
template <typename T>
inline UnitSubscriber<T> UnitTopic<T>::SubscribeEx(
std::string_view typeString, T defaultValue, const PubSubOptions& options) {
return UnitSubscriber<T>{
::nt::Subscribe(m_handle, NT_DOUBLE, typeString, options), defaultValue};
}
template <typename T>
inline UnitPublisher<T> UnitTopic<T>::Publish(const PubSubOptions& options) {
return UnitPublisher<T>{::nt::PublishEx(m_handle, NT_DOUBLE, "double",
{{"unit", T{}.name()}}, options)};
}
template <typename T>
inline UnitPublisher<T> UnitTopic<T>::PublishEx(std::string_view typeString,
const wpi::json& properties,
const PubSubOptions& options) {
wpi::json props = properties;
props["unit"] = T{}.name();
return UnitPublisher<T>{
::nt::PublishEx(m_handle, NT_DOUBLE, typeString, props, options)};
}
template <typename T>
inline UnitEntry<T> UnitTopic<T>::GetEntry(T defaultValue,
const PubSubOptions& options) {
return UnitEntry<T>{::nt::GetEntry(m_handle, NT_DOUBLE, "double", options),
defaultValue};
}
template <typename T>
inline UnitEntry<T> UnitTopic<T>::GetEntryEx(std::string_view typeString,
T defaultValue,
const PubSubOptions& options) {
return UnitEntry<T>{::nt::GetEntry(m_handle, NT_DOUBLE, typeString, options),
defaultValue};
}
} // namespace nt