/*----------------------------------------------------------------------------*/ /* Copyright (c) 2017-2020 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. */ /*----------------------------------------------------------------------------*/ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include "frc/smartdashboard/SendableBuilder.h" namespace frc { class SendableBuilderImpl : public SendableBuilder { public: SendableBuilderImpl() = default; ~SendableBuilderImpl() override = default; SendableBuilderImpl(SendableBuilderImpl&&) = default; SendableBuilderImpl& operator=(SendableBuilderImpl&&) = default; /** * Set the network table. Must be called prior to any Add* functions being * called. * @param table Network table */ void SetTable(std::shared_ptr table); /** * Get the network table. * @return The network table */ std::shared_ptr 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. */ bool IsActuator() const; /** * Update the network table values by calling the getters for all properties. */ void UpdateTable(); /** * Hook setters for all properties. */ void StartListeners(); /** * Unhook setters for all properties. */ void StopListeners(); /** * Start LiveWindow mode by hooking the setters for all properties. Also * calls the SafeState function if one was provided. */ void StartLiveWindowMode(); /** * Stop LiveWindow mode by unhooking the setters for all properties. Also * calls the SafeState function if one was provided. */ void StopLiveWindowMode(); /** * Clear properties. */ void ClearProperties(); void SetSmartDashboardType(const wpi::Twine& type) override; void SetActuator(bool value) override; void SetSafeState(std::function func) override; void SetUpdateTable(std::function func) override; nt::NetworkTableEntry GetEntry(const wpi::Twine& key) override; void AddBooleanProperty(const wpi::Twine& key, std::function getter, std::function setter) override; void AddDoubleProperty(const wpi::Twine& key, std::function getter, std::function setter) override; void AddStringProperty(const wpi::Twine& key, std::function getter, std::function setter) override; void AddBooleanArrayProperty( const wpi::Twine& key, std::function()> getter, std::function)> setter) override; void AddDoubleArrayProperty( const wpi::Twine& key, std::function()> getter, std::function)> setter) override; void AddStringArrayProperty( const wpi::Twine& key, std::function()> getter, std::function)> setter) override; void AddRawProperty(const wpi::Twine& key, std::function getter, std::function setter) override; void AddValueProperty( const wpi::Twine& key, std::function()> getter, std::function)> setter) override; void AddSmallStringProperty( const wpi::Twine& key, std::function& buf)> getter, std::function setter) override; void AddSmallBooleanArrayProperty( const wpi::Twine& key, std::function(wpi::SmallVectorImpl& buf)> getter, std::function)> setter) override; void AddSmallDoubleArrayProperty( const wpi::Twine& key, std::function(wpi::SmallVectorImpl& buf)> getter, std::function)> setter) override; void AddSmallStringArrayProperty( const wpi::Twine& key, std::function< wpi::ArrayRef(wpi::SmallVectorImpl& buf)> getter, std::function)> setter) override; void AddSmallRawProperty( const wpi::Twine& key, std::function& buf)> getter, std::function setter) override; private: struct Property { Property(nt::NetworkTable& table, const wpi::Twine& key) : entry(table.GetEntry(key)) {} Property(const Property&) = delete; Property& operator=(const Property&) = delete; Property(Property&& other) noexcept : entry(other.entry), listener(other.listener), update(std::move(other.update)), createListener(std::move(other.createListener)) { other.entry = nt::NetworkTableEntry(); other.listener = 0; } Property& operator=(Property&& other) noexcept { entry = other.entry; listener = other.listener; other.entry = nt::NetworkTableEntry(); other.listener = 0; update = std::move(other.update); createListener = std::move(other.createListener); return *this; } ~Property() { StopListener(); } void StartListener() { if (entry && listener == 0 && createListener) listener = createListener(entry); } void StopListener() { if (entry && listener != 0) { entry.RemoveListener(listener); listener = 0; } } nt::NetworkTableEntry entry; NT_EntryListener listener = 0; std::function update; std::function createListener; }; std::vector m_properties; std::function m_safeState; std::vector> m_updateTables; std::shared_ptr m_table; nt::NetworkTableEntry m_controllableEntry; bool m_actuator = false; }; } // namespace frc