diff --git a/wpilibc/src/main/native/cpp/Preferences.cpp b/wpilibc/src/main/native/cpp/Preferences.cpp index 1f65e0c8ee..1c3b00d6b8 100644 --- a/wpilibc/src/main/native/cpp/Preferences.cpp +++ b/wpilibc/src/main/native/cpp/Preferences.cpp @@ -222,3 +222,14 @@ bool Preferences::ContainsKey(wpi::StringRef key) { * @param key the key */ void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); } + +/** + * Remove all preferences. + */ +void Preferences::RemoveAll() { + for (auto preference : GetKeys()) { + if (preference != ".type") { + Remove(preference); + } + } +} diff --git a/wpilibc/src/main/native/include/Preferences.h b/wpilibc/src/main/native/include/Preferences.h index 70f28d742c..be00735f33 100644 --- a/wpilibc/src/main/native/include/Preferences.h +++ b/wpilibc/src/main/native/include/Preferences.h @@ -52,6 +52,7 @@ class Preferences : public ErrorBase { void PutLong(wpi::StringRef key, int64_t value); bool ContainsKey(wpi::StringRef key); void Remove(wpi::StringRef key); + void RemoveAll(); protected: Preferences(); 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 fcab667e82..4af3b053c6 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java @@ -173,6 +173,17 @@ public class Preferences { m_table.delete(key); } + /** + * Remove all preferences. + */ + public void removeAll() { + for (String key : m_table.getKeys()) { + if (!".type".equals(key)) { + remove(key); + } + } + } + /** * Returns the string at the given key. If this table does not have a value for that position, * then the given backup value will be returned. diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java index c63f4b305f..bacefe259f 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PreferencesTest.java @@ -7,6 +7,7 @@ package edu.wpi.first.wpilibj; +import java.util.Arrays; import org.junit.Before; import org.junit.Test; @@ -88,6 +89,25 @@ public class PreferencesTest extends AbstractComsSetup { m_pref.putBoolean("checkedValueBoolean", true); } + /** + * Just checking to make sure our helper method works. + */ + @Test + public void testRemove() { + remove(); + assertEquals("Preferences was not empty! Preferences in table: " + + Arrays.toString(m_pref.getKeys().toArray()), + 1, m_pref.getKeys().size()); + } + + @Test + public void testRemoveAll() { + m_pref.removeAll(); + assertEquals("Preferences was not empty! Preferences in table: " + + Arrays.toString(m_pref.getKeys().toArray()), + 1, m_pref.getKeys().size()); + } + @Test public void testAddRemoveSave() { assertEquals(m_pref.getLong("checkedValueLong", 0), 172L);