blob: cfb5964516bc6f056241110410dd614933c43116 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Preferences.h"
6
7#include <algorithm>
8
James Kuszmaulcf324122023-01-14 14:07:17 -08009#include <fmt/format.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080010#include <hal/FRCUsageReporting.h>
James Kuszmaulcf324122023-01-14 14:07:17 -080011#include <networktables/MultiSubscriber.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070012#include <networktables/NetworkTable.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include <networktables/NetworkTableInstance.h>
James Kuszmaulcf324122023-01-14 14:07:17 -080014#include <networktables/NetworkTableListener.h>
15#include <networktables/StringTopic.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080016
17using namespace frc;
18
19// The Preferences table name
Austin Schuh812d0d12021-11-04 20:16:48 -070020static constexpr std::string_view kTableName{"Preferences"};
21
22namespace {
23struct Instance {
24 Instance();
25
26 std::shared_ptr<nt::NetworkTable> table{
27 nt::NetworkTableInstance::GetDefault().GetTable(kTableName)};
James Kuszmaulcf324122023-01-14 14:07:17 -080028 nt::StringPublisher typePublisher{table->GetStringTopic(".type").Publish()};
29 nt::MultiSubscriber tableSubscriber{nt::NetworkTableInstance::GetDefault(),
30 {{fmt::format("{}/", table->GetPath())}}};
31 nt::NetworkTableListener listener;
Austin Schuh812d0d12021-11-04 20:16:48 -070032};
33} // namespace
34
35static Instance& GetInstance() {
36 static Instance instance;
37 return instance;
38}
Brian Silverman8fce7482020-01-05 13:18:21 -080039
Austin Schuh75263e32022-02-22 18:05:32 -080040#ifndef __FRC_ROBORIO__
41namespace frc::impl {
42void ResetPreferencesInstance() {
43 GetInstance() = Instance();
44}
45} // namespace frc::impl
46#endif
47
Austin Schuh812d0d12021-11-04 20:16:48 -070048std::vector<std::string> Preferences::GetKeys() {
49 return ::GetInstance().table->GetKeys();
Brian Silverman8fce7482020-01-05 13:18:21 -080050}
51
Austin Schuh812d0d12021-11-04 20:16:48 -070052std::string Preferences::GetString(std::string_view key,
53 std::string_view defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080054 return ::GetInstance().table->GetEntry(key).GetString(defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080055}
56
Austin Schuh812d0d12021-11-04 20:16:48 -070057int Preferences::GetInt(std::string_view key, int defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080058 return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080059}
60
Austin Schuh812d0d12021-11-04 20:16:48 -070061double Preferences::GetDouble(std::string_view key, double defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080062 return ::GetInstance().table->GetEntry(key).GetDouble(defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080063}
64
Austin Schuh812d0d12021-11-04 20:16:48 -070065float Preferences::GetFloat(std::string_view key, float defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080066 return ::GetInstance().table->GetEntry(key).GetFloat(defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080067}
68
Austin Schuh812d0d12021-11-04 20:16:48 -070069bool Preferences::GetBoolean(std::string_view key, bool defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080070 return ::GetInstance().table->GetEntry(key).GetBoolean(defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080071}
72
Austin Schuh812d0d12021-11-04 20:16:48 -070073int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) {
James Kuszmaulcf324122023-01-14 14:07:17 -080074 return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
Austin Schuh812d0d12021-11-04 20:16:48 -070075}
76
77void Preferences::SetString(std::string_view key, std::string_view value) {
78 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -080079 entry.SetString(value);
80 entry.SetPersistent();
81}
82
Austin Schuh812d0d12021-11-04 20:16:48 -070083void Preferences::InitString(std::string_view key, std::string_view value) {
84 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -080085 entry.SetDefaultString(value);
Austin Schuh75263e32022-02-22 18:05:32 -080086 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -080087}
88
Austin Schuh812d0d12021-11-04 20:16:48 -070089void Preferences::SetInt(std::string_view key, int value) {
90 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -080091 entry.SetInteger(value);
Brian Silverman8fce7482020-01-05 13:18:21 -080092 entry.SetPersistent();
93}
94
Austin Schuh812d0d12021-11-04 20:16:48 -070095void Preferences::InitInt(std::string_view key, int value) {
96 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -080097 entry.SetDefaultInteger(value);
Austin Schuh75263e32022-02-22 18:05:32 -080098 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -080099}
100
Austin Schuh812d0d12021-11-04 20:16:48 -0700101void Preferences::SetDouble(std::string_view key, double value) {
102 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800103 entry.SetDouble(value);
104 entry.SetPersistent();
105}
106
Austin Schuh812d0d12021-11-04 20:16:48 -0700107void Preferences::InitDouble(std::string_view key, double value) {
108 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800109 entry.SetDefaultDouble(value);
Austin Schuh75263e32022-02-22 18:05:32 -0800110 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -0800111}
112
Austin Schuh812d0d12021-11-04 20:16:48 -0700113void Preferences::SetFloat(std::string_view key, float value) {
114 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -0800115 entry.SetFloat(value);
Brian Silverman8fce7482020-01-05 13:18:21 -0800116 entry.SetPersistent();
117}
118
Austin Schuh812d0d12021-11-04 20:16:48 -0700119void Preferences::InitFloat(std::string_view key, float value) {
120 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -0800121 entry.SetDefaultFloat(value);
Austin Schuh75263e32022-02-22 18:05:32 -0800122 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -0800123}
124
Austin Schuh812d0d12021-11-04 20:16:48 -0700125void Preferences::SetBoolean(std::string_view key, bool value) {
126 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800127 entry.SetBoolean(value);
128 entry.SetPersistent();
129}
130
Austin Schuh812d0d12021-11-04 20:16:48 -0700131void Preferences::InitBoolean(std::string_view key, bool value) {
132 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800133 entry.SetDefaultBoolean(value);
Austin Schuh75263e32022-02-22 18:05:32 -0800134 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -0800135}
136
Austin Schuh812d0d12021-11-04 20:16:48 -0700137void Preferences::SetLong(std::string_view key, int64_t value) {
138 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -0800139 entry.SetInteger(value);
Brian Silverman8fce7482020-01-05 13:18:21 -0800140 entry.SetPersistent();
141}
142
Austin Schuh812d0d12021-11-04 20:16:48 -0700143void Preferences::InitLong(std::string_view key, int64_t value) {
144 auto entry = ::GetInstance().table->GetEntry(key);
James Kuszmaulcf324122023-01-14 14:07:17 -0800145 entry.SetDefaultInteger(value);
Austin Schuh75263e32022-02-22 18:05:32 -0800146 entry.SetPersistent();
Austin Schuh1e69f942020-11-14 15:06:14 -0800147}
148
Austin Schuh812d0d12021-11-04 20:16:48 -0700149bool Preferences::ContainsKey(std::string_view key) {
150 return ::GetInstance().table->ContainsKey(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800151}
152
Austin Schuh812d0d12021-11-04 20:16:48 -0700153void Preferences::Remove(std::string_view key) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800154 auto entry = ::GetInstance().table->GetEntry(key);
155 entry.ClearPersistent();
156 entry.Unpublish();
Austin Schuh812d0d12021-11-04 20:16:48 -0700157}
Brian Silverman8fce7482020-01-05 13:18:21 -0800158
159void Preferences::RemoveAll() {
160 for (auto preference : GetKeys()) {
161 if (preference != ".type") {
162 Remove(preference);
163 }
164 }
165}
166
Austin Schuh812d0d12021-11-04 20:16:48 -0700167Instance::Instance() {
James Kuszmaulcf324122023-01-14 14:07:17 -0800168 typePublisher.Set("RobotPreferences");
169 listener = nt::NetworkTableListener::CreateListener(
170 tableSubscriber, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
171 [typeTopic = typePublisher.GetTopic().GetHandle()](auto& event) {
172 if (auto topicInfo = event.GetTopicInfo()) {
173 if (topicInfo->topic != typeTopic) {
174 nt::SetTopicPersistent(topicInfo->topic, true);
175 }
176 }
177 });
Brian Silverman8fce7482020-01-05 13:18:21 -0800178 HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
179}