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
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-06-25 09:05:49 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <memory>
|
2013-12-15 18:30:16 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTable.h>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ErrorBase.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
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* The preferences class provides a relatively simple way to save important
|
2016-05-29 09:19:43 -07:00
|
|
|
* values to the roboRIO to access the next time the roboRIO is booted.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* This class loads and saves from a file inside the roboRIO. The user cannot
|
|
|
|
|
* access the file directly, but may modify values at specific fields which will
|
|
|
|
|
* then be automatically periodically saved to the file by the NetworkTable
|
|
|
|
|
* server.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* This class is thread safe.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* This will also interact with {@link NetworkTable} by creating a table called
|
|
|
|
|
* "Preferences" with all the key-value pairs.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-08-13 23:17:19 -07:00
|
|
|
class Preferences : public ErrorBase {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get the one and only {@link Preferences} object.
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the {@link Preferences}
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
static Preferences* GetInstance();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a vector of all the keys.
|
|
|
|
|
*
|
|
|
|
|
* @return a vector of the keys
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
std::vector<std::string> GetKeys();
|
2018-05-31 20:47:15 -07: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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
std::string GetString(wpi::StringRef key, wpi::StringRef defaultValue = "");
|
2018-05-31 20:47:15 -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.
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
int GetInt(wpi::StringRef key, int defaultValue = 0);
|
2018-05-31 20:47:15 -07: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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
double GetDouble(wpi::StringRef key, double defaultValue = 0.0);
|
2018-05-31 20:47:15 -07: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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
float GetFloat(wpi::StringRef key, float defaultValue = 0.0);
|
2018-05-31 20:47:15 -07: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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
bool GetBoolean(wpi::StringRef key, bool defaultValue = false);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the long (int64_t) 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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
int64_t GetLong(wpi::StringRef key, int64_t defaultValue = 0);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given string into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The value may not have quotation marks, nor may the key have any whitespace
|
|
|
|
|
* nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutString(wpi::StringRef key, wpi::StringRef value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given int into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The key may not have any whitespace nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutInt(wpi::StringRef key, int value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given double into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The key may not have any whitespace nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutDouble(wpi::StringRef key, double value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given float into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The key may not have any whitespace nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutFloat(wpi::StringRef key, float value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given boolean into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The key may not have any whitespace nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutBoolean(wpi::StringRef key, bool value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given long (int64_t) into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* The key may not have any whitespace nor an equals sign.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void PutLong(wpi::StringRef key, int64_t value);
|
2018-05-31 20:47:15 -07: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
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
bool ContainsKey(wpi::StringRef key);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a preference.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
void Remove(wpi::StringRef key);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove all preferences.
|
|
|
|
|
*/
|
2018-05-16 22:50:35 -04:00
|
|
|
void RemoveAll();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
|
|
|
|
Preferences();
|
2015-06-25 01:54:20 -07:00
|
|
|
virtual ~Preferences() = default;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
Preferences(Preferences&&) = default;
|
|
|
|
|
Preferences& operator=(Preferences&&) = default;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2017-09-02 00:17:43 -07:00
|
|
|
std::shared_ptr<nt::NetworkTable> m_table;
|
|
|
|
|
NT_EntryListener m_listener;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|