2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2014-10-19 13:34:50 -07:00
|
|
|
/* Copyright (c) FIRST 2011. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Preferences.h"
|
|
|
|
|
|
|
|
|
|
#include "WPIErrors.h"
|
2014-09-25 20:36:59 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
/** The Preferences table name */
|
|
|
|
|
static const char *kTableName = "Preferences";
|
|
|
|
|
|
2015-10-03 09:04:38 -07:00
|
|
|
void Preferences::Listener::ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value,
|
|
|
|
|
bool isNew) {}
|
|
|
|
|
void Preferences::Listener::ValueChangedEx(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value,
|
|
|
|
|
unsigned int flags) {
|
|
|
|
|
source->SetPersistent(key);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
Preferences::Preferences() : m_table(NetworkTable::GetTable(kTableName)) {
|
2015-10-03 09:04:38 -07:00
|
|
|
m_table->AddTableListenerEx(&m_listener, NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
|
2015-06-25 01:54:20 -07:00
|
|
|
HALReport(HALUsageReporting::kResourceType_Preferences, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the one and only {@link Preferences} object
|
|
|
|
|
* @return pointer to the {@link Preferences}
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a vector of all the keys
|
|
|
|
|
* @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.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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) {
|
|
|
|
|
return static_cast<float>(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.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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
|
|
|
|
|
* value
|
2013-12-15 18:30:16 -05:00
|
|
|
* for that position, then the given defaultValue value will be returned.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutString(llvm::StringRef key, llvm::StringRef value) {
|
|
|
|
|
m_table->PutString(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutInt(llvm::StringRef key, int value) {
|
|
|
|
|
m_table->PutNumber(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutDouble(llvm::StringRef key, double value) {
|
|
|
|
|
m_table->PutNumber(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutFloat(llvm::StringRef key, float value) {
|
|
|
|
|
m_table->PutNumber(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutBoolean(llvm::StringRef key, bool value) {
|
|
|
|
|
m_table->PutBoolean(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
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>
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::PutLong(llvm::StringRef key, int64_t value) {
|
|
|
|
|
m_table->PutNumber(key, value);
|
|
|
|
|
m_table->SetPersistent(key);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-08-13 23:17:19 -07:00
|
|
|
* This function is no longer required, as NetworkTables automatically
|
|
|
|
|
* saves persistent values (which all Preferences values are) periodically
|
|
|
|
|
* when running as a server.
|
|
|
|
|
* @deprecated backwards compatibility shim
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::Save() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not there is a key with the given name.
|
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-08-13 23:17:19 -07:00
|
|
|
* Remove a preference
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param key the key
|
|
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
void Preferences::Remove(llvm::StringRef key) {
|
|
|
|
|
m_table->Delete(key);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|