mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
SendableChooser generic value (#433)
* Java SendableChooser now decorates with type (non-breaking change) * C++ SendableChooser now is templated on the type instead of using void* and stores values (breaking change) * C++ SendableChooser now uses llvm::StringMap instead of std::map
This commit is contained in:
committed by
Peter Johnson
parent
25ae7b2c2b
commit
5aa5e3e09e
@@ -7,11 +7,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "SmartDashboard/Sendable.h"
|
||||
#include "SmartDashboard/SendableChooserBase.h"
|
||||
#include "llvm/StringMap.h"
|
||||
#include "llvm/StringRef.h"
|
||||
#include "tables/ITable.h"
|
||||
|
||||
namespace frc {
|
||||
@@ -27,24 +28,24 @@ namespace frc {
|
||||
* laptop. Once autonomous starts, simply ask the {@link SendableChooser} what
|
||||
* the selected value is.</p>
|
||||
*
|
||||
* @tparam T The type of values to be stored
|
||||
* @see SmartDashboard
|
||||
*/
|
||||
class SendableChooser : public Sendable {
|
||||
template <class T>
|
||||
class SendableChooser : public SendableChooserBase {
|
||||
public:
|
||||
virtual ~SendableChooser() = default;
|
||||
|
||||
void AddObject(const std::string& name, void* object);
|
||||
void AddDefault(const std::string& name, void* object);
|
||||
void* GetSelected();
|
||||
void AddObject(llvm::StringRef name, const T& object);
|
||||
void AddDefault(llvm::StringRef name, const T& object);
|
||||
T GetSelected();
|
||||
|
||||
void InitTable(std::shared_ptr<ITable> subtable) override;
|
||||
std::shared_ptr<ITable> GetTable() const override;
|
||||
std::string GetSmartDashboardType() const override;
|
||||
|
||||
private:
|
||||
std::string m_defaultChoice;
|
||||
std::map<std::string, void*> m_choices;
|
||||
std::shared_ptr<ITable> m_table;
|
||||
llvm::StringMap<T> m_choices;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
#include "SendableChooser.inc"
|
||||
|
||||
@@ -5,13 +5,15 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "SmartDashboard/SendableChooser.h"
|
||||
#pragma once
|
||||
|
||||
using namespace frc;
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
static const std::string kDefault = "default";
|
||||
static const std::string kOptions = "options";
|
||||
static const std::string kSelected = "selected";
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Adds the given object to the list of options.
|
||||
@@ -22,7 +24,8 @@ static const std::string kSelected = "selected";
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
void SendableChooser::AddObject(const std::string& name, void* object) {
|
||||
template <class T>
|
||||
void SendableChooser<T>::AddObject(llvm::StringRef name, const T& object) {
|
||||
m_choices[name] = object;
|
||||
}
|
||||
|
||||
@@ -36,7 +39,8 @@ void SendableChooser::AddObject(const std::string& name, void* object) {
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
void SendableChooser::AddDefault(const std::string& name, void* object) {
|
||||
template <class T>
|
||||
void SendableChooser<T>::AddDefault(llvm::StringRef name, const T& object) {
|
||||
m_defaultChoice = name;
|
||||
AddObject(name, object);
|
||||
}
|
||||
@@ -49,29 +53,31 @@ void SendableChooser::AddDefault(const std::string& name, void* object) {
|
||||
*
|
||||
* @return the option selected
|
||||
*/
|
||||
void* SendableChooser::GetSelected() {
|
||||
template <class T>
|
||||
T SendableChooser<T>::GetSelected() {
|
||||
std::string selected = m_table->GetString(kSelected, m_defaultChoice);
|
||||
if (selected == "")
|
||||
if (selected == "") {
|
||||
return nullptr;
|
||||
else
|
||||
} else {
|
||||
return m_choices[selected];
|
||||
}
|
||||
}
|
||||
|
||||
void SendableChooser::InitTable(std::shared_ptr<ITable> subtable) {
|
||||
template <class T>
|
||||
void SendableChooser<T>::InitTable(std::shared_ptr<ITable> subtable) {
|
||||
std::vector<std::string> keys;
|
||||
m_table = subtable;
|
||||
if (m_table != nullptr) {
|
||||
std::map<std::string, void*>::iterator iter;
|
||||
for (iter = m_choices.begin(); iter != m_choices.end(); iter++) {
|
||||
keys.push_back(iter->first);
|
||||
for (const auto& choice : m_choices) {
|
||||
keys.push_back(choice.first());
|
||||
}
|
||||
|
||||
// Unlike std::map, llvm::StringMap elements are not sorted
|
||||
std::sort(keys.begin(), keys.end());
|
||||
|
||||
m_table->PutValue(kOptions, nt::Value::MakeStringArray(std::move(keys)));
|
||||
m_table->PutString(kDefault, m_defaultChoice);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<ITable> SendableChooser::GetTable() const { return m_table; }
|
||||
|
||||
std::string SendableChooser::GetSmartDashboardType() const {
|
||||
return "String Chooser";
|
||||
}
|
||||
} // namespace frc
|
||||
40
wpilibc/shared/include/SmartDashboard/SendableChooserBase.h
Normal file
40
wpilibc/shared/include/SmartDashboard/SendableChooserBase.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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 <memory>
|
||||
#include <string>
|
||||
|
||||
#include "SmartDashboard/Sendable.h"
|
||||
#include "tables/ITable.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* This class is a non-template base class for {@link SendableChooser}.
|
||||
*
|
||||
* It contains static, non-templated variables to avoid their duplication in the
|
||||
* template class.
|
||||
*/
|
||||
class SendableChooserBase : public Sendable {
|
||||
public:
|
||||
virtual ~SendableChooserBase() = default;
|
||||
|
||||
std::shared_ptr<ITable> GetTable() const override;
|
||||
std::string GetSmartDashboardType() const override;
|
||||
|
||||
protected:
|
||||
static const char* kDefault;
|
||||
static const char* kOptions;
|
||||
static const char* kSelected;
|
||||
|
||||
std::string m_defaultChoice;
|
||||
std::shared_ptr<ITable> m_table;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
22
wpilibc/shared/src/SmartDashboard/SendableChooserBase.cpp
Normal file
22
wpilibc/shared/src/SmartDashboard/SendableChooserBase.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "SmartDashboard/SendableChooserBase.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
const char* SendableChooserBase::kDefault = "default";
|
||||
const char* SendableChooserBase::kOptions = "options";
|
||||
const char* SendableChooserBase::kSelected = "selected";
|
||||
|
||||
std::shared_ptr<ITable> SendableChooserBase::GetTable() const {
|
||||
return m_table;
|
||||
}
|
||||
|
||||
std::string SendableChooserBase::GetSmartDashboardType() const {
|
||||
return "String Chooser";
|
||||
}
|
||||
@@ -22,8 +22,10 @@ import edu.wpi.first.wpilibj.tables.ITable;
|
||||
* SendableChooser} and then put it into the {@link SmartDashboard} to have a list of options appear
|
||||
* on the laptop. Once autonomous starts, simply ask the {@link SendableChooser} what the selected
|
||||
* value is.
|
||||
*
|
||||
* @param <V> The type of the values to be stored
|
||||
*/
|
||||
public class SendableChooser implements Sendable {
|
||||
public class SendableChooser<V> implements Sendable {
|
||||
|
||||
/**
|
||||
* The key for the default value.
|
||||
@@ -40,7 +42,7 @@ public class SendableChooser implements Sendable {
|
||||
/**
|
||||
* A map linking strings to the objects the represent.
|
||||
*/
|
||||
private final HashMap<String, Object> m_map = new HashMap<>();
|
||||
private final HashMap<String, V> m_map = new HashMap<>();
|
||||
private String m_defaultChoice = null;
|
||||
|
||||
/**
|
||||
@@ -56,7 +58,7 @@ public class SendableChooser implements Sendable {
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
public void addObject(String name, Object object) {
|
||||
public void addObject(String name, V object) {
|
||||
// if we don't have a default, set the default automatically
|
||||
if (m_defaultChoice == null) {
|
||||
addDefault(name, object);
|
||||
@@ -78,7 +80,7 @@ public class SendableChooser implements Sendable {
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
public void addDefault(String name, Object object) {
|
||||
public void addDefault(String name, V object) {
|
||||
if (name == null) {
|
||||
throw new NullPointerException("Name cannot be null");
|
||||
}
|
||||
@@ -95,7 +97,7 @@ public class SendableChooser implements Sendable {
|
||||
*
|
||||
* @return the option selected
|
||||
*/
|
||||
public Object getSelected() {
|
||||
public V getSelected() {
|
||||
String selected = m_table.getString(SELECTED, null);
|
||||
return m_map.getOrDefault(selected, m_map.get(m_defaultChoice));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user