Files
allwpilib/wpilibc/src/main/native/cpp/util/Preferences.cpp

188 lines
5.5 KiB
C++
Raw Normal View History

// 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.
2025-11-07 19:56:21 -05:00
#include "wpi/util/Preferences.hpp"
2024-09-20 17:43:39 -07:00
#include <memory>
#include <string>
#include <vector>
2022-10-08 10:01:31 -07:00
#include <fmt/format.h>
2025-11-07 19:57:55 -05:00
2025-11-07 19:56:21 -05:00
#include "wpi/hal/UsageReporting.h"
#include "wpi/nt/MultiSubscriber.hpp"
#include "wpi/nt/NetworkTable.hpp"
#include "wpi/nt/NetworkTableInstance.hpp"
#include "wpi/nt/NetworkTableListener.hpp"
#include "wpi/nt/StringTopic.hpp"
#include "wpi/util/json.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi;
// The Preferences table name
static constexpr std::string_view kTableName{"Preferences"};
static constexpr std::string_view kSmartDashboardType = "RobotPreferences";
namespace {
struct Instance {
Instance();
2025-11-07 20:00:05 -05:00
std::shared_ptr<wpi::nt::NetworkTable> table{
wpi::nt::NetworkTableInstance::GetDefault().GetTable(kTableName)};
2025-11-07 20:01:58 -05:00
wpi::nt::StringPublisher typePublisher{
table->GetStringTopic(".type").PublishEx(
wpi::nt::StringTopic::kTypeString,
{{"SmartDashboard", kSmartDashboardType}})};
wpi::nt::MultiSubscriber tableSubscriber{
wpi::nt::NetworkTableInstance::GetDefault(),
{{fmt::format("{}/", table->GetPath())}}};
2025-11-07 20:00:05 -05:00
wpi::nt::NetworkTableListener listener;
};
} // namespace
static Instance& GetInstance() {
static Instance instance;
return instance;
}
#ifndef __FRC_SYSTEMCORE__
2025-11-07 20:00:05 -05:00
namespace wpi::impl {
void ResetPreferencesInstance() {
GetInstance() = Instance();
}
2025-11-07 20:00:05 -05:00
} // namespace wpi::impl
#endif
std::vector<std::string> Preferences::GetKeys() {
return ::GetInstance().table->GetKeys();
}
std::string Preferences::GetString(std::string_view key,
std::string_view defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetString(defaultValue);
}
int Preferences::GetInt(std::string_view key, int defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
}
double Preferences::GetDouble(std::string_view key, double defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetDouble(defaultValue);
}
float Preferences::GetFloat(std::string_view key, float defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetFloat(defaultValue);
}
bool Preferences::GetBoolean(std::string_view key, bool defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetBoolean(defaultValue);
}
int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) {
2022-10-08 10:01:31 -07:00
return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
}
void Preferences::SetString(std::string_view key, std::string_view value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetString(value);
entry.SetPersistent();
}
void Preferences::InitString(std::string_view key, std::string_view value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultString(value);
entry.SetPersistent();
}
void Preferences::SetInt(std::string_view key, int value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetInteger(value);
entry.SetPersistent();
}
void Preferences::InitInt(std::string_view key, int value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetDefaultInteger(value);
entry.SetPersistent();
}
void Preferences::SetDouble(std::string_view key, double value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDouble(value);
entry.SetPersistent();
}
void Preferences::InitDouble(std::string_view key, double value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultDouble(value);
entry.SetPersistent();
}
void Preferences::SetFloat(std::string_view key, float value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetFloat(value);
entry.SetPersistent();
}
void Preferences::InitFloat(std::string_view key, float value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetDefaultFloat(value);
entry.SetPersistent();
}
void Preferences::SetBoolean(std::string_view key, bool value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetBoolean(value);
entry.SetPersistent();
}
void Preferences::InitBoolean(std::string_view key, bool value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultBoolean(value);
entry.SetPersistent();
}
void Preferences::SetLong(std::string_view key, int64_t value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetInteger(value);
entry.SetPersistent();
}
void Preferences::InitLong(std::string_view key, int64_t value) {
auto entry = ::GetInstance().table->GetEntry(key);
2022-10-08 10:01:31 -07:00
entry.SetDefaultInteger(value);
entry.SetPersistent();
}
bool Preferences::ContainsKey(std::string_view key) {
return ::GetInstance().table->ContainsKey(key);
}
void Preferences::Remove(std::string_view key) {
2022-10-08 10:01:31 -07:00
auto entry = ::GetInstance().table->GetEntry(key);
entry.ClearPersistent();
entry.Unpublish();
}
void Preferences::RemoveAll() {
for (auto preference : GetKeys()) {
if (preference != ".type") {
Remove(preference);
}
}
}
Instance::Instance() {
typePublisher.Set(kSmartDashboardType);
2025-11-07 20:00:05 -05:00
listener = wpi::nt::NetworkTableListener::CreateListener(
tableSubscriber, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
[typeTopic = typePublisher.GetTopic().GetHandle()](auto& event) {
if (auto topicInfo = event.GetTopicInfo()) {
if (topicInfo->topic != typeTopic) {
2025-11-07 20:00:05 -05:00
wpi::nt::SetTopicPersistent(topicInfo->topic, true);
}
}
});
HAL_ReportUsage("Preferences", "");
}