2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-25 22:40:15 -07:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
#include <memory>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/StringMap.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SendableChooserBase.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2017-11-16 00:33:51 -08:00
|
|
|
* The SendableChooser class is a useful tool for presenting a selection of
|
|
|
|
|
* options to the SmartDashboard.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* For instance, you may wish to be able to select between multiple autonomous
|
|
|
|
|
* modes. You can do this by putting every possible Command you want to run as
|
|
|
|
|
* an autonomous into a SendableChooser and then put it into the SmartDashboard
|
|
|
|
|
* to have a list of options appear on the laptop. Once autonomous starts,
|
|
|
|
|
* simply ask the SendableChooser what the selected value is.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-01-03 14:40:31 -08:00
|
|
|
* @tparam T The type of values to be stored
|
2013-12-15 18:30:16 -05:00
|
|
|
* @see SmartDashboard
|
|
|
|
|
*/
|
2017-01-03 14:40:31 -08:00
|
|
|
template <class T>
|
|
|
|
|
class SendableChooser : public SendableChooserBase {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringMap<T> m_choices;
|
2017-06-30 18:37:22 -04:00
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
static U _unwrap_smart_ptr(const U& value);
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
static U* _unwrap_smart_ptr(const std::unique_ptr<U>& value);
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2020-07-30 13:34:07 -05:00
|
|
|
SendableChooser() = default;
|
2020-12-05 20:04:44 -08:00
|
|
|
~SendableChooser() override = default;
|
2020-07-30 13:34:07 -05:00
|
|
|
SendableChooser(SendableChooser&& rhs) = default;
|
|
|
|
|
SendableChooser& operator=(SendableChooser&& rhs) = default;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-05 20:04:44 -08:00
|
|
|
/**
|
|
|
|
|
* Adds the given object to the list of options.
|
|
|
|
|
*
|
|
|
|
|
* On the SmartDashboard on the desktop, the object will appear as the given
|
|
|
|
|
* name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name of the option
|
|
|
|
|
* @param object the option
|
|
|
|
|
*/
|
2021-06-06 16:13:58 -07:00
|
|
|
void AddOption(std::string_view name, T object);
|
2020-12-05 20:04:44 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add the given object to the list of options and marks it as the default.
|
|
|
|
|
*
|
|
|
|
|
* Functionally, this is very close to AddOption() 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
|
|
|
|
|
*/
|
2021-06-06 16:13:58 -07:00
|
|
|
void SetDefaultOption(std::string_view name, T object);
|
2018-08-19 01:51:17 -07:00
|
|
|
|
2020-12-05 20:04:44 -08:00
|
|
|
/**
|
|
|
|
|
* Returns a copy of the selected option (a raw pointer U* if T =
|
|
|
|
|
* std::unique_ptr<U> or a std::weak_ptr<U> if T = std::shared_ptr<U>).
|
|
|
|
|
*
|
|
|
|
|
* If there is none selected, it will return the default. If there is none
|
|
|
|
|
* selected and no default, then it will return a value-initialized instance.
|
|
|
|
|
* For integer types, this is 0. For container types like std::string, this is
|
|
|
|
|
* an empty string.
|
|
|
|
|
*
|
|
|
|
|
* @return The option selected
|
|
|
|
|
*/
|
2017-06-30 18:37:22 -04:00
|
|
|
auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""]));
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void InitSendable(nt::NTSendableBuilder& builder) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|
2017-01-03 14:40:31 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SendableChooser.inc"
|