[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / settings / SettingUtils.cpp
blobc6909320688a59235152347d08f42ff9c44c4458
1 /*
2 * Copyright (C) 2013-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "SettingUtils.h"
11 #include "settings/lib/Setting.h"
12 #include "utils/StringUtils.h"
13 #include "utils/Variant.h"
15 #include <algorithm>
17 std::vector<CVariant> CSettingUtils::GetList(const std::shared_ptr<const CSettingList>& settingList)
19 return ListToValues(settingList, settingList->GetValue());
22 bool CSettingUtils::SetList(const std::shared_ptr<CSettingList>& settingList,
23 const std::vector<CVariant>& value)
25 SettingList newValues;
26 if (!ValuesToList(settingList, value, newValues))
27 return false;
29 return settingList->SetValue(newValues);
32 std::vector<CVariant> CSettingUtils::ListToValues(
33 const std::shared_ptr<const CSettingList>& setting,
34 const std::vector<std::shared_ptr<CSetting>>& values)
36 std::vector<CVariant> realValues;
38 if (setting == NULL)
39 return realValues;
41 for (const auto& value : values)
43 switch (setting->GetElementType())
45 case SettingType::Boolean:
46 realValues.emplace_back(std::static_pointer_cast<const CSettingBool>(value)->GetValue());
47 break;
49 case SettingType::Integer:
50 realValues.emplace_back(std::static_pointer_cast<const CSettingInt>(value)->GetValue());
51 break;
53 case SettingType::Number:
54 realValues.emplace_back(std::static_pointer_cast<const CSettingNumber>(value)->GetValue());
55 break;
57 case SettingType::String:
58 realValues.emplace_back(std::static_pointer_cast<const CSettingString>(value)->GetValue());
59 break;
61 default:
62 break;
66 return realValues;
69 bool CSettingUtils::ValuesToList(const std::shared_ptr<const CSettingList>& setting,
70 const std::vector<CVariant>& values,
71 std::vector<std::shared_ptr<CSetting>>& newValues)
73 if (setting == NULL)
74 return false;
76 int index = 0;
77 bool ret = true;
78 for (const auto& value : values)
80 SettingPtr settingValue =
81 setting->GetDefinition()->Clone(StringUtils::Format("{}.{}", setting->GetId(), index++));
82 if (settingValue == NULL)
83 return false;
85 switch (setting->GetElementType())
87 case SettingType::Boolean:
88 if (!value.isBoolean())
89 ret = false;
90 else
91 ret = std::static_pointer_cast<CSettingBool>(settingValue)->SetValue(value.asBoolean());
92 break;
94 case SettingType::Integer:
95 if (!value.isInteger())
96 ret = false;
97 else
98 ret = std::static_pointer_cast<CSettingInt>(settingValue)->SetValue(static_cast<int>(value.asInteger()));
99 break;
101 case SettingType::Number:
102 if (!value.isDouble())
103 ret = false;
104 else
105 ret = std::static_pointer_cast<CSettingNumber>(settingValue)->SetValue(value.asDouble());
106 break;
108 case SettingType::String:
109 if (!value.isString())
110 ret = false;
111 else
112 ret = std::static_pointer_cast<CSettingString>(settingValue)->SetValue(value.asString());
113 break;
115 default:
116 ret = false;
117 break;
120 if (!ret)
121 return false;
123 newValues.push_back(std::const_pointer_cast<CSetting>(settingValue));
126 return true;
129 bool CSettingUtils::FindIntInList(const std::shared_ptr<const CSettingList>& settingList, int value)
131 if (settingList == nullptr || settingList->GetElementType() != SettingType::Integer)
132 return false;
134 const auto values = settingList->GetValue();
135 const auto matchingValue =
136 std::find_if(values.begin(), values.end(), [value](const SettingPtr& setting) {
137 return std::static_pointer_cast<CSettingInt>(setting)->GetValue() == value;
139 return matchingValue != values.end();