WPILibC++  2018.4.1-20180821013237-1172-g8d8f120
 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;
31  SendableBuilderImpl(SendableBuilderImpl&& other) = default;
32  SendableBuilderImpl& operator=(const SendableBuilderImpl&) = delete;
33  SendableBuilderImpl& operator=(SendableBuilderImpl&& other) = default;
34  ~SendableBuilderImpl() override = default;
35 
41  void SetTable(std::shared_ptr<nt::NetworkTable> table);
42 
47  std::shared_ptr<nt::NetworkTable> GetTable();
48 
53  bool IsActuator() const;
54 
58  void UpdateTable();
59 
63  void StartListeners();
64 
68  void StopListeners();
69 
74  void StartLiveWindowMode();
75 
80  void StopLiveWindowMode();
81 
82  void SetSmartDashboardType(const wpi::Twine& type) override;
83  void SetActuator(bool value) override;
84  void SetSafeState(std::function<void()> func) override;
85  void SetUpdateTable(std::function<void()> func) override;
86  nt::NetworkTableEntry GetEntry(const wpi::Twine& key) override;
87 
88  void AddBooleanProperty(const wpi::Twine& key, std::function<bool()> getter,
89  std::function<void(bool)> setter) override;
90 
91  void AddDoubleProperty(const wpi::Twine& key, std::function<double()> getter,
92  std::function<void(double)> setter) override;
93 
94  void AddStringProperty(const wpi::Twine& key,
95  std::function<std::string()> getter,
96  std::function<void(wpi::StringRef)> setter) override;
97 
99  const wpi::Twine& key, std::function<std::vector<int>()> getter,
100  std::function<void(wpi::ArrayRef<int>)> setter) override;
101 
103  const wpi::Twine& key, std::function<std::vector<double>()> getter,
104  std::function<void(wpi::ArrayRef<double>)> setter) override;
105 
107  const wpi::Twine& key, std::function<std::vector<std::string>()> getter,
108  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
109 
110  void AddRawProperty(const wpi::Twine& key,
111  std::function<std::string()> getter,
112  std::function<void(wpi::StringRef)> setter) override;
113 
114  void AddValueProperty(
115  const wpi::Twine& key, std::function<std::shared_ptr<nt::Value>()> getter,
116  std::function<void(std::shared_ptr<nt::Value>)> setter) override;
117 
119  const wpi::Twine& key,
120  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
121  std::function<void(wpi::StringRef)> setter) override;
122 
124  const wpi::Twine& key,
125  std::function<wpi::ArrayRef<int>(wpi::SmallVectorImpl<int>& buf)> getter,
126  std::function<void(wpi::ArrayRef<int>)> setter) override;
127 
129  const wpi::Twine& key,
131  getter,
132  std::function<void(wpi::ArrayRef<double>)> setter) override;
133 
135  const wpi::Twine& key,
136  std::function<
138  getter,
139  std::function<void(wpi::ArrayRef<std::string>)> setter) override;
140 
141  void AddSmallRawProperty(
142  const wpi::Twine& key,
143  std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
144  std::function<void(wpi::StringRef)> setter) override;
145 
146  private:
147  struct Property {
148  Property(nt::NetworkTable& table, const wpi::Twine& key)
149  : entry(table.GetEntry(key)) {}
150 
151  Property(const Property&) = delete;
152  Property& operator=(const Property&) = delete;
153 
154  Property(Property&& other) noexcept
155  : entry(other.entry),
156  listener(other.listener),
157  update(std::move(other.update)),
158  createListener(std::move(other.createListener)) {
159  other.entry = nt::NetworkTableEntry();
160  other.listener = 0;
161  }
162 
163  Property& operator=(Property&& other) noexcept {
164  entry = other.entry;
165  listener = other.listener;
166  other.entry = nt::NetworkTableEntry();
167  other.listener = 0;
168  update = std::move(other.update);
169  createListener = std::move(other.createListener);
170  return *this;
171  }
172 
173  ~Property() { StopListener(); }
174 
175  void StartListener() {
176  if (entry && listener == 0 && createListener)
177  listener = createListener(entry);
178  }
179 
180  void StopListener() {
181  if (entry && listener != 0) {
182  entry.RemoveListener(listener);
183  listener = 0;
184  }
185  }
186 
187  nt::NetworkTableEntry entry;
188  NT_EntryListener listener = 0;
189  std::function<void(nt::NetworkTableEntry entry, uint64_t time)> update;
190  std::function<NT_EntryListener(nt::NetworkTableEntry entry)> createListener;
191  };
192 
193  std::vector<Property> m_properties;
194  std::function<void()> m_safeState;
195  std::function<void()> m_updateTable;
196  std::shared_ptr<nt::NetworkTable> m_table;
197  nt::NetworkTableEntry m_controllableEntry;
198  bool m_actuator = false;
199 };
200 
201 } // 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.