2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2011-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Preferences.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-06-05 07:33:37 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/StringRef.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/WPIErrors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-11-16 00:33:51 -08:00
|
|
|
// The Preferences table name
|
2018-04-29 23:33:19 -07:00
|
|
|
static wpi::StringRef kTableName{"Preferences"};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
Preferences* Preferences::GetInstance() {
|
2015-06-24 04:25:10 -07:00
|
|
|
static Preferences instance;
|
|
|
|
|
return &instance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
std::vector<std::string> Preferences::GetKeys() { return m_table->GetKeys(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
std::string Preferences::GetString(wpi::StringRef key,
|
|
|
|
|
wpi::StringRef defaultValue) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return m_table->GetString(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
int Preferences::GetInt(wpi::StringRef key, int defaultValue) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return static_cast<int>(m_table->GetNumber(key, defaultValue));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
double Preferences::GetDouble(wpi::StringRef key, double defaultValue) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return m_table->GetNumber(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
float Preferences::GetFloat(wpi::StringRef key, float defaultValue) {
|
2016-11-20 07:25:03 -08:00
|
|
|
return m_table->GetNumber(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool Preferences::GetBoolean(wpi::StringRef key, bool defaultValue) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return m_table->GetBoolean(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
int64_t Preferences::GetLong(wpi::StringRef key, int64_t defaultValue) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return static_cast<int64_t>(m_table->GetNumber(key, defaultValue));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutString(wpi::StringRef key, wpi::StringRef value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetString(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutInt(wpi::StringRef key, int value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetDouble(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutDouble(wpi::StringRef key, double value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetDouble(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutFloat(wpi::StringRef key, float value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetDouble(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutBoolean(wpi::StringRef key, bool value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetBoolean(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::PutLong(wpi::StringRef key, int64_t value) {
|
2017-09-02 00:17:43 -07:00
|
|
|
auto entry = m_table->GetEntry(key);
|
|
|
|
|
entry.SetDouble(value);
|
|
|
|
|
entry.SetPersistent();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool Preferences::ContainsKey(wpi::StringRef key) {
|
2015-08-13 23:17:19 -07:00
|
|
|
return m_table->ContainsKey(key);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); }
|
2018-05-16 22:50:35 -04:00
|
|
|
|
|
|
|
|
void Preferences::RemoveAll() {
|
|
|
|
|
for (auto preference : GetKeys()) {
|
|
|
|
|
if (preference != ".type") {
|
|
|
|
|
Remove(preference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
Preferences::Preferences()
|
|
|
|
|
: m_table(nt::NetworkTableInstance::GetDefault().GetTable(kTableName)) {
|
|
|
|
|
m_table->GetEntry(".type").SetString("RobotPreferences");
|
|
|
|
|
m_listener = m_table->AddEntryListener(
|
|
|
|
|
[=](nt::NetworkTable* table, wpi::StringRef name,
|
|
|
|
|
nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value,
|
|
|
|
|
int flags) { entry.SetPersistent(); },
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
|
|
|
|
|
}
|