2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
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>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include <string>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2013-12-15 18:30:16 -05:00
|
|
|
#include <vector>
|
2016-05-25 22:38:11 -07: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
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* This will also interact with NetworkTable by creating a table called
|
2017-11-16 00:33:51 -08:00
|
|
|
* "Preferences" with all the key-value pairs.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2021-04-18 20:35:29 -07:00
|
|
|
class Preferences {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a vector of all the keys.
|
|
|
|
|
*
|
|
|
|
|
* @return a vector of the keys
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static std::string GetString(std::string_view key,
|
|
|
|
|
std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static int GetInt(std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static double GetDouble(std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static float GetFloat(std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static bool GetBoolean(std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static int64_t GetLong(std::string_view 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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetString(std::string_view key, std::string_view value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given string into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitString(std::string_view key, std::string_view value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetInt(std::string_view key, int value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given int into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitInt(std::string_view key, int value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetDouble(std::string_view key, double value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given double into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitDouble(std::string_view key, double value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetFloat(std::string_view key, float value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given float into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitFloat(std::string_view key, float value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetBoolean(std::string_view key, bool value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given boolean into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitBoolean(std::string_view key, bool value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void SetLong(std::string_view key, int64_t value);
|
2021-05-06 18:25:37 +03:00
|
|
|
|
2020-04-01 23:26:49 -04:00
|
|
|
/**
|
|
|
|
|
* Puts the given long into the preferences table if it doesn't
|
|
|
|
|
* already exist.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void InitLong(std::string_view key, int64_t value);
|
2020-04-01 23:26:49 -04:00
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static bool ContainsKey(std::string_view key);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a preference.
|
|
|
|
|
*
|
|
|
|
|
* @param key the key
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void Remove(std::string_view key);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove all preferences.
|
|
|
|
|
*/
|
2021-06-15 23:06:03 -07:00
|
|
|
static void RemoveAll();
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2021-06-15 23:06:03 -07:00
|
|
|
Preferences() = default;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|