Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Preferences.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 9 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <hal/FRCUsageReporting.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 11 | #include <networktables/MultiSubscriber.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 12 | #include <networktables/NetworkTable.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include <networktables/NetworkTableInstance.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 14 | #include <networktables/NetworkTableListener.h> |
| 15 | #include <networktables/StringTopic.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | // The Preferences table name |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | static constexpr std::string_view kTableName{"Preferences"}; |
| 21 | |
| 22 | namespace { |
| 23 | struct Instance { |
| 24 | Instance(); |
| 25 | |
| 26 | std::shared_ptr<nt::NetworkTable> table{ |
| 27 | nt::NetworkTableInstance::GetDefault().GetTable(kTableName)}; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 28 | nt::StringPublisher typePublisher{table->GetStringTopic(".type").Publish()}; |
| 29 | nt::MultiSubscriber tableSubscriber{nt::NetworkTableInstance::GetDefault(), |
| 30 | {{fmt::format("{}/", table->GetPath())}}}; |
| 31 | nt::NetworkTableListener listener; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | }; |
| 33 | } // namespace |
| 34 | |
| 35 | static Instance& GetInstance() { |
| 36 | static Instance instance; |
| 37 | return instance; |
| 38 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 40 | #ifndef __FRC_ROBORIO__ |
| 41 | namespace frc::impl { |
| 42 | void ResetPreferencesInstance() { |
| 43 | GetInstance() = Instance(); |
| 44 | } |
| 45 | } // namespace frc::impl |
| 46 | #endif |
| 47 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 48 | std::vector<std::string> Preferences::GetKeys() { |
| 49 | return ::GetInstance().table->GetKeys(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 52 | std::string Preferences::GetString(std::string_view key, |
| 53 | std::string_view defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 54 | return ::GetInstance().table->GetEntry(key).GetString(defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 57 | int Preferences::GetInt(std::string_view key, int defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 58 | return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 61 | double Preferences::GetDouble(std::string_view key, double defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 62 | return ::GetInstance().table->GetEntry(key).GetDouble(defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | float Preferences::GetFloat(std::string_view key, float defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 66 | return ::GetInstance().table->GetEntry(key).GetFloat(defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 69 | bool Preferences::GetBoolean(std::string_view key, bool defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 70 | return ::GetInstance().table->GetEntry(key).GetBoolean(defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 73 | int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 74 | return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void Preferences::SetString(std::string_view key, std::string_view value) { |
| 78 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 79 | entry.SetString(value); |
| 80 | entry.SetPersistent(); |
| 81 | } |
| 82 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 83 | void Preferences::InitString(std::string_view key, std::string_view value) { |
| 84 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 85 | entry.SetDefaultString(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 86 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 89 | void Preferences::SetInt(std::string_view key, int value) { |
| 90 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 91 | entry.SetInteger(value); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | entry.SetPersistent(); |
| 93 | } |
| 94 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 95 | void Preferences::InitInt(std::string_view key, int value) { |
| 96 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 97 | entry.SetDefaultInteger(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 98 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 101 | void Preferences::SetDouble(std::string_view key, double value) { |
| 102 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | entry.SetDouble(value); |
| 104 | entry.SetPersistent(); |
| 105 | } |
| 106 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 107 | void Preferences::InitDouble(std::string_view key, double value) { |
| 108 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 109 | entry.SetDefaultDouble(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 110 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 113 | void Preferences::SetFloat(std::string_view key, float value) { |
| 114 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 115 | entry.SetFloat(value); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | entry.SetPersistent(); |
| 117 | } |
| 118 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 119 | void Preferences::InitFloat(std::string_view key, float value) { |
| 120 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 121 | entry.SetDefaultFloat(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 122 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 125 | void Preferences::SetBoolean(std::string_view key, bool value) { |
| 126 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | entry.SetBoolean(value); |
| 128 | entry.SetPersistent(); |
| 129 | } |
| 130 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 131 | void Preferences::InitBoolean(std::string_view key, bool value) { |
| 132 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 133 | entry.SetDefaultBoolean(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 134 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 137 | void Preferences::SetLong(std::string_view key, int64_t value) { |
| 138 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 139 | entry.SetInteger(value); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 140 | entry.SetPersistent(); |
| 141 | } |
| 142 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 143 | void Preferences::InitLong(std::string_view key, int64_t value) { |
| 144 | auto entry = ::GetInstance().table->GetEntry(key); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 145 | entry.SetDefaultInteger(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 146 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 149 | bool Preferences::ContainsKey(std::string_view key) { |
| 150 | return ::GetInstance().table->ContainsKey(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 153 | void Preferences::Remove(std::string_view key) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 154 | auto entry = ::GetInstance().table->GetEntry(key); |
| 155 | entry.ClearPersistent(); |
| 156 | entry.Unpublish(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 157 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 158 | |
| 159 | void Preferences::RemoveAll() { |
| 160 | for (auto preference : GetKeys()) { |
| 161 | if (preference != ".type") { |
| 162 | Remove(preference); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 167 | Instance::Instance() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 168 | 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 178 | HAL_Report(HALUsageReporting::kResourceType_Preferences, 0); |
| 179 | } |