Merge .inc files into headers (#7215)

This commit is contained in:
Tyler Veness
2024-10-15 23:42:57 -07:00
committed by GitHub
parent 40caabea23
commit fad06ae1e7
25 changed files with 913 additions and 1520 deletions

View File

@@ -36,7 +36,11 @@ class [[deprecated(
*/
template <class... MotorControllers>
explicit MotorControllerGroup(MotorController& motorController,
MotorControllers&... motorControllers);
MotorControllers&... motorControllers)
: m_motorControllers(std::vector<std::reference_wrapper<MotorController>>{
motorController, motorControllers...}) {
Initialize();
}
/**
* Create a new MotorControllerGroup with the provided MotorControllers.
@@ -68,6 +72,4 @@ class [[deprecated(
} // namespace frc
#include "frc/motorcontrol/MotorControllerGroup.inc"
WPI_UNIGNORE_DEPRECATED

View File

@@ -1,22 +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 <functional>
#include <vector>
#include "frc/motorcontrol/MotorControllerGroup.h"
namespace frc {
template <class... MotorControllers>
MotorControllerGroup::MotorControllerGroup(
MotorController& motorController, MotorControllers&... motorControllers)
: m_motorControllers(std::vector<std::reference_wrapper<MotorController>>{
motorController, motorControllers...}) {
Initialize();
}
} // namespace frc

View File

@@ -4,8 +4,6 @@
#pragma once
#include <memory>
#include <string>
#include <string_view>
#include <networktables/NetworkTable.h>
@@ -34,7 +32,9 @@ class ShuffleboardComponent : public ShuffleboardComponentBase {
* @param type The component type.
*/
ShuffleboardComponent(ShuffleboardContainer& parent, std::string_view title,
std::string_view type = "");
std::string_view type = "")
: ShuffleboardValue(title),
ShuffleboardComponentBase(parent, title, type) {}
/**
* Sets custom properties for this component. Property names are
@@ -44,7 +44,11 @@ class ShuffleboardComponent : public ShuffleboardComponentBase {
* @param properties the properties for this component
* @return this component
*/
Derived& WithProperties(const wpi::StringMap<nt::Value>& properties);
Derived& WithProperties(const wpi::StringMap<nt::Value>& properties) {
m_properties = properties;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
/**
* Sets the position of this component in the tab. This has no effect if this
@@ -59,7 +63,12 @@ class ShuffleboardComponent : public ShuffleboardComponentBase {
* @param rowIndex the row in the tab to place this component
* @return this component
*/
Derived& WithPosition(int columnIndex, int rowIndex);
Derived& WithPosition(int columnIndex, int rowIndex) {
m_column = columnIndex;
m_row = rowIndex;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
/**
* Sets the size of this component in the tab. This has no effect if this
@@ -69,9 +78,12 @@ class ShuffleboardComponent : public ShuffleboardComponentBase {
* @param height how many rows high the component should be
* @return this component
*/
Derived& WithSize(int width, int height);
Derived& WithSize(int width, int height) {
m_width = width;
m_height = height;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
};
} // namespace frc
#include "frc/shuffleboard/ShuffleboardComponent.inc"

View File

@@ -1,47 +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 <memory>
#include <string>
#include <string_view>
#include "frc/shuffleboard/ShuffleboardComponent.h"
namespace frc {
template <typename Derived>
ShuffleboardComponent<Derived>::ShuffleboardComponent(
ShuffleboardContainer& parent, std::string_view title,
std::string_view type)
: ShuffleboardValue(title),
ShuffleboardComponentBase(parent, title, type) {}
template <typename Derived>
Derived& ShuffleboardComponent<Derived>::WithProperties(
const wpi::StringMap<nt::Value>& properties) {
m_properties = properties;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
template <typename Derived>
Derived& ShuffleboardComponent<Derived>::WithPosition(int columnIndex,
int rowIndex) {
m_column = columnIndex;
m_row = rowIndex;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
template <typename Derived>
Derived& ShuffleboardComponent<Derived>::WithSize(int width, int height) {
m_width = width;
m_height = height;
m_metadataDirty = true;
return *static_cast<Derived*>(this);
}
} // namespace frc

View File

@@ -14,7 +14,6 @@
#include <networktables/NetworkTable.h>
#include "frc/shuffleboard/ShuffleboardComponent.h"
#include "frc/shuffleboard/ShuffleboardComponent.inc"
#include "frc/shuffleboard/ShuffleboardComponentBase.h"
#include "frc/shuffleboard/ShuffleboardWidget.h"

View File

@@ -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"

View File

@@ -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