WPILibC++  2020.3.2
SendableBuilderImpl.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 #include <networktables/NetworkTable.h>
17 #include <networktables/NetworkTableEntry.h>
18 #include <networktables/NetworkTableValue.h>
19 #include <wpi/ArrayRef.h>
20 #include <wpi/SmallVector.h>
21 #include <wpi/Twine.h>
22 
23 #include "frc/smartdashboard/SendableBuilder.h"
24 
25 namespace frc {
26 
28  public:
29  SendableBuilderImpl() = default;
30  ~SendableBuilderImpl() override = default;
31 
33  SendableBuilderImpl& operator=(SendableBuilderImpl&&) = default;
34 
40  void SetTable(std::shared_ptr<nt::NetworkTable> table);
41 
46  std::shared_ptr<nt::NetworkTable> GetTable();
47 
52  bool HasTable() const;
53 
58  bool IsActuator() const;
59 
63  void UpdateTable();
64 
68  void StartListeners();
69 
73  void StopListeners();
74 
79  void StartLiveWindowMode();
80 
85  void StopLiveWindowMode();
86 
90  void ClearProperties();
91 
92  void SetSmartDashboardType(const wpi::Twine& type) override;
93  void SetActuator(bool value) override;
94  void SetSafeState(std::function<void()> func) override;
95  void SetUpdateTable(std::function<void()> func) override;
96  nt::NetworkTableEntry GetEntry(const wpi::Twine& key) override;
97 
98  void AddBooleanProperty(const wpi::Twine& key, std::function<bool()> getter,
99  std::function<void(bool)> setter) override;
100 
101  void AddDoubleProperty(const wpi::Twine& key, std::function<double()> getter,
102  std::function<void(double)> setter) override;
103 
104  void AddStringProperty(const wpi::Twine& key,
105  std::function<std::string()> getter,
106  std::function<void(wpi::StringRef)> setter) override;
107 
109  const wpi::Twine& key, std::function<std::vector<int>()> getter,
110  std::function<void(wpi::ArrayRef<int>)> setter) override;
111 
113  const wpi::Twine& key, std::function<std::vector<double>()> getter,
114  std::function<void(wpi::ArrayRef<double>)> setter) override;
115 
117  const wpi::Twine& key, std::function<std::vector<std::string>()> getter,
118  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
119 
120  void AddRawProperty(const wpi::Twine& key,
121  std::function<std::string()> getter,
122  std::function<void(wpi::StringRef)> setter) override;
123 
124  void AddValueProperty(
125  const wpi::Twine& key, std::function<std::shared_ptr<nt::Value>()> getter,
126  std::function<void(std::shared_ptr<nt::Value>)> setter) override;
127 
129  const wpi::Twine& key,
130  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
131  std::function<void(wpi::StringRef)> setter) override;
132 
134  const wpi::Twine& key,
135  std::function<wpi::ArrayRef<int>(wpi::SmallVectorImpl<int>& buf)> getter,
136  std::function<void(wpi::ArrayRef<int>)> setter) override;
137 
139  const wpi::Twine& key,
141  getter,
142  std::function<void(wpi::ArrayRef<double>)> setter) override;
143 
145  const wpi::Twine& key,
146  std::function<
148  getter,
149  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
150 
151  void AddSmallRawProperty(
152  const wpi::Twine& key,
153  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
154  std::function<void(wpi::StringRef)> setter) override;
155 
156  private:
157  struct Property {
158  Property(nt::NetworkTable& table, const wpi::Twine& key)
159  : entry(table.GetEntry(key)) {}
160 
161  Property(const Property&) = delete;
162  Property& operator=(const Property&) = delete;
163 
164  Property(Property&& other) noexcept
165  : entry(other.entry),
166  listener(other.listener),
167  update(std::move(other.update)),
168  createListener(std::move(other.createListener)) {
169  other.entry = nt::NetworkTableEntry();
170  other.listener = 0;
171  }
172 
173  Property& operator=(Property&& other) noexcept {
174  entry = other.entry;
175  listener = other.listener;
176  other.entry = nt::NetworkTableEntry();
177  other.listener = 0;
178  update = std::move(other.update);
179  createListener = std::move(other.createListener);
180  return *this;
181  }
182 
183  ~Property() { StopListener(); }
184 
185  void StartListener() {
186  if (entry && listener == 0 && createListener)
187  listener = createListener(entry);
188  }
189 
190  void StopListener() {
191  if (entry && listener != 0) {
192  entry.RemoveListener(listener);
193  listener = 0;
194  }
195  }
196 
197  nt::NetworkTableEntry entry;
198  NT_EntryListener listener = 0;
199  std::function<void(nt::NetworkTableEntry entry, uint64_t time)> update;
200  std::function<NT_EntryListener(nt::NetworkTableEntry entry)> createListener;
201  };
202 
203  std::vector<Property> m_properties;
204  std::function<void()> m_safeState;
205  std::vector<std::function<void()>> m_updateTables;
206  std::shared_ptr<nt::NetworkTable> m_table;
207  nt::NetworkTableEntry m_controllableEntry;
208  bool m_actuator = false;
209 };
210 
211 } // namespace frc
nt::NetworkTable
A network table that knows its subtable path.
Definition: NetworkTable.h:54
frc::SendableBuilderImpl::ClearProperties
void ClearProperties()
Clear properties.
frc::SendableBuilderImpl::AddSmallStringArrayProperty
void AddSmallStringArrayProperty(const wpi::Twine &key, std::function< wpi::ArrayRef< std::string >(wpi::SmallVectorImpl< std::string > &buf)> getter, std::function< void(wpi::ArrayRef< std::string >)> setter) override
Add a string array property (SmallVector form).
frc::SendableBuilderImpl::AddSmallRawProperty
void AddSmallRawProperty(const wpi::Twine &key, std::function< wpi::StringRef(wpi::SmallVectorImpl< char > &buf)> getter, std::function< void(wpi::StringRef)> setter) override
Add a raw property (SmallVector form).
frc::SendableBuilderImpl
Definition: SendableBuilderImpl.h:27
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::SmallVectorImpl< char >
frc::SendableBuilderImpl::AddSmallDoubleArrayProperty
void AddSmallDoubleArrayProperty(const wpi::Twine &key, std::function< wpi::ArrayRef< double >(wpi::SmallVectorImpl< double > &buf)> getter, std::function< void(wpi::ArrayRef< double >)> setter) override
Add a double array property (SmallVector form).
frc::SendableBuilderImpl::SetActuator
void SetActuator(bool value) override
Set a flag indicating if this sendable should be treated as an actuator.
frc::SendableBuilderImpl::IsActuator
bool IsActuator() const
Return whether this sendable should be treated as an actuator.
frc::SendableBuilderImpl::GetEntry
nt::NetworkTableEntry GetEntry(const wpi::Twine &key) override
Add a property without getters or setters.
nt::NetworkTableEntry
NetworkTables Entry.
Definition: NetworkTableEntry.h:39
frc::SendableBuilderImpl::StartListeners
void StartListeners()
Hook setters for all properties.
frc::SendableBuilderImpl::AddDoubleArrayProperty
void AddDoubleArrayProperty(const wpi::Twine &key, std::function< std::vector< double >()> getter, std::function< void(wpi::ArrayRef< double >)> setter) override
Add a double array property.
frc::SendableBuilderImpl::AddRawProperty
void AddRawProperty(const wpi::Twine &key, std::function< std::string()> getter, std::function< void(wpi::StringRef)> setter) override
Add a raw property.
frc::SendableBuilderImpl::StartLiveWindowMode
void StartLiveWindowMode()
Start LiveWindow mode by hooking the setters for all properties.
frc::SendableBuilderImpl::SetSmartDashboardType
void SetSmartDashboardType(const wpi::Twine &type) override
Set the string representation of the named data type that will be used by the smart dashboard for thi...
frc::SendableBuilderImpl::SetTable
void SetTable(std::shared_ptr< nt::NetworkTable > table)
Set the network table.
frc::SendableBuilderImpl::StopLiveWindowMode
void StopLiveWindowMode()
Stop LiveWindow mode by unhooking the setters for all properties.
frc::SendableBuilderImpl::HasTable
bool HasTable() const
Return whether this sendable has an associated table.
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::SendableBuilderImpl::SetUpdateTable
void SetUpdateTable(std::function< void()> func) override
Set the function that should be called to update the network table for things other than properties.
frc::SendableBuilderImpl::AddSmallStringProperty
void AddSmallStringProperty(const wpi::Twine &key, std::function< wpi::StringRef(wpi::SmallVectorImpl< char > &buf)> getter, std::function< void(wpi::StringRef)> setter) override
Add a string property (SmallString form).
frc::SendableBuilderImpl::AddStringProperty
void AddStringProperty(const wpi::Twine &key, std::function< std::string()> getter, std::function< void(wpi::StringRef)> setter) override
Add a string property.
frc::SendableBuilderImpl::StopListeners
void StopListeners()
Unhook setters for all properties.
frc::SendableBuilderImpl::AddBooleanArrayProperty
void AddBooleanArrayProperty(const wpi::Twine &key, std::function< std::vector< int >()> getter, std::function< void(wpi::ArrayRef< int >)> setter) override
Add a boolean array property.
frc::SendableBuilderImpl::SetSafeState
void SetSafeState(std::function< void()> func) override
Set the function that should be called to set the Sendable into a safe state.
frc::SendableBuilderImpl::UpdateTable
void UpdateTable()
Update the network table values by calling the getters for all properties.
frc::SendableBuilderImpl::AddBooleanProperty
void AddBooleanProperty(const wpi::Twine &key, std::function< bool()> getter, std::function< void(bool)> setter) override
Add a boolean property.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::SendableBuilderImpl::AddStringArrayProperty
void AddStringArrayProperty(const wpi::Twine &key, std::function< std::vector< std::string >()> getter, std::function< void(wpi::ArrayRef< std::string >)> setter) override
Add a string array property.
frc::SendableBuilderImpl::AddSmallBooleanArrayProperty
void AddSmallBooleanArrayProperty(const wpi::Twine &key, std::function< wpi::ArrayRef< int >(wpi::SmallVectorImpl< int > &buf)> getter, std::function< void(wpi::ArrayRef< int >)> setter) override
Add a boolean array property (SmallVector form).
frc::SendableBuilderImpl::AddValueProperty
void AddValueProperty(const wpi::Twine &key, std::function< std::shared_ptr< nt::Value >()> getter, std::function< void(std::shared_ptr< nt::Value >)> setter) override
Add a NetworkTableValue property.
frc::SendableBuilderImpl::AddDoubleProperty
void AddDoubleProperty(const wpi::Twine &key, std::function< double()> getter, std::function< void(double)> setter) override
Add a double property.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::SendableBuilderImpl::GetTable
std::shared_ptr< nt::NetworkTable > GetTable()
Get the network table.
frc::SendableBuilder
Definition: SendableBuilder.h:23