diff --git a/wpilibc/src/main/native/cpp/Preferences.cpp b/wpilibc/src/main/native/cpp/Preferences.cpp index 60147757c4..8c1eb87b75 100644 --- a/wpilibc/src/main/native/cpp/Preferences.cpp +++ b/wpilibc/src/main/native/cpp/Preferences.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2011-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -58,36 +58,66 @@ void Preferences::PutString(wpi::StringRef key, wpi::StringRef value) { entry.SetPersistent(); } +void Preferences::InitString(wpi::StringRef key, wpi::StringRef value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultString(value); +} + void Preferences::PutInt(wpi::StringRef key, int value) { auto entry = m_table->GetEntry(key); entry.SetDouble(value); entry.SetPersistent(); } +void Preferences::InitInt(wpi::StringRef key, int value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultDouble(value); +} + void Preferences::PutDouble(wpi::StringRef key, double value) { auto entry = m_table->GetEntry(key); entry.SetDouble(value); entry.SetPersistent(); } +void Preferences::InitDouble(wpi::StringRef key, double value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultDouble(value); +} + void Preferences::PutFloat(wpi::StringRef key, float value) { auto entry = m_table->GetEntry(key); entry.SetDouble(value); entry.SetPersistent(); } +void Preferences::InitFloat(wpi::StringRef key, float value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultDouble(value); +} + void Preferences::PutBoolean(wpi::StringRef key, bool value) { auto entry = m_table->GetEntry(key); entry.SetBoolean(value); entry.SetPersistent(); } +void Preferences::InitBoolean(wpi::StringRef key, bool value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultBoolean(value); +} + void Preferences::PutLong(wpi::StringRef key, int64_t value) { auto entry = m_table->GetEntry(key); entry.SetDouble(value); entry.SetPersistent(); } +void Preferences::InitLong(wpi::StringRef key, int64_t value) { + auto entry = m_table->GetEntry(key); + entry.SetDefaultDouble(value); +} + bool Preferences::ContainsKey(wpi::StringRef key) { return m_table->ContainsKey(key); } diff --git a/wpilibc/src/main/native/include/frc/Preferences.h b/wpilibc/src/main/native/include/frc/Preferences.h index 57678a824f..f04b012bec 100644 --- a/wpilibc/src/main/native/include/frc/Preferences.h +++ b/wpilibc/src/main/native/include/frc/Preferences.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2011-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2011-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -121,6 +121,12 @@ class Preferences : public ErrorBase { */ void PutString(wpi::StringRef key, wpi::StringRef value); + /** + * Puts the given string into the preferences table if it doesn't + * already exist. + */ + void InitString(wpi::StringRef key, wpi::StringRef value); + /** * Puts the given int into the preferences table. * @@ -131,6 +137,12 @@ class Preferences : public ErrorBase { */ void PutInt(wpi::StringRef key, int value); + /** + * Puts the given int into the preferences table if it doesn't + * already exist. + */ + void InitInt(wpi::StringRef key, int value); + /** * Puts the given double into the preferences table. * @@ -141,6 +153,12 @@ class Preferences : public ErrorBase { */ void PutDouble(wpi::StringRef key, double value); + /** + * Puts the given double into the preferences table if it doesn't + * already exist. + */ + void InitDouble(wpi::StringRef key, double value); + /** * Puts the given float into the preferences table. * @@ -151,6 +169,12 @@ class Preferences : public ErrorBase { */ void PutFloat(wpi::StringRef key, float value); + /** + * Puts the given float into the preferences table if it doesn't + * already exist. + */ + void InitFloat(wpi::StringRef key, float value); + /** * Puts the given boolean into the preferences table. * @@ -161,6 +185,12 @@ class Preferences : public ErrorBase { */ void PutBoolean(wpi::StringRef key, bool value); + /** + * Puts the given boolean into the preferences table if it doesn't + * already exist. + */ + void InitBoolean(wpi::StringRef key, bool value); + /** * Puts the given long (int64_t) into the preferences table. * @@ -171,6 +201,12 @@ class Preferences : public ErrorBase { */ void PutLong(wpi::StringRef key, int64_t value); + /** + * Puts the given long into the preferences table if it doesn't + * already exist. + */ + void InitLong(wpi::StringRef key, int64_t value); + /** * Returns whether or not there is a key with the given name. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java index b25d9073cb..a45b6de276 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -31,6 +31,7 @@ import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam; *
This will also interact with {@link NetworkTable} by creating a table called "Preferences" * with all the key-value pairs.
*/ +@SuppressWarnings("PMD.TooManyMethods") public final class Preferences { /** * The Preferences table name. @@ -94,6 +95,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given string into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initString(String key, String value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultString(value); + } + /** * Puts the given int into the preferences table. * @@ -106,6 +118,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given int into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initInt(String key, int value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultDouble(value); + } + /** * Puts the given double into the preferences table. * @@ -118,6 +141,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given double into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initDouble(String key, double value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultDouble(value); + } + /** * Puts the given float into the preferences table. * @@ -130,6 +164,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given float into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initFloat(String key, float value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultDouble(value); + } + /** * Puts the given boolean into the preferences table. * @@ -142,6 +187,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given boolean into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initBoolean(String key, boolean value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultBoolean(value); + } + /** * Puts the given long into the preferences table. * @@ -154,6 +210,17 @@ public final class Preferences { entry.setPersistent(); } + /** + * Puts the given long into the preferences table if it doesn't already exist. + * + * @param key The key + * @param value The value + */ + public void initLong(String key, long value) { + NetworkTableEntry entry = m_table.getEntry(key); + entry.setDefaultDouble(value); + } + /** * Returns whether or not there is a key with the given name. *