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"
|
||||
|
||||
83
wpilibc/shared/include/SmartDashboard/SendableChooser.inc
Normal file
83
wpilibc/shared/include/SmartDashboard/SendableChooser.inc
Normal file
@@ -0,0 +1,83 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2011-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 <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Adds the given object to the list of options.
|
||||
*
|
||||
* On the {@link SmartDashboard} on the desktop, the object will appear as the
|
||||
* given name.
|
||||
*
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
template <class T>
|
||||
void SendableChooser<T>::AddObject(llvm::StringRef name, const T& object) {
|
||||
m_choices[name] = object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given object to the list of options and marks it as the default.
|
||||
*
|
||||
* Functionally, this is very close to {@link SendableChooser#AddObject(const
|
||||
* char *name, void *object) AddObject(...)} except that it will use this as
|
||||
* the default option if none other is explicitly selected.
|
||||
*
|
||||
* @param name the name of the option
|
||||
* @param object the option
|
||||
*/
|
||||
template <class T>
|
||||
void SendableChooser<T>::AddDefault(llvm::StringRef name, const T& object) {
|
||||
m_defaultChoice = name;
|
||||
AddObject(name, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the selected option.
|
||||
*
|
||||
* If there is none selected, it will return the default. If there is none
|
||||
* selected and no default, then it will return {@code nullptr}.
|
||||
*
|
||||
* @return the option selected
|
||||
*/
|
||||
template <class T>
|
||||
T SendableChooser<T>::GetSelected() {
|
||||
std::string selected = m_table->GetString(kSelected, m_defaultChoice);
|
||||
if (selected == "") {
|
||||
return nullptr;
|
||||
} else {
|
||||
return m_choices[selected];
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void SendableChooser<T>::InitTable(std::shared_ptr<ITable> subtable) {
|
||||
std::vector<std::string> keys;
|
||||
m_table = subtable;
|
||||
if (m_table != nullptr) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
} // 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
|
||||
Reference in New Issue
Block a user