WPILib 2012
WPILibRoboticsLibraryforFRC
Public Member Functions | Static Public Member Functions
Preferences Class Reference

#include <Preferences.h>

Inheritance diagram for Preferences:
ErrorBase NetworkTableChangeListener

List of all members.

Public Member Functions

std::vector< std::string > GetKeys ()
std::string GetString (const char *key, const char *defaultValue="")
int GetString (const char *key, char *value, int valueSize, const char *defaultValue="")
int GetInt (const char *key, int defaultValue=0)
double GetDouble (const char *key, double defaultValue=0.0)
float GetFloat (const char *key, float defaultValue=0.0)
bool GetBoolean (const char *key, bool defaultValue=false)
INT64 GetLong (const char *key, INT64 defaultValue=0)
void PutString (const char *key, const char *value)
void PutInt (const char *key, int value)
void PutDouble (const char *key, double value)
void PutFloat (const char *key, float value)
void PutBoolean (const char *key, bool value)
void PutLong (const char *key, INT64 value)
void Save ()
bool ContainsKey (const char *key)
void Remove (const char *key)

Static Public Member Functions

static PreferencesGetInstance ()

Detailed Description

The preferences class provides a relatively simple way to save important values to the cRIO to access the next time the cRIO is booted.

This class loads and saves from a file inside the cRIO. The user can not access the file directly, but may modify values at specific fields which will then be saved to the file when Save() is called.

This class is thread safe.

This will also interact with NetworkTable by creating a table called "Preferences" with all the key-value pairs. To save using NetworkTable, simply set the boolean at position "~S A V E~" to true. Also, if the value of any variable is " in the NetworkTable, then that represents non-existence in the Preferences table


Member Function Documentation

bool Preferences::ContainsKey ( const char *  key)

Returns whether or not there is a key with the given name.

Parameters:
keythe key
Returns:
if there is a value at the given key
bool Preferences::GetBoolean ( const char *  key,
bool  defaultValue = false 
)

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.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
double Preferences::GetDouble ( const char *  key,
double  defaultValue = 0.0 
)

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.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
float Preferences::GetFloat ( const char *  key,
float  defaultValue = 0.0 
)

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.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
Preferences * Preferences::GetInstance ( ) [static]

Get the one and only Preferences object

Returns:
pointer to the Preferences
int Preferences::GetInt ( const char *  key,
int  defaultValue = 0 
)

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.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
std::vector< std::string > Preferences::GetKeys ( )

Returns a vector of all the keys

Returns:
a vector of the keys
INT64 Preferences::GetLong ( const char *  key,
INT64  defaultValue = 0 
)

Returns the long (INT64) at the given key. If this table does not have a value for that position, then the given defaultValue value will be returned.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
int Preferences::GetString ( const char *  key,
char *  value,
int  valueSize,
const char *  defaultValue = "" 
)

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.

Parameters:
keythe key
valuethe buffer to copy the value into
valueSizethe size of value
defaultValuethe value to return if none exists in the table
Returns:
The size of the returned string
std::string Preferences::GetString ( const char *  key,
const char *  defaultValue = "" 
)

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.

Parameters:
keythe key
defaultValuethe value to return if none exists in the table
Returns:
either the value in the table, or the defaultValue
void Preferences::PutBoolean ( const char *  key,
bool  value 
)

Puts the given boolean into the preferences table.

The key may not have any whitespace nor an equals sign

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care) at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::PutDouble ( const char *  key,
double  value 
)

Puts the given double into the preferences table.

The key may not have any whitespace nor an equals sign

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care) at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::PutFloat ( const char *  key,
float  value 
)

Puts the given float into the preferences table.

The key may not have any whitespace nor an equals sign

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care) at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::PutInt ( const char *  key,
int  value 
)

Puts the given int into the preferences table.

The key may not have any whitespace nor an equals sign

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care) at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::PutLong ( const char *  key,
INT64  value 
)

Puts the given long (INT64) into the preferences table.

The key may not have any whitespace nor an equals sign

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care) at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::PutString ( const char *  key,
const char *  value 
)

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

This will NOT save the value to memory between power cycles, to do that you must call Save() (which must be used with care). at some point after calling this.

Parameters:
keythe key
valuethe value
void Preferences::Remove ( const char *  key)

Remove a preference

Parameters:
keythe key
void Preferences::Save ( )

Saves the preferences to a file on the cRIO.

This should NOT be called often. Too many writes can damage the cRIO's flash memory. While it is ok to save once or twice a match, this should never be called every run of IterativeRobot#TeleopPeriodic(), etc.

The actual writing of the file is done in a separate thread. However, any call to a get or put method will wait until the table is fully saved before continuing.


The documentation for this class was generated from the following files:
 All Classes Functions Variables