2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <functional>
|
2019-09-14 15:22:54 -05:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2021-05-26 17:44:18 -07:00
|
|
|
#include <string_view>
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
#include "wpi/function_ref.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
namespace wpi {
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
class Sendable;
|
2021-06-13 16:38:05 -07:00
|
|
|
class SendableBuilder;
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The SendableRegistry class is the public interface for registering sensors
|
|
|
|
|
* and actuators for use on dashboards and LiveWindow.
|
|
|
|
|
*/
|
|
|
|
|
class SendableRegistry {
|
|
|
|
|
public:
|
2021-06-15 23:06:03 -07:00
|
|
|
SendableRegistry() = delete;
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
using UID = size_t;
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the factory for LiveWindow builders.
|
|
|
|
|
*
|
|
|
|
|
* @param factory factory function
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetLiveWindowBuilderFactory(
|
2021-06-13 16:38:05 -07:00
|
|
|
std::function<std::unique_ptr<SendableBuilder>()> factory);
|
|
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param name component name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Add(Sendable* sendable, std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Add(Sendable* sendable, std::string_view moduleType, int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param moduleNumber The number of the particular module type
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Add(Sendable* sendable, std::string_view moduleType,
|
|
|
|
|
int moduleNumber, int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param subsystem subsystem name
|
|
|
|
|
* @param name component name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Add(Sendable* sendable, std::string_view subsystem,
|
|
|
|
|
std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry and LiveWindow.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param name component name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddLW(Sendable* sendable, std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry and LiveWindow.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddLW(Sendable* sendable, std::string_view moduleType,
|
|
|
|
|
int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry and LiveWindow.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param moduleNumber The number of the particular module type
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddLW(Sendable* sendable, std::string_view moduleType,
|
|
|
|
|
int moduleNumber, int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an object to the registry and LiveWindow.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to add
|
|
|
|
|
* @param subsystem subsystem name
|
|
|
|
|
* @param name component name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddLW(Sendable* sendable, std::string_view subsystem,
|
|
|
|
|
std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2019-12-29 23:37:14 -06:00
|
|
|
/**
|
|
|
|
|
* Adds a child object to an object. Adds the child object to the registry
|
|
|
|
|
* if it's not already present.
|
|
|
|
|
*
|
|
|
|
|
* @param parent parent object
|
|
|
|
|
* @param child child object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddChild(Sendable* parent, Sendable* child);
|
2019-12-29 23:37:14 -06:00
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
/**
|
|
|
|
|
* Adds a child object to an object. Adds the child object to the registry
|
|
|
|
|
* if it's not already present.
|
|
|
|
|
*
|
|
|
|
|
* @param parent parent object
|
|
|
|
|
* @param child child object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void AddChild(Sendable* parent, void* child);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes an object from the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to remove
|
|
|
|
|
* @return true if the object was removed; false if it was not present
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static bool Remove(Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Moves an object in the registry (for use in move constructors/assignments).
|
|
|
|
|
*
|
|
|
|
|
* @param to new object
|
|
|
|
|
* @param from old object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Move(Sendable* to, Sendable* from);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if an object is in the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object to check
|
|
|
|
|
* @return True if in registry, false if not.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static bool Contains(const Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the name of an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @return Name (empty if object is not in registry)
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static std::string GetName(const Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the name of an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param name name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetName(Sendable* sendable, std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the name of an object with a channel number.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetName(Sendable* sendable, std::string_view moduleType,
|
|
|
|
|
int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the name of an object with a module and channel number.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param moduleType A string that defines the module name in the label for
|
|
|
|
|
* the value
|
|
|
|
|
* @param moduleNumber The number of the particular module type
|
|
|
|
|
* @param channel The channel number the device is plugged into
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetName(Sendable* sendable, std::string_view moduleType,
|
|
|
|
|
int moduleNumber, int channel);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets both the subsystem name and device name of an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param subsystem subsystem name
|
|
|
|
|
* @param name device name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetName(Sendable* sendable, std::string_view subsystem,
|
|
|
|
|
std::string_view name);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the subsystem name of an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @return Subsystem name (empty if object is not in registry)
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static std::string GetSubsystem(const Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the subsystem name of an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param subsystem subsystem name
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetSubsystem(Sendable* sendable, std::string_view subsystem);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a unique handle for setting/getting data with SetData() and GetData().
|
|
|
|
|
*
|
|
|
|
|
* @return Handle
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static int GetDataHandle();
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Associates arbitrary data with an object in the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param handle data handle returned by GetDataHandle()
|
|
|
|
|
* @param data data to set
|
|
|
|
|
* @return Previous data (may be null)
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static std::shared_ptr<void> SetData(Sendable* sendable, int handle,
|
|
|
|
|
std::shared_ptr<void> data);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets arbitrary data associated with an object in the registry.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
* @param handle data handle returned by GetDataHandle()
|
|
|
|
|
* @return data (may be null if none associated)
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static std::shared_ptr<void> GetData(Sendable* sendable, int handle);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables LiveWindow for an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void EnableLiveWindow(Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disables LiveWindow for an object.
|
|
|
|
|
*
|
|
|
|
|
* @param sendable object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void DisableLiveWindow(Sendable* sendable);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static UID GetUniqueId(Sendable* sendable);
|
2019-10-17 22:01:31 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get sendable object for a given unique id.
|
|
|
|
|
*
|
|
|
|
|
* @param uid unique id
|
|
|
|
|
* @return sendable object (may be null)
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static Sendable* GetSendable(UID uid);
|
2019-10-17 22:01:31 -07:00
|
|
|
|
|
|
|
|
/**
|
2021-06-13 16:38:05 -07:00
|
|
|
* Publishes an object in the registry.
|
2019-10-17 22:01:31 -07:00
|
|
|
*
|
|
|
|
|
* @param sendableUid sendable unique id
|
2021-06-13 16:38:05 -07:00
|
|
|
* @param builder publisher backend
|
2019-10-17 22:01:31 -07:00
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Publish(UID sendableUid,
|
|
|
|
|
std::unique_ptr<SendableBuilder> builder);
|
2019-10-17 22:01:31 -07:00
|
|
|
|
|
|
|
|
/**
|
2021-06-13 16:38:05 -07:00
|
|
|
* Updates published information from an object.
|
2019-10-17 22:01:31 -07:00
|
|
|
*
|
|
|
|
|
* @param sendableUid sendable unique id
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Update(UID sendableUid);
|
2019-10-17 22:01:31 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Data passed to ForeachLiveWindow() callback function
|
|
|
|
|
*/
|
|
|
|
|
struct CallbackData {
|
2021-06-06 16:13:58 -07:00
|
|
|
CallbackData(Sendable* sendable_, std::string_view name_,
|
2021-06-13 16:38:05 -07:00
|
|
|
std::string_view subsystem_, wpi::Sendable* parent_,
|
|
|
|
|
std::shared_ptr<void>& data_, SendableBuilder& builder_)
|
2019-10-17 22:01:31 -07:00
|
|
|
: sendable(sendable_),
|
|
|
|
|
name(name_),
|
|
|
|
|
subsystem(subsystem_),
|
|
|
|
|
parent(parent_),
|
|
|
|
|
data(data_),
|
|
|
|
|
builder(builder_) {}
|
|
|
|
|
|
|
|
|
|
Sendable* sendable;
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view name;
|
|
|
|
|
std::string_view subsystem;
|
2019-10-17 22:01:31 -07:00
|
|
|
Sendable* parent;
|
|
|
|
|
std::shared_ptr<void>& data;
|
2021-06-13 16:38:05 -07:00
|
|
|
SendableBuilder& builder;
|
2019-10-17 22:01:31 -07:00
|
|
|
};
|
|
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
/**
|
|
|
|
|
* Iterates over LiveWindow-enabled objects in the registry.
|
|
|
|
|
* It is *not* safe to call other SendableRegistry functions from the
|
|
|
|
|
* callback (this will likely deadlock).
|
|
|
|
|
*
|
|
|
|
|
* @param dataHandle data handle to get data pointer passed to callback
|
|
|
|
|
* @param callback function to call for each object
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void ForeachLiveWindow(
|
|
|
|
|
int dataHandle, wpi::function_ref<void(CallbackData& cbdata)> callback);
|
2019-09-14 15:22:54 -05:00
|
|
|
};
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
} // namespace wpi
|