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 | |
| 33 | Preferences* Preferences::GetInstance() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 34 | ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | static Preferences instance; |
| 36 | return &instance; |
| 37 | } |
| 38 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 39 | std::vector<std::string> Preferences::GetKeys() { |
| 40 | return ::GetInstance().table->GetKeys(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | std::string Preferences::GetString(std::string_view key, |
| 44 | std::string_view defaultValue) { |
| 45 | return ::GetInstance().table->GetString(key, defaultValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 48 | int Preferences::GetInt(std::string_view key, int defaultValue) { |
| 49 | return static_cast<int>(::GetInstance().table->GetNumber(key, defaultValue)); |
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 | double Preferences::GetDouble(std::string_view key, double defaultValue) { |
| 53 | return ::GetInstance().table->GetNumber(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 | float Preferences::GetFloat(std::string_view key, float defaultValue) { |
| 57 | return ::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 | bool Preferences::GetBoolean(std::string_view key, bool defaultValue) { |
| 61 | return ::GetInstance().table->GetBoolean(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 | int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) { |
| 65 | return static_cast<int64_t>( |
| 66 | ::GetInstance().table->GetNumber(key, defaultValue)); |
| 67 | } |
| 68 | |
| 69 | void Preferences::SetString(std::string_view key, std::string_view value) { |
| 70 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | entry.SetString(value); |
| 72 | entry.SetPersistent(); |
| 73 | } |
| 74 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 75 | void Preferences::PutString(std::string_view key, std::string_view value) { |
| 76 | SetString(key, value); |
| 77 | } |
| 78 | |
| 79 | void Preferences::InitString(std::string_view key, std::string_view value) { |
| 80 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 81 | entry.SetDefaultString(value); |
| 82 | } |
| 83 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 84 | void Preferences::SetInt(std::string_view key, int value) { |
| 85 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | entry.SetDouble(value); |
| 87 | entry.SetPersistent(); |
| 88 | } |
| 89 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 90 | void Preferences::PutInt(std::string_view key, int value) { |
| 91 | SetInt(key, value); |
| 92 | } |
| 93 | |
| 94 | void Preferences::InitInt(std::string_view key, int value) { |
| 95 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 96 | entry.SetDefaultDouble(value); |
| 97 | } |
| 98 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 99 | void Preferences::SetDouble(std::string_view key, double value) { |
| 100 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | entry.SetDouble(value); |
| 102 | entry.SetPersistent(); |
| 103 | } |
| 104 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 105 | void Preferences::PutDouble(std::string_view key, double value) { |
| 106 | SetDouble(key, value); |
| 107 | } |
| 108 | |
| 109 | void Preferences::InitDouble(std::string_view key, double value) { |
| 110 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 111 | entry.SetDefaultDouble(value); |
| 112 | } |
| 113 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 114 | void Preferences::SetFloat(std::string_view key, float value) { |
| 115 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | entry.SetDouble(value); |
| 117 | entry.SetPersistent(); |
| 118 | } |
| 119 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 120 | void Preferences::PutFloat(std::string_view key, float value) { |
| 121 | SetFloat(key, value); |
| 122 | } |
| 123 | |
| 124 | void Preferences::InitFloat(std::string_view key, float value) { |
| 125 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 126 | entry.SetDefaultDouble(value); |
| 127 | } |
| 128 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 129 | void Preferences::SetBoolean(std::string_view key, bool value) { |
| 130 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | entry.SetBoolean(value); |
| 132 | entry.SetPersistent(); |
| 133 | } |
| 134 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 135 | void Preferences::PutBoolean(std::string_view key, bool value) { |
| 136 | SetBoolean(key, value); |
| 137 | } |
| 138 | |
| 139 | void Preferences::InitBoolean(std::string_view key, bool value) { |
| 140 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 141 | entry.SetDefaultBoolean(value); |
| 142 | } |
| 143 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 144 | void Preferences::SetLong(std::string_view key, int64_t value) { |
| 145 | auto entry = ::GetInstance().table->GetEntry(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | entry.SetDouble(value); |
| 147 | entry.SetPersistent(); |
| 148 | } |
| 149 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 150 | void Preferences::PutLong(std::string_view key, int64_t value) { |
| 151 | SetLong(key, value); |
| 152 | } |
| 153 | |
| 154 | void Preferences::InitLong(std::string_view key, int64_t value) { |
| 155 | auto entry = ::GetInstance().table->GetEntry(key); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 156 | entry.SetDefaultDouble(value); |
| 157 | } |
| 158 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 159 | bool Preferences::ContainsKey(std::string_view key) { |
| 160 | return ::GetInstance().table->ContainsKey(key); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 163 | void Preferences::Remove(std::string_view key) { |
| 164 | ::GetInstance().table->Delete(key); |
| 165 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 166 | |
| 167 | void Preferences::RemoveAll() { |
| 168 | for (auto preference : GetKeys()) { |
| 169 | if (preference != ".type") { |
| 170 | Remove(preference); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 175 | Instance::Instance() { |
| 176 | table->GetEntry(".type").SetString("RobotPreferences"); |
| 177 | listener = table->AddEntryListener( |
| 178 | [=](nt::NetworkTable* table, std::string_view name, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 179 | nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value, |
| 180 | int flags) { entry.SetPersistent(); }, |
| 181 | NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE); |
| 182 | HAL_Report(HALUsageReporting::kResourceType_Preferences, 0); |
| 183 | } |