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 | |
| 9 | #include <hal/FRCUsageReporting.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include <networktables/NetworkTable.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include <networktables/NetworkTableInstance.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | using namespace frc; |
| 14 | |
| 15 | // The Preferences table name |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | static constexpr std::string_view kTableName{"Preferences"}; |
| 17 | |
| 18 | namespace { |
| 19 | struct Instance { |
| 20 | Instance(); |
| 21 | |
| 22 | std::shared_ptr<nt::NetworkTable> table{ |
| 23 | nt::NetworkTableInstance::GetDefault().GetTable(kTableName)}; |
| 24 | NT_EntryListener listener; |
| 25 | }; |
| 26 | } // namespace |
| 27 | |
| 28 | static Instance& GetInstance() { |
| 29 | static Instance instance; |
| 30 | return instance; |
| 31 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 33 | #ifndef __FRC_ROBORIO__ |
| 34 | namespace frc::impl { |
| 35 | void ResetPreferencesInstance() { |
| 36 | GetInstance() = Instance(); |
| 37 | } |
| 38 | } // namespace frc::impl |
| 39 | #endif |
| 40 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | Preferences* Preferences::GetInstance() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | static Preferences instance; |
| 44 | return &instance; |
| 45 | } |
| 46 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 47 | std::vector<std::string> Preferences::GetKeys() { |
| 48 | return ::GetInstance().table->GetKeys(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 51 | std::string Preferences::GetString(std::string_view key, |
| 52 | std::string_view defaultValue) { |
| 53 | return ::GetInstance().table->GetString(key, defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 56 | int Preferences::GetInt(std::string_view key, int defaultValue) { |
| 57 | return static_cast<int>(::GetInstance().table->GetNumber(key, defaultValue)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | double Preferences::GetDouble(std::string_view key, double defaultValue) { |
| 61 | return ::GetInstance().table->GetNumber(key, defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 64 | float Preferences::GetFloat(std::string_view key, float defaultValue) { |
| 65 | return ::GetInstance().table->GetNumber(key, defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 68 | bool Preferences::GetBoolean(std::string_view key, bool defaultValue) { |
| 69 | return ::GetInstance().table->GetBoolean(key, defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 72 | int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) { |
| 73 | return static_cast<int64_t>( |
| 74 | ::GetInstance().table->GetNumber(key, defaultValue)); |
| 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::PutString(std::string_view key, std::string_view value) { |
| 84 | SetString(key, value); |
| 85 | } |
| 86 | |
| 87 | void Preferences::InitString(std::string_view key, std::string_view value) { |
| 88 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 89 | entry.SetDefaultString(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 90 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 93 | void Preferences::SetInt(std::string_view key, int value) { |
| 94 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | entry.SetDouble(value); |
| 96 | entry.SetPersistent(); |
| 97 | } |
| 98 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 99 | void Preferences::PutInt(std::string_view key, int value) { |
| 100 | SetInt(key, value); |
| 101 | } |
| 102 | |
| 103 | void Preferences::InitInt(std::string_view key, int value) { |
| 104 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 105 | entry.SetDefaultDouble(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 106 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 109 | void Preferences::SetDouble(std::string_view key, double value) { |
| 110 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | entry.SetDouble(value); |
| 112 | entry.SetPersistent(); |
| 113 | } |
| 114 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 115 | void Preferences::PutDouble(std::string_view key, double value) { |
| 116 | SetDouble(key, value); |
| 117 | } |
| 118 | |
| 119 | void Preferences::InitDouble(std::string_view key, double value) { |
| 120 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 121 | entry.SetDefaultDouble(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::SetFloat(std::string_view key, float value) { |
| 126 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | entry.SetDouble(value); |
| 128 | entry.SetPersistent(); |
| 129 | } |
| 130 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 131 | void Preferences::PutFloat(std::string_view key, float value) { |
| 132 | SetFloat(key, value); |
| 133 | } |
| 134 | |
| 135 | void Preferences::InitFloat(std::string_view key, float value) { |
| 136 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 137 | entry.SetDefaultDouble(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 138 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 141 | void Preferences::SetBoolean(std::string_view key, bool value) { |
| 142 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | entry.SetBoolean(value); |
| 144 | entry.SetPersistent(); |
| 145 | } |
| 146 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 147 | void Preferences::PutBoolean(std::string_view key, bool value) { |
| 148 | SetBoolean(key, value); |
| 149 | } |
| 150 | |
| 151 | void Preferences::InitBoolean(std::string_view key, bool value) { |
| 152 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 153 | entry.SetDefaultBoolean(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 154 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 157 | void Preferences::SetLong(std::string_view key, int64_t value) { |
| 158 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 159 | entry.SetDouble(value); |
| 160 | entry.SetPersistent(); |
| 161 | } |
| 162 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 163 | void Preferences::PutLong(std::string_view key, int64_t value) { |
| 164 | SetLong(key, value); |
| 165 | } |
| 166 | |
| 167 | void Preferences::InitLong(std::string_view key, int64_t value) { |
| 168 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 169 | entry.SetDefaultDouble(value); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 170 | entry.SetPersistent(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 173 | bool Preferences::ContainsKey(std::string_view key) { |
| 174 | return ::GetInstance().table->ContainsKey(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 177 | void Preferences::Remove(std::string_view key) { |
| 178 | ::GetInstance().table->Delete(key); |
| 179 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 180 | |
| 181 | void Preferences::RemoveAll() { |
| 182 | for (auto preference : GetKeys()) { |
| 183 | if (preference != ".type") { |
| 184 | Remove(preference); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 189 | Instance::Instance() { |
| 190 | table->GetEntry(".type").SetString("RobotPreferences"); |
| 191 | listener = table->AddEntryListener( |
| 192 | [=](nt::NetworkTable* table, std::string_view name, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 193 | nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value, |
| 194 | int flags) { entry.SetPersistent(); }, |
| 195 | NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE); |
| 196 | HAL_Report(HALUsageReporting::kResourceType_Preferences, 0); |
| 197 | } |