Squashed 'third_party/allwpilib_2017/' content from commit 35ac87d
Change-Id: I7bb6f5556c30d3f5a092e68de0be9c710c60c9f4
git-subtree-dir: third_party/allwpilib_2017
git-subtree-split: 35ac87d6ff8b7f061c4f18c9ea316e5dccd4888a
diff --git a/wpilibc/shared/include/SmartDashboard/NamedSendable.h b/wpilibc/shared/include/SmartDashboard/NamedSendable.h
new file mode 100644
index 0000000..759447f
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/NamedSendable.h
@@ -0,0 +1,30 @@
+/*----------------------------------------------------------------------------*/
+/* Copyright (c) FIRST 2012-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 <string>
+
+#include "SmartDashboard/Sendable.h"
+
+namespace frc {
+
+/**
+ * The interface for sendable objects that gives the sendable a default name in
+ * the Smart Dashboard
+ *
+ */
+class NamedSendable : public Sendable {
+ public:
+ /**
+ * @return the name of the subtable of SmartDashboard that the Sendable object
+ * will use
+ */
+ virtual std::string GetName() const = 0;
+};
+
+} // namespace frc
diff --git a/wpilibc/shared/include/SmartDashboard/Sendable.h b/wpilibc/shared/include/SmartDashboard/Sendable.h
new file mode 100644
index 0000000..d7ca758
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/Sendable.h
@@ -0,0 +1,37 @@
+/*----------------------------------------------------------------------------*/
+/* 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 <memory>
+#include <string>
+
+#include "tables/ITable.h"
+
+namespace frc {
+
+class Sendable {
+ public:
+ /**
+ * Initializes a table for this sendable object.
+ * @param subtable The table to put the values in.
+ */
+ virtual void InitTable(std::shared_ptr<ITable> subtable) = 0;
+
+ /**
+ * @return the table that is currently associated with the sendable
+ */
+ virtual std::shared_ptr<ITable> GetTable() const = 0;
+
+ /**
+ * @return the string representation of the named data type that will be used
+ * by the smart dashboard for this sendable
+ */
+ virtual std::string GetSmartDashboardType() const = 0;
+};
+
+} // namespace frc
diff --git a/wpilibc/shared/include/SmartDashboard/SendableChooser.h b/wpilibc/shared/include/SmartDashboard/SendableChooser.h
new file mode 100644
index 0000000..5dbee54
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/SendableChooser.h
@@ -0,0 +1,51 @@
+/*----------------------------------------------------------------------------*/
+/* 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 <memory>
+#include <string>
+
+#include "SmartDashboard/SendableChooserBase.h"
+#include "llvm/StringMap.h"
+#include "llvm/StringRef.h"
+#include "tables/ITable.h"
+
+namespace frc {
+
+/**
+ * The {@link SendableChooser} class is a useful tool for presenting a selection
+ * of options to the {@link SmartDashboard}.
+ *
+ * <p>For instance, you may wish to be able to select between multiple
+ * autonomous modes. You can do this by putting every possible {@link Command}
+ * you want to run as an autonomous into a {@link 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.</p>
+ *
+ * @tparam T The type of values to be stored
+ * @see SmartDashboard
+ */
+template <class T>
+class SendableChooser : public SendableChooserBase {
+ public:
+ virtual ~SendableChooser() = default;
+
+ 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;
+
+ private:
+ llvm::StringMap<T> m_choices;
+};
+
+} // namespace frc
+
+#include "SendableChooser.inc"
diff --git a/wpilibc/shared/include/SmartDashboard/SendableChooser.inc b/wpilibc/shared/include/SmartDashboard/SendableChooser.inc
new file mode 100644
index 0000000..ba9363f
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/SendableChooser.inc
@@ -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
diff --git a/wpilibc/shared/include/SmartDashboard/SendableChooserBase.h b/wpilibc/shared/include/SmartDashboard/SendableChooserBase.h
new file mode 100644
index 0000000..04102ee
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/SendableChooserBase.h
@@ -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
diff --git a/wpilibc/shared/include/SmartDashboard/SmartDashboard.h b/wpilibc/shared/include/SmartDashboard/SmartDashboard.h
new file mode 100644
index 0000000..8656d73
--- /dev/null
+++ b/wpilibc/shared/include/SmartDashboard/SmartDashboard.h
@@ -0,0 +1,100 @@
+/*----------------------------------------------------------------------------*/
+/* 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 <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "SensorBase.h"
+#include "SmartDashboard/NamedSendable.h"
+#include "SmartDashboard/Sendable.h"
+#include "tables/ITable.h"
+
+namespace frc {
+
+class SmartDashboard : public SensorBase {
+ public:
+ static void init();
+
+ static bool ContainsKey(llvm::StringRef key);
+
+ static std::vector<std::string> GetKeys(int types = 0);
+
+ static void SetPersistent(llvm::StringRef key);
+ static void ClearPersistent(llvm::StringRef key);
+ static bool IsPersistent(llvm::StringRef key);
+
+ static void SetFlags(llvm::StringRef key, unsigned int flags);
+ static void ClearFlags(llvm::StringRef key, unsigned int flags);
+ static unsigned int GetFlags(llvm::StringRef key);
+
+ static void Delete(llvm::StringRef key);
+
+ static void PutData(llvm::StringRef key, Sendable* data);
+ static void PutData(NamedSendable* value);
+ static Sendable* GetData(llvm::StringRef keyName);
+
+ static bool PutBoolean(llvm::StringRef keyName, bool value);
+ static bool SetDefaultBoolean(llvm::StringRef key, bool defaultValue);
+ static bool GetBoolean(llvm::StringRef keyName, bool defaultValue);
+
+ static bool PutNumber(llvm::StringRef keyName, double value);
+ static bool SetDefaultNumber(llvm::StringRef key, double defaultValue);
+ static double GetNumber(llvm::StringRef keyName, double defaultValue);
+
+ static bool PutString(llvm::StringRef keyName, llvm::StringRef value);
+ static bool SetDefaultString(llvm::StringRef key,
+ llvm::StringRef defaultValue);
+ static std::string GetString(llvm::StringRef keyName,
+ llvm::StringRef defaultValue);
+
+ static bool PutBooleanArray(llvm::StringRef key, llvm::ArrayRef<int> value);
+ static bool SetDefaultBooleanArray(llvm::StringRef key,
+ llvm::ArrayRef<int> defaultValue);
+ static std::vector<int> GetBooleanArray(llvm::StringRef key,
+ llvm::ArrayRef<int> defaultValue);
+
+ static bool PutNumberArray(llvm::StringRef key, llvm::ArrayRef<double> value);
+ static bool SetDefaultNumberArray(llvm::StringRef key,
+ llvm::ArrayRef<double> defaultValue);
+ static std::vector<double> GetNumberArray(
+ llvm::StringRef key, llvm::ArrayRef<double> defaultValue);
+
+ static bool PutStringArray(llvm::StringRef key,
+ llvm::ArrayRef<std::string> value);
+ static bool SetDefaultStringArray(llvm::StringRef key,
+ llvm::ArrayRef<std::string> defaultValue);
+ static std::vector<std::string> GetStringArray(
+ llvm::StringRef key, llvm::ArrayRef<std::string> defaultValue);
+
+ static bool PutRaw(llvm::StringRef key, llvm::StringRef value);
+ static bool SetDefaultRaw(llvm::StringRef key, llvm::StringRef defaultValue);
+ static std::string GetRaw(llvm::StringRef key, llvm::StringRef defaultValue);
+
+ static bool PutValue(llvm::StringRef keyName,
+ std::shared_ptr<nt::Value> value);
+ static bool SetDefaultValue(llvm::StringRef key,
+ std::shared_ptr<nt::Value> defaultValue);
+ static std::shared_ptr<nt::Value> GetValue(llvm::StringRef keyName);
+
+ private:
+ virtual ~SmartDashboard() = default;
+
+ /** The {@link NetworkTable} used by {@link SmartDashboard} */
+ static std::shared_ptr<ITable> m_table;
+
+ /**
+ * A map linking tables in the SmartDashboard to the
+ * {@link SmartDashboardData} objects they came from.
+ */
+ static std::map<std::shared_ptr<ITable>, Sendable*> m_tablesToData;
+};
+
+} // namespace frc