WPILibC++ 2023.4.3
SuppliedValueWidget.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <string>
10#include <string_view>
11
15
20
21namespace frc {
22class ShuffleboardContainer;
23
24template <typename T>
25class SuppliedValueWidget : public ShuffleboardWidget<SuppliedValueWidget<T>> {
26 public:
28 std::string_view typeString, std::function<T()> supplier,
29 std::function<void(nt::GenericPublisher&, T)> setter)
30 : ShuffleboardValue(title),
31 ShuffleboardWidget<SuppliedValueWidget<T>>(parent, title),
32 m_typeString(typeString),
33 m_supplier(supplier),
34 m_setter(setter) {}
35
36 void BuildInto(std::shared_ptr<nt::NetworkTable> parentTable,
37 std::shared_ptr<nt::NetworkTable> metaTable) override {
38 this->BuildMetadata(metaTable);
39 if (!m_controllablePub) {
40 m_controllablePub =
41 nt::BooleanTopic{metaTable->GetTopic("Controllable")}.Publish();
42 m_controllablePub.Set(false);
43 }
44
45 if (!m_entry) {
46 m_entry =
47 parentTable->GetTopic(this->GetTitle()).GenericPublish(m_typeString);
48 }
49 m_setter(m_entry, m_supplier());
50 }
51
52 private:
53 std::string m_typeString;
54 std::function<T()> m_supplier;
55 std::function<void(nt::GenericPublisher&, T)> m_setter;
56 nt::BooleanPublisher m_controllablePub;
58};
59} // namespace frc
void BuildMetadata(std::shared_ptr< nt::NetworkTable > metaTable)
Common interface for objects that can contain shuffleboard components.
Definition: ShuffleboardContainer.h:42
Definition: ShuffleboardValue.h:15
const std::string & GetTitle() const
Gets the title of this Shuffleboard value.
Definition: ShuffleboardValue.h:27
Abstract superclass for widgets.
Definition: ShuffleboardWidget.h:29
Definition: SuppliedValueWidget.h:25
void BuildInto(std::shared_ptr< nt::NetworkTable > parentTable, std::shared_ptr< nt::NetworkTable > metaTable) override
Builds the entries for this value.
Definition: SuppliedValueWidget.h:36
SuppliedValueWidget(ShuffleboardContainer &parent, std::string_view title, std::string_view typeString, std::function< T()> supplier, std::function< void(nt::GenericPublisher &, T)> setter)
Definition: SuppliedValueWidget.h:27
NetworkTables Boolean publisher.
Definition: BooleanTopic.h:110
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: BooleanTopic.inc:48
NetworkTables Boolean topic.
Definition: BooleanTopic.h:210
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: BooleanTopic.inc:89
NetworkTables generic publisher.
Definition: GenericEntry.h:193
TopicType GetTopic() const
Get the corresponding topic.
Definition: GenericEntry.inc:200
GenericPublisher GenericPublish(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagFieldLayout.h:22