WPILibC++  2018.4.1-1223-g36000dd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SendableBuilderImpl.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2017-2018 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 IsActuator() const;
53 
57  void UpdateTable();
58 
62  void StartListeners();
63 
67  void StopListeners();
68 
73  void StartLiveWindowMode();
74 
79  void StopLiveWindowMode();
80 
81  void SetSmartDashboardType(const wpi::Twine& type) override;
82  void SetActuator(bool value) override;
83  void SetSafeState(std::function<void()> func) override;
84  void SetUpdateTable(std::function<void()> func) override;
85  nt::NetworkTableEntry GetEntry(const wpi::Twine& key) override;
86 
87  void AddBooleanProperty(const wpi::Twine& key, std::function<bool()> getter,
88  std::function<void(bool)> setter) override;
89 
90  void AddDoubleProperty(const wpi::Twine& key, std::function<double()> getter,
91  std::function<void(double)> setter) override;
92 
93  void AddStringProperty(const wpi::Twine& key,
94  std::function<std::string()> getter,
95  std::function<void(wpi::StringRef)> setter) override;
96 
98  const wpi::Twine& key, std::function<std::vector<int>()> getter,
99  std::function<void(wpi::ArrayRef<int>)> setter) override;
100 
102  const wpi::Twine& key, std::function<std::vector<double>()> getter,
103  std::function<void(wpi::ArrayRef<double>)> setter) override;
104 
106  const wpi::Twine& key, std::function<std::vector<std::string>()> getter,
107  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
108 
109  void AddRawProperty(const wpi::Twine& key,
110  std::function<std::string()> getter,
111  std::function<void(wpi::StringRef)> setter) override;
112 
113  void AddValueProperty(
114  const wpi::Twine& key, std::function<std::shared_ptr<nt::Value>()> getter,
115  std::function<void(std::shared_ptr<nt::Value>)> setter) override;
116 
118  const wpi::Twine& key,
119  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
120  std::function<void(wpi::StringRef)> setter) override;
121 
123  const wpi::Twine& key,
124  std::function<wpi::ArrayRef<int>(wpi::SmallVectorImpl<int>& buf)> getter,
125  std::function<void(wpi::ArrayRef<int>)> setter) override;
126 
128  const wpi::Twine& key,
130  getter,
131  std::function<void(wpi::ArrayRef<double>)> setter) override;
132 
134  const wpi::Twine& key,
135  std::function<
137  getter,
138  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
139 
140  void AddSmallRawProperty(
141  const wpi::Twine& key,
142  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
143  std::function<void(wpi::StringRef)> setter) override;
144 
145  private:
146  struct Property {
147  Property(nt::NetworkTable& table, const wpi::Twine& key)
148  : entry(table.GetEntry(key)) {}
149 
150  Property(const Property&) = delete;
151  Property& operator=(const Property&) = delete;
152 
153  Property(Property&& other) noexcept
154  : entry(other.entry),
155  listener(other.listener),
156  update(std::move(other.update)),
157  createListener(std::move(other.createListener)) {
158  other.entry = nt::NetworkTableEntry();
159  other.listener = 0;
160  }
161 
162  Property& operator=(Property&& other) noexcept {
163  entry = other.entry;
164  listener = other.listener;
165  other.entry = nt::NetworkTableEntry();
166  other.listener = 0;
167  update = std::move(other.update);
168  createListener = std::move(other.createListener);
169  return *this;
170  }
171 
172  ~Property() { StopListener(); }
173 
174  void StartListener() {
175  if (entry && listener == 0 && createListener)
176  listener = createListener(entry);
177  }
178 
179  void StopListener() {
180  if (entry && listener != 0) {
181  entry.RemoveListener(listener);
182  listener = 0;
183  }
184  }
185 
186  nt::NetworkTableEntry entry;
187  NT_EntryListener listener = 0;
188  std::function<void(nt::NetworkTableEntry entry, uint64_t time)> update;
189  std::function<NT_EntryListener(nt::NetworkTableEntry entry)> createListener;
190  };
191 
192  std::vector<Property> m_properties;
193  std::function<void()> m_safeState;
194  std::function<void()> m_updateTable;
195  std::shared_ptr<nt::NetworkTable> m_table;
196  nt::NetworkTableEntry m_controllableEntry;
197  bool m_actuator = false;
198 };
199 
200 } // namespace frc
void SetUpdateTable(std::function< void()> func) override
Set the function that should be called to update the network table for things other than properties...
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.
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
void StartListeners()
Hook setters for all properties.
void AddStringProperty(const wpi::Twine &key, std::function< std::string()> getter, std::function< void(wpi::StringRef)> setter) override
Add a string property.
void SetTable(std::shared_ptr< nt::NetworkTable > table)
Set the network table.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
void SetSafeState(std::function< void()> func) override
Set the function that should be called to set the Sendable into a safe state.
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).
void UpdateTable()
Update the network table values by calling the getters for all properties.
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).
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.
nt::NetworkTableEntry GetEntry(const wpi::Twine &key) override
Add a property without getters or setters.
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.
void StopListeners()
Unhook setters for all properties.
Definition: SendableBuilderImpl.h:27
void StartLiveWindowMode()
Start LiveWindow mode by hooking the setters for all properties.
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).
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).
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void AddDoubleProperty(const wpi::Twine &key, std::function< double()> getter, std::function< void(double)> setter) override
Add a double property.
Definition: SendableBuilder.h:23
NetworkTables Entry.
Definition: NetworkTableEntry.h:38
bool IsActuator() const
Return whether this sendable should be treated as an actuator.
A network table that knows its subtable path.
Definition: NetworkTable.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void StopLiveWindowMode()
Stop LiveWindow mode by unhooking the setters for all properties.
void SetActuator(bool value) override
Set a flag indicating if this sendable should be treated as an actuator.
void AddBooleanProperty(const wpi::Twine &key, std::function< bool()> getter, std::function< void(bool)> setter) override
Add a boolean property.
std::shared_ptr< nt::NetworkTable > GetTable()
Get the network table.
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...
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).
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.
void AddRawProperty(const wpi::Twine &key, std::function< std::string()> getter, std::function< void(wpi::StringRef)> setter) override
Add a raw property.