[ntcore] NetworkTables 4 (#3217)

This commit is contained in:
Peter Johnson
2022-10-08 10:01:31 -07:00
committed by GitHub
parent 90cfa00115
commit 77301b126c
380 changed files with 34573 additions and 22095 deletions

View File

@@ -11,10 +11,11 @@
#include <utility>
#include <vector>
#include <networktables/BooleanTopic.h>
#include <networktables/NTSendableBuilder.h>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTableValue.h>
#include <networktables/StringTopic.h>
#include <wpi/FunctionExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/span.h>
@@ -54,7 +55,8 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
bool IsActuator() const;
/**
* Update the network table values by calling the getters for all properties.
* Synchronize with network table values by calling the getters for all
* properties and setters when the network table value has changed.
*/
void Update() override;
@@ -88,12 +90,18 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
void SetSmartDashboardType(std::string_view type) override;
void SetActuator(bool value) override;
void SetSafeState(std::function<void()> func) override;
void SetUpdateTable(std::function<void()> func) override;
nt::NetworkTableEntry GetEntry(std::string_view key) override;
void SetUpdateTable(wpi::unique_function<void()> func) override;
nt::Topic GetTopic(std::string_view key) override;
void AddBooleanProperty(std::string_view key, std::function<bool()> getter,
std::function<void(bool)> setter) override;
void AddIntegerProperty(std::string_view key, std::function<int64_t()> getter,
std::function<void(int64_t)> setter) override;
void AddFloatProperty(std::string_view key, std::function<float()> getter,
std::function<void(float)> setter) override;
void AddDoubleProperty(std::string_view key, std::function<double()> getter,
std::function<void(double)> setter) override;
@@ -105,6 +113,14 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::string_view key, std::function<std::vector<int>()> getter,
std::function<void(wpi::span<const int>)> setter) override;
void AddIntegerArrayProperty(
std::string_view key, std::function<std::vector<int64_t>()> getter,
std::function<void(wpi::span<const int64_t>)> setter) override;
void AddFloatArrayProperty(
std::string_view key, std::function<std::vector<float>()> getter,
std::function<void(wpi::span<const float>)> setter) override;
void AddDoubleArrayProperty(
std::string_view key, std::function<std::vector<double>()> getter,
std::function<void(wpi::span<const double>)> setter) override;
@@ -113,12 +129,10 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::string_view key, std::function<std::vector<std::string>()> getter,
std::function<void(wpi::span<const std::string>)> setter) override;
void AddRawProperty(std::string_view key, std::function<std::string()> getter,
std::function<void(std::string_view)> setter) override;
void AddValueProperty(
std::string_view key, std::function<std::shared_ptr<nt::Value>()> getter,
std::function<void(std::shared_ptr<nt::Value>)> setter) override;
void AddRawProperty(
std::string_view key, std::string_view typeString,
std::function<std::vector<uint8_t>()> getter,
std::function<void(wpi::span<const uint8_t>)> setter) override;
void AddSmallStringProperty(
std::string_view key,
@@ -131,6 +145,19 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
getter,
std::function<void(wpi::span<const int>)> setter) override;
void AddSmallIntegerArrayProperty(
std::string_view key,
std::function<
wpi::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
getter,
std::function<void(wpi::span<const int64_t>)> setter) override;
void AddSmallFloatArrayProperty(
std::string_view key,
std::function<wpi::span<const float>(wpi::SmallVectorImpl<float>& buf)>
getter,
std::function<void(wpi::span<const float>)> setter) override;
void AddSmallDoubleArrayProperty(
std::string_view key,
std::function<wpi::span<const double>(wpi::SmallVectorImpl<double>& buf)>
@@ -145,64 +172,46 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::function<void(wpi::span<const std::string>)> setter) override;
void AddSmallRawProperty(
std::string_view key,
std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(std::string_view)> setter) override;
std::string_view key, std::string_view typeString,
std::function<wpi::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
getter,
std::function<void(wpi::span<const uint8_t>)> setter) override;
private:
struct Property {
Property(nt::NetworkTable& table, std::string_view key)
: entry(table.GetEntry(key)) {}
Property(const Property&) = delete;
Property& operator=(const Property&) = delete;
Property(Property&& other) noexcept
: entry(other.entry),
listener(other.listener),
update(std::move(other.update)),
createListener(std::move(other.createListener)) {
other.entry = nt::NetworkTableEntry();
other.listener = 0;
}
Property& operator=(Property&& other) noexcept {
entry = other.entry;
listener = other.listener;
other.entry = nt::NetworkTableEntry();
other.listener = 0;
update = std::move(other.update);
createListener = std::move(other.createListener);
return *this;
}
~Property() { StopListener(); }
void StartListener() {
if (entry && listener == 0 && createListener) {
listener = createListener(entry);
}
}
void StopListener() {
if (entry && listener != 0) {
entry.RemoveListener(listener);
listener = 0;
}
}
nt::NetworkTableEntry entry;
NT_EntryListener listener = 0;
std::function<void(nt::NetworkTableEntry entry, uint64_t time)> update;
std::function<NT_EntryListener(nt::NetworkTableEntry entry)> createListener;
virtual ~Property() = default;
virtual void Update(bool controllable, int64_t time) = 0;
};
std::vector<Property> m_properties;
template <typename Topic>
struct PropertyImpl : public Property {
void Update(bool controllable, int64_t time) override;
using Publisher = typename Topic::PublisherType;
using Subscriber = typename Topic::SubscriberType;
Publisher pub;
Subscriber sub;
std::function<void(Publisher& pub, int64_t time)> updateNetwork;
std::function<void(Subscriber& sub)> updateLocal;
};
template <typename Topic, typename Getter, typename Setter>
void AddPropertyImpl(Topic topic, Getter getter, Setter setter);
template <typename T, size_t Size, typename Topic, typename Getter,
typename Setter>
void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter);
std::vector<std::unique_ptr<Property>> m_properties;
std::function<void()> m_safeState;
std::vector<std::function<void()>> m_updateTables;
std::vector<wpi::unique_function<void()>> m_updateTables;
std::shared_ptr<nt::NetworkTable> m_table;
nt::NetworkTableEntry m_controllableEntry;
bool m_controllable = false;
bool m_actuator = false;
nt::BooleanPublisher m_controllablePublisher;
nt::StringPublisher m_typePublisher;
nt::BooleanPublisher m_actuatorPublisher;
};
} // namespace frc