duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

layered_settings_interface.cpp (7256B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #include "layered_settings_interface.h"
      5 #include "common/assert.h"
      6 #include <unordered_set>
      7 
      8 LayeredSettingsInterface::LayeredSettingsInterface() = default;
      9 
     10 LayeredSettingsInterface::~LayeredSettingsInterface() = default;
     11 
     12 bool LayeredSettingsInterface::Save(Error* error /* = nullptr */)
     13 {
     14   Panic("Attempting to save layered settings interface");
     15 }
     16 
     17 void LayeredSettingsInterface::Clear()
     18 {
     19   Panic("Attempting to clear layered settings interface");
     20 }
     21 
     22 bool LayeredSettingsInterface::IsEmpty()
     23 {
     24   return false;
     25 }
     26 
     27 bool LayeredSettingsInterface::GetIntValue(const char* section, const char* key, s32* value) const
     28 {
     29   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
     30   {
     31     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
     32     {
     33       if (sif->GetIntValue(section, key, value))
     34         return true;
     35     }
     36   }
     37 
     38   return false;
     39 }
     40 
     41 bool LayeredSettingsInterface::GetUIntValue(const char* section, const char* key, u32* value) const
     42 {
     43   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
     44   {
     45     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
     46     {
     47       if (sif->GetUIntValue(section, key, value))
     48         return true;
     49     }
     50   }
     51 
     52   return false;
     53 }
     54 
     55 bool LayeredSettingsInterface::GetFloatValue(const char* section, const char* key, float* value) const
     56 {
     57   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
     58   {
     59     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
     60     {
     61       if (sif->GetFloatValue(section, key, value))
     62         return true;
     63     }
     64   }
     65 
     66   return false;
     67 }
     68 
     69 bool LayeredSettingsInterface::GetDoubleValue(const char* section, const char* key, double* value) const
     70 {
     71   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
     72   {
     73     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
     74     {
     75       if (sif->GetDoubleValue(section, key, value))
     76         return true;
     77     }
     78   }
     79 
     80   return false;
     81 }
     82 
     83 bool LayeredSettingsInterface::GetBoolValue(const char* section, const char* key, bool* value) const
     84 {
     85   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
     86   {
     87     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
     88     {
     89       if (sif->GetBoolValue(section, key, value))
     90         return true;
     91     }
     92   }
     93 
     94   return false;
     95 }
     96 
     97 bool LayeredSettingsInterface::GetStringValue(const char* section, const char* key, std::string* value) const
     98 {
     99   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
    100   {
    101     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
    102     {
    103       if (sif->GetStringValue(section, key, value))
    104         return true;
    105     }
    106   }
    107 
    108   return false;
    109 }
    110 
    111 bool LayeredSettingsInterface::GetStringValue(const char* section, const char* key, SmallStringBase* value) const
    112 {
    113   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
    114   {
    115     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
    116     {
    117       if (sif->GetStringValue(section, key, value))
    118         return true;
    119     }
    120   }
    121 
    122   return false;
    123 }
    124 
    125 void LayeredSettingsInterface::SetIntValue(const char* section, const char* key, int value)
    126 {
    127   Panic("Attempt to call SetIntValue() on layered settings interface");
    128 }
    129 
    130 void LayeredSettingsInterface::SetUIntValue(const char* section, const char* key, u32 value)
    131 {
    132   Panic("Attempt to call SetUIntValue() on layered settings interface");
    133 }
    134 
    135 void LayeredSettingsInterface::SetFloatValue(const char* section, const char* key, float value)
    136 {
    137   Panic("Attempt to call SetFloatValue() on layered settings interface");
    138 }
    139 
    140 void LayeredSettingsInterface::SetDoubleValue(const char* section, const char* key, double value)
    141 {
    142   Panic("Attempt to call SetDoubleValue() on layered settings interface");
    143 }
    144 
    145 void LayeredSettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
    146 {
    147   Panic("Attempt to call SetBoolValue() on layered settings interface");
    148 }
    149 
    150 void LayeredSettingsInterface::SetStringValue(const char* section, const char* key, const char* value)
    151 {
    152   Panic("Attempt to call SetStringValue() on layered settings interface");
    153 }
    154 
    155 bool LayeredSettingsInterface::ContainsValue(const char* section, const char* key) const
    156 {
    157   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
    158   {
    159     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
    160     {
    161       if (sif->ContainsValue(section, key))
    162         return true;
    163     }
    164   }
    165   return false;
    166 }
    167 
    168 void LayeredSettingsInterface::DeleteValue(const char* section, const char* key)
    169 {
    170   Panic("Attempt to call DeleteValue() on layered settings interface");
    171 }
    172 
    173 void LayeredSettingsInterface::ClearSection(const char* section)
    174 {
    175   Panic("Attempt to call ClearSection() on layered settings interface");
    176 }
    177 
    178 void LayeredSettingsInterface::RemoveSection(const char* section)
    179 {
    180   Panic("Attempt to call RemoveSection() on layered settings interface");
    181 }
    182 
    183 void LayeredSettingsInterface::RemoveEmptySections()
    184 {
    185   Panic("Attempt to call RemoveEmptySections() on layered settings interface");
    186 }
    187 
    188 std::vector<std::string> LayeredSettingsInterface::GetStringList(const char* section, const char* key) const
    189 {
    190   std::vector<std::string> ret;
    191 
    192   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
    193   {
    194     if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
    195     {
    196       ret = sif->GetStringList(section, key);
    197       if (!ret.empty())
    198         break;
    199     }
    200   }
    201 
    202   return ret;
    203 }
    204 
    205 void LayeredSettingsInterface::SetStringList(const char* section, const char* key,
    206                                              const std::vector<std::string>& items)
    207 {
    208   Panic("Attempt to call SetStringList() on layered settings interface");
    209 }
    210 
    211 bool LayeredSettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item)
    212 {
    213   Panic("Attempt to call RemoveFromStringList() on layered settings interface");
    214 }
    215 
    216 bool LayeredSettingsInterface::AddToStringList(const char* section, const char* key, const char* item)
    217 {
    218   Panic("Attempt to call AddToStringList() on layered settings interface");
    219 }
    220 
    221 std::vector<std::pair<std::string, std::string>> LayeredSettingsInterface::GetKeyValueList(const char* section) const
    222 {
    223   std::unordered_set<std::string_view> seen;
    224   std::vector<std::pair<std::string, std::string>> ret;
    225   for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
    226   {
    227     if (SettingsInterface* sif = m_layers[layer])
    228     {
    229       const size_t newly_added_begin = ret.size();
    230       std::vector<std::pair<std::string, std::string>> entries = sif->GetKeyValueList(section);
    231       for (std::pair<std::string, std::string>& entry : entries)
    232       {
    233         if (seen.find(entry.first) != seen.end())
    234           continue;
    235         ret.push_back(std::move(entry));
    236       }
    237 
    238       // Mark keys as seen after processing all entries in case the layer has multiple entries for a specific key
    239       for (auto cur = ret.begin() + newly_added_begin, end = ret.end(); cur < end; cur++)
    240         seen.insert(cur->first);
    241     }
    242   }
    243 
    244   return ret;
    245 }
    246 
    247 void LayeredSettingsInterface::SetKeyValueList(const char* section,
    248                                                const std::vector<std::pair<std::string, std::string>>& items)
    249 {
    250   Panic("Attempt to call SetKeyValueList() on layered settings interface");
    251 }