mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
Merge .inc files into headers (#7215)
This commit is contained in:
@@ -4,12 +4,17 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <concepts>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/StringMap.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
|
||||
#include "frc/smartdashboard/SendableChooserBase.h"
|
||||
|
||||
@@ -34,10 +39,14 @@ class SendableChooser : public SendableChooserBase {
|
||||
wpi::StringMap<T> m_choices;
|
||||
std::function<void(T)> m_listener;
|
||||
template <class U>
|
||||
static U _unwrap_smart_ptr(const U& value);
|
||||
static U _unwrap_smart_ptr(const U& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
|
||||
static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
public:
|
||||
using CopyType = decltype(_unwrap_smart_ptr(m_choices.lookup("")));
|
||||
@@ -56,7 +65,9 @@ class SendableChooser : public SendableChooserBase {
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
void AddOption(std::string_view name, T object);
|
||||
void AddOption(std::string_view name, T object) {
|
||||
m_choices[name] = std::move(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given object to the list of options and marks it as the default.
|
||||
@@ -67,7 +78,10 @@ class SendableChooser : public SendableChooserBase {
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
void SetDefaultOption(std::string_view name, T object);
|
||||
void SetDefaultOption(std::string_view name, T object) {
|
||||
m_defaultChoice = name;
|
||||
AddOption(name, std::move(object));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the selected option (a std::weak_ptr<U> if T =
|
||||
@@ -80,7 +94,20 @@ class SendableChooser : public SendableChooserBase {
|
||||
*
|
||||
* @return The option selected
|
||||
*/
|
||||
CopyType GetSelected() const;
|
||||
CopyType GetSelected() const {
|
||||
std::string selected = m_defaultChoice;
|
||||
{
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_haveSelected) {
|
||||
selected = m_selected;
|
||||
}
|
||||
}
|
||||
if (selected.empty()) {
|
||||
return CopyType{};
|
||||
} else {
|
||||
return _unwrap_smart_ptr(m_choices.lookup(selected));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a listener that's called when the selected value changes.
|
||||
@@ -88,11 +115,66 @@ class SendableChooser : public SendableChooserBase {
|
||||
* previous listener.
|
||||
* @param listener The function to call that accepts the new value
|
||||
*/
|
||||
void OnChange(std::function<void(T)>);
|
||||
void OnChange(std::function<void(T)> listener) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_listener = listener;
|
||||
}
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
void InitSendable(wpi::SendableBuilder& builder) override {
|
||||
builder.SetSmartDashboardType("String Chooser");
|
||||
builder.PublishConstInteger(kInstance, m_instance);
|
||||
builder.AddStringArrayProperty(
|
||||
kOptions,
|
||||
[=, this] {
|
||||
std::vector<std::string> keys;
|
||||
for (const auto& choice : m_choices) {
|
||||
keys.emplace_back(choice.first());
|
||||
}
|
||||
|
||||
// Unlike std::map, wpi::StringMap elements
|
||||
// are not sorted
|
||||
std::sort(keys.begin(), keys.end());
|
||||
|
||||
return keys;
|
||||
},
|
||||
nullptr);
|
||||
builder.AddSmallStringProperty(
|
||||
kDefault,
|
||||
[=, this](wpi::SmallVectorImpl<char>&) -> std::string_view {
|
||||
return m_defaultChoice;
|
||||
},
|
||||
nullptr);
|
||||
builder.AddSmallStringProperty(
|
||||
kActive,
|
||||
[=, this](wpi::SmallVectorImpl<char>& buf) -> std::string_view {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_haveSelected) {
|
||||
buf.assign(m_selected.begin(), m_selected.end());
|
||||
return {buf.data(), buf.size()};
|
||||
} else {
|
||||
return m_defaultChoice;
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
builder.AddStringProperty(kSelected, nullptr,
|
||||
[=, this](std::string_view val) {
|
||||
T choice{};
|
||||
std::function<void(T)> listener;
|
||||
{
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_haveSelected = true;
|
||||
m_selected = val;
|
||||
if (m_previousVal != val && m_listener) {
|
||||
choice = m_choices[val];
|
||||
listener = m_listener;
|
||||
}
|
||||
m_previousVal = val;
|
||||
}
|
||||
if (listener) {
|
||||
listener(choice);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
#include "frc/smartdashboard/SendableChooser.inc"
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
|
||||
#include "frc/smartdashboard/SendableChooser.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
void SendableChooser<T>::AddOption(std::string_view name, T object) {
|
||||
m_choices[name] = std::move(object);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
void SendableChooser<T>::SetDefaultOption(std::string_view name, T object) {
|
||||
m_defaultChoice = name;
|
||||
AddOption(name, std::move(object));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
typename SendableChooser<T>::CopyType SendableChooser<T>::GetSelected() const {
|
||||
std::string selected = m_defaultChoice;
|
||||
{
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_haveSelected) {
|
||||
selected = m_selected;
|
||||
}
|
||||
}
|
||||
if (selected.empty()) {
|
||||
return CopyType{};
|
||||
} else {
|
||||
return _unwrap_smart_ptr(m_choices.lookup(selected));
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
void SendableChooser<T>::OnChange(std::function<void(T)> listener) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_listener = listener;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
void SendableChooser<T>::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("String Chooser");
|
||||
builder.PublishConstInteger(kInstance, m_instance);
|
||||
builder.AddStringArrayProperty(
|
||||
kOptions,
|
||||
[=, this] {
|
||||
std::vector<std::string> keys;
|
||||
for (const auto& choice : m_choices) {
|
||||
keys.emplace_back(choice.first());
|
||||
}
|
||||
|
||||
// Unlike std::map, wpi::StringMap elements
|
||||
// are not sorted
|
||||
std::sort(keys.begin(), keys.end());
|
||||
|
||||
return keys;
|
||||
},
|
||||
nullptr);
|
||||
builder.AddSmallStringProperty(
|
||||
kDefault,
|
||||
[=, this](wpi::SmallVectorImpl<char>&) -> std::string_view {
|
||||
return m_defaultChoice;
|
||||
},
|
||||
nullptr);
|
||||
builder.AddSmallStringProperty(
|
||||
kActive,
|
||||
[=, this](wpi::SmallVectorImpl<char>& buf) -> std::string_view {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_haveSelected) {
|
||||
buf.assign(m_selected.begin(), m_selected.end());
|
||||
return {buf.data(), buf.size()};
|
||||
} else {
|
||||
return m_defaultChoice;
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
builder.AddStringProperty(kSelected, nullptr,
|
||||
[=, this](std::string_view val) {
|
||||
T choice{};
|
||||
std::function<void(T)> listener;
|
||||
{
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_haveSelected = true;
|
||||
m_selected = val;
|
||||
if (m_previousVal != val && m_listener) {
|
||||
choice = m_choices[val];
|
||||
listener = m_listener;
|
||||
}
|
||||
m_previousVal = val;
|
||||
}
|
||||
if (listener) {
|
||||
listener(choice);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
template <class U>
|
||||
U SendableChooser<T>::_unwrap_smart_ptr(const U& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
template <class U>
|
||||
std::weak_ptr<U> SendableChooser<T>::_unwrap_smart_ptr(
|
||||
const std::shared_ptr<U>& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user