1 // Copyright 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
16 #define LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
17
18 // This file provides a set of helper functions to convert between base::Value
19 // and native types. Apart from handling standard types such as 'int' and
20 // 'std::string' it also provides conversion to/from std::vector<T> (which
21 // converts to Base::listValue) and std::map<std::string, T> (convertible to
22 // base::DictionaryValue).
23
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <utility>
28 #include <vector>
29
30 #include <base/values.h>
31 #include <brillo/brillo_export.h>
32
33 namespace brillo {
34
FromValue(const base::Value & in_value,bool * out_value)35 inline bool FromValue(const base::Value& in_value, bool* out_value) {
36 return in_value.GetAsBoolean(out_value);
37 }
38
FromValue(const base::Value & in_value,int * out_value)39 inline bool FromValue(const base::Value& in_value, int* out_value) {
40 return in_value.GetAsInteger(out_value);
41 }
42
FromValue(const base::Value & in_value,double * out_value)43 inline bool FromValue(const base::Value& in_value, double* out_value) {
44 return in_value.GetAsDouble(out_value);
45 }
46
FromValue(const base::Value & in_value,std::string * out_value)47 inline bool FromValue(const base::Value& in_value, std::string* out_value) {
48 return in_value.GetAsString(out_value);
49 }
50
FromValue(const base::Value & in_value,const base::ListValue ** out_value)51 inline bool FromValue(const base::Value& in_value,
52 const base::ListValue** out_value) {
53 return in_value.GetAsList(out_value);
54 }
55
FromValue(const base::Value & in_value,const base::DictionaryValue ** out_value)56 inline bool FromValue(const base::Value& in_value,
57 const base::DictionaryValue** out_value) {
58 return in_value.GetAsDictionary(out_value);
59 }
60
61 BRILLO_EXPORT bool FromValue(const base::Value& in_value,
62 std::unique_ptr<base::ListValue>* out_value);
63 BRILLO_EXPORT bool FromValue(const base::Value& in_value,
64 std::unique_ptr<base::DictionaryValue>* out_value);
65
66 template <typename T, typename Pred, typename Alloc>
67 bool FromValue(const base::Value& in_value,
68 std::map<std::string, T, Pred, Alloc>* out_value);
69
70 template <typename T, typename Alloc>
FromValue(const base::Value & in_value,std::vector<T,Alloc> * out_value)71 bool FromValue(const base::Value& in_value, std::vector<T, Alloc>* out_value) {
72 const base::ListValue* list = nullptr;
73 if (!in_value.GetAsList(&list))
74 return false;
75 out_value->clear();
76 out_value->reserve(list->GetSize());
77 for (const base::Value& item : *list) {
78 T value{};
79 if (!FromValue(item, &value))
80 return false;
81 out_value->push_back(std::move(value));
82 }
83 return true;
84 }
85
86 template <typename T, typename Pred, typename Alloc>
FromValue(const base::Value & in_value,std::map<std::string,T,Pred,Alloc> * out_value)87 bool FromValue(const base::Value& in_value,
88 std::map<std::string, T, Pred, Alloc>* out_value) {
89 const base::DictionaryValue* dict = nullptr;
90 if (!in_value.GetAsDictionary(&dict))
91 return false;
92 out_value->clear();
93 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
94 if (!FromValue(it.value(), &(*out_value)[it.key()]))
95 return false;
96 }
97 return true;
98 }
99
100 template <typename T>
FromValue(const base::Value & value)101 T FromValue(const base::Value& value) {
102 T out_value{};
103 CHECK(FromValue(value, &out_value));
104 return out_value;
105 }
106
107 BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(int value);
108 BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(bool value);
109 BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(double value);
110 BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(const std::string& value);
111 // Implicit conversion of char* to 'bool' has precedence over the user-defined
112 // std::string conversion. Override this behavior explicitly.
113 BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(const char* value);
114
115 template <typename T, typename Pred, typename Alloc>
116 std::unique_ptr<base::Value> ToValue(
117 const std::map<std::string, T, Pred, Alloc>& dictionary);
118
119 template <typename T, typename Alloc>
ToValue(const std::vector<T,Alloc> & list)120 std::unique_ptr<base::Value> ToValue(const std::vector<T, Alloc>& list) {
121 std::unique_ptr<base::ListValue> result{new base::ListValue};
122 for (const auto& value : list)
123 result->Append(ToValue(value));
124 return std::move(result);
125 }
126
127 template <typename T, typename Pred, typename Alloc>
ToValue(const std::map<std::string,T,Pred,Alloc> & dictionary)128 std::unique_ptr<base::Value> ToValue(
129 const std::map<std::string, T, Pred, Alloc>& dictionary) {
130 std::unique_ptr<base::DictionaryValue> result{new base::DictionaryValue};
131 for (const auto& pair : dictionary)
132 result->Set(pair.first, ToValue(pair.second));
133 return std::move(result);
134 }
135
136 } // namespace brillo
137
138 #endif // LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
139