Merge "Automatically set persistent on new Preferences keys."

This commit is contained in:
Brad Miller (WPI)
2015-10-04 15:09:14 -07:00
committed by Gerrit Code Review
3 changed files with 36 additions and 0 deletions

View File

@@ -60,4 +60,13 @@ class Preferences : public ErrorBase {
private:
std::shared_ptr<ITable> m_table;
class Listener : public ITableListener {
public:
void ValueChanged(ITable* source, llvm::StringRef key,
std::shared_ptr<nt::Value> value, bool isNew) override;
void ValueChangedEx(ITable* source, llvm::StringRef key,
std::shared_ptr<nt::Value> value,
unsigned int flags) override;
};
Listener m_listener;
};

View File

@@ -16,7 +16,17 @@
/** The Preferences table name */
static const char *kTableName = "Preferences";
void Preferences::Listener::ValueChanged(ITable* source, llvm::StringRef key,
std::shared_ptr<nt::Value> value,
bool isNew) {}
void Preferences::Listener::ValueChangedEx(ITable* source, llvm::StringRef key,
std::shared_ptr<nt::Value> value,
unsigned int flags) {
source->SetPersistent(key);
}
Preferences::Preferences() : m_table(NetworkTable::GetTable(kTableName)) {
m_table->AddTableListenerEx(&m_listener, NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
HALReport(HALUsageReporting::kResourceType_Preferences, 0);
}

View File

@@ -11,6 +11,8 @@ import java.util.Vector;
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
import edu.wpi.first.wpilibj.communication.UsageReporting;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.tables.TableKeyNotDefinedException;
/**
@@ -48,6 +50,20 @@ public class Preferences {
* The network table
*/
private NetworkTable table;
/**
* Listener to set all Preferences values to persistent (for backwards
* compatibility with old dashboards).
*/
private final ITableListener listener = new ITableListener() {
@Override
public void valueChanged(ITable table, String key, Object value, boolean isNew) {
// unused
}
@Override
public void valueChangedEx(ITable table, String key, Object value, int flags) {
table.setPersistent(key);
}
};
/**
* Returns the preferences instance.
@@ -66,6 +82,7 @@ public class Preferences {
*/
private Preferences() {
table = NetworkTable.getTable(TABLE_NAME);
table.addTableListenerEx(listener, ITable.NOTIFY_NEW | ITable.NOTIFY_IMMEDIATE);
UsageReporting.report(tResourceType.kResourceType_Preferences, 0);
}