Fix move handling of C++ Sendable in SmartDashboard and LiveWindow

This commit is contained in:
Peter Johnson
2019-10-17 22:01:31 -07:00
parent d726591ce4
commit 05c25deb7b
6 changed files with 244 additions and 134 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -45,6 +45,12 @@ class SendableBuilderImpl : public SendableBuilder {
*/
std::shared_ptr<nt::NetworkTable> GetTable();
/**
* Return whether this sendable has an associated table.
* @return True if it has a table, false if not.
*/
bool HasTable() const;
/**
* Return whether this sendable should be treated as an actuator.
* @return True if actuator, false if not.
@@ -78,6 +84,11 @@ class SendableBuilderImpl : public SendableBuilder {
*/
void StopLiveWindowMode();
/**
* Clear properties.
*/
void ClearProperties();
void SetSmartDashboardType(const wpi::Twine& type) override;
void SetActuator(bool value) override;
void SetSafeState(std::function<void()> func) override;

View File

@@ -10,12 +10,14 @@
#include <memory>
#include <string>
#include <networktables/NetworkTable.h>
#include <wpi/STLExtras.h>
#include <wpi/Twine.h>
namespace frc {
class Sendable;
class SendableBuilderImpl;
/**
* The SendableRegistry class is the public interface for registering sensors
@@ -26,6 +28,8 @@ class SendableRegistry {
SendableRegistry(const SendableRegistry&) = delete;
SendableRegistry& operator=(const SendableRegistry&) = delete;
using UID = size_t;
/**
* Gets an instance of the SendableRegistry class.
*
@@ -252,6 +256,60 @@ class SendableRegistry {
*/
void DisableLiveWindow(Sendable* sendable);
/**
* Get unique id for an object. Since objects can move, use this instead
* of storing Sendable* directly if ownership is in question.
*
* @param sendable object
* @return unique id
*/
UID GetUniqueId(Sendable* sendable);
/**
* Get sendable object for a given unique id.
*
* @param uid unique id
* @return sendable object (may be null)
*/
Sendable* GetSendable(UID uid);
/**
* Publishes an object in the registry to a network table.
*
* @param sendableUid sendable unique id
* @param table network table
*/
void Publish(UID sendableUid, std::shared_ptr<NetworkTable> table);
/**
* Updates network table information from an object.
*
* @param sendableUid sendable unique id
*/
void Update(UID sendableUid);
/**
* Data passed to ForeachLiveWindow() callback function
*/
struct CallbackData {
CallbackData(Sendable* sendable_, wpi::StringRef name_,
wpi::StringRef subsystem_, Sendable* parent_,
std::shared_ptr<void>& data_, SendableBuilderImpl& builder_)
: sendable(sendable_),
name(name_),
subsystem(subsystem_),
parent(parent_),
data(data_),
builder(builder_) {}
Sendable* sendable;
wpi::StringRef name;
wpi::StringRef subsystem;
Sendable* parent;
std::shared_ptr<void>& data;
SendableBuilderImpl& builder;
};
/**
* Iterates over LiveWindow-enabled objects in the registry.
* It is *not* safe to call other SendableRegistry functions from the
@@ -262,10 +320,7 @@ class SendableRegistry {
*/
void ForeachLiveWindow(
int dataHandle,
wpi::function_ref<void(Sendable* sendable, wpi::StringRef name,
wpi::StringRef subsystem, Sendable* parent,
std::shared_ptr<void>& data)>
callback) const;
wpi::function_ref<void(CallbackData& cbdata)> callback) const;
private:
SendableRegistry();