2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2011-2017 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Preferences.h"
|
|
|
|
|
|
2016-06-05 07:33:37 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
#include <llvm/StringRef.h>
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "WPIErrors.h"
|
2017-09-02 00:17:43 -07:00
|
|
|
#include "networktables/NetworkTableInstance.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/** The Preferences table name */
|
2016-12-20 22:08:24 -08:00
|
|
|
static llvm::StringRef kTableName{"Preferences"};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
Preferences::Preferences()
|
|
|
|
|
: m_table(nt::NetworkTableInstance::GetDefault().GetTable(kTableName)) {
|
|
|
|
|
m_listener = m_table->AddEntryListener(
|
|
|
|
|
[=](nt::NetworkTable* table, llvm::StringRef name,
|
|
|
|
|
nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value,
|
|
|
|
|
int flags) { entry.SetPersistent(); },
|
|
|
|
|
NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Get the one and only {@link Preferences} object.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return pointer to the {@link Preferences}
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Returns a vector of all the keys.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return a vector of the keys
|
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the string at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue will be returned.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
std::string Preferences::GetString(llvm::StringRef key,
|
|
|
|
|
llvm::StringRef defaultValue) {
|
|
|
|
|
return m_table->GetString(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-06-05 07:33:37 -07:00
|
|
|
* Returns the int at the given key. If this table does not have a value for
|
|
|
|
|
* that position, then the given defaultValue value will be returned.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
int Preferences::GetInt(llvm::StringRef key, int defaultValue) {
|
|
|
|
|
return static_cast<int>(m_table->GetNumber(key, defaultValue));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the double at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue value will be returned.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
double Preferences::GetDouble(llvm::StringRef key, double defaultValue) {
|
|
|
|
|
return m_table->GetNumber(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the float at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue value will be returned.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
float Preferences::GetFloat(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the boolean at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue value will be returned.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
bool Preferences::GetBoolean(llvm::StringRef key, bool defaultValue) {
|
|
|
|
|
return m_table->GetBoolean(key, defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the long (int64_t) at the given key. If this table does not have a
|
2016-05-20 17:30:37 -07:00
|
|
|
* value for that position, then the given defaultValue value will be returned.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return either the value in the table, or the defaultValue
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
int64_t Preferences::GetLong(llvm::StringRef key, int64_t defaultValue) {
|
|
|
|
|
return static_cast<int64_t>(m_table->GetNumber(key, defaultValue));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given string into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The value may not have quotation marks, nor may the key
|
|
|
|
|
* have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutString(llvm::StringRef key, llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given int into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The key may not have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutInt(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given double into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The key may not have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutDouble(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given float into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The key may not have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutFloat(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given boolean into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The key may not have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutBoolean(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given long (int64_t) into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The key may not have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param key the key
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutLong(llvm::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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not there is a key with the given name.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param key the key
|
|
|
|
|
* @return if there is a value at the given key
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
bool Preferences::ContainsKey(llvm::StringRef key) {
|
|
|
|
|
return m_table->ContainsKey(key);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Remove a preference.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param key the key
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
void Preferences::Remove(llvm::StringRef key) { m_table->Delete(key); }
|