Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BUFFET_COMMANDS_SCHEMA_UTILS_H_ |
| 6 | #define BUFFET_COMMANDS_SCHEMA_UTILS_H_ |
| 7 | |
| 8 | #include <limits> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <type_traits> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <base/values.h> |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 16 | #include <chromeos/any.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 17 | #include <chromeos/errors/error.h> |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 18 | #include <chromeos/variant_dictionary.h> |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 19 | |
| 20 | namespace buffet { |
| 21 | |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 22 | class PropType; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 23 | class PropValue; |
| 24 | class ObjectSchema; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 25 | class ObjectValue; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 26 | |
| 27 | namespace native_types { |
| 28 | // C++ representation of object values. |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 29 | using Object = std::map<std::string, std::shared_ptr<const PropValue>>; |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 30 | // C++ representation of array of values. |
| 31 | using Array = std::vector<std::shared_ptr<const PropValue>>; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 32 | } // namespace native_types |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 33 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 34 | // Converts an object to string. |
| 35 | std::string ToString(const native_types::Object& obj); |
| 36 | |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 37 | // Converts an array to string. |
| 38 | std::string ToString(const native_types::Array& arr); |
| 39 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 40 | // InheritableAttribute class is used for specifying various command parameter |
| 41 | // attributes that can be inherited from a base (parent) schema. |
| 42 | // The |value| still specifies the actual attribute values, whether it |
| 43 | // is inherited or overridden, while |is_inherited| can be used to identify |
| 44 | // if the attribute was inherited (true) or overridden (false). |
| 45 | template<typename T> |
| 46 | class InheritableAttribute { |
| 47 | public: |
| 48 | InheritableAttribute() = default; |
| 49 | explicit InheritableAttribute(T val) |
| 50 | : value(std::move(val)), is_inherited(true) {} |
| 51 | InheritableAttribute(T val, bool inherited) |
| 52 | : value(std::move(val)), is_inherited(inherited) {} |
| 53 | T value{}; |
| 54 | bool is_inherited{true}; |
| 55 | }; |
| 56 | |
| 57 | // A bunch of helper function to create base::Value for specific C++ classes, |
| 58 | // including vectors of types. These are used in template classes below |
| 59 | // to simplify specialization logic. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 60 | std::unique_ptr<base::Value> TypedValueToJson(bool value, |
| 61 | chromeos::ErrorPtr* error); |
| 62 | std::unique_ptr<base::Value> TypedValueToJson(int value, |
| 63 | chromeos::ErrorPtr* error); |
| 64 | std::unique_ptr<base::Value> TypedValueToJson(double value, |
| 65 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 66 | std::unique_ptr<base::Value> TypedValueToJson(const std::string& value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 67 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 68 | std::unique_ptr<base::Value> TypedValueToJson(const native_types::Object& value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 69 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 70 | std::unique_ptr<base::Value> TypedValueToJson(const native_types::Array& value, |
| 71 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 72 | template<typename T> |
| 73 | std::unique_ptr<base::Value> TypedValueToJson(const std::vector<T>& values, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 74 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 75 | std::unique_ptr<base::ListValue> list(new base::ListValue); |
| 76 | for (const auto& v : values) { |
| 77 | auto json = TypedValueToJson(v, error); |
| 78 | if (!json) |
| 79 | return std::unique_ptr<base::Value>(); |
| 80 | list->Append(json.release()); |
| 81 | } |
| 82 | return std::move(list); |
| 83 | } |
| 84 | |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 85 | // Similarly to TypedValueToJson() function above, the following overloaded |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 86 | // helper methods allow to extract specific C++ data types from base::Value. |
| 87 | // Also used in template classes below to simplify specialization logic. |
| 88 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 89 | const PropType* type, |
| 90 | bool* value_out, |
| 91 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 92 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 93 | const PropType* type, |
| 94 | int* value_out, |
| 95 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 96 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 97 | const PropType* type, |
| 98 | double* value_out, |
| 99 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 100 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 101 | const PropType* type, |
| 102 | std::string* value_out, |
| 103 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 104 | bool TypedValueFromJson(const base::Value* value_in, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 105 | const PropType* type, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 106 | native_types::Object* value_out, |
| 107 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 108 | bool TypedValueFromJson(const base::Value* value_in, |
| 109 | const PropType* type, |
| 110 | native_types::Array* value_out, |
| 111 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 112 | |
| 113 | bool operator==(const native_types::Object& obj1, |
| 114 | const native_types::Object& obj2); |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 115 | bool operator==(const native_types::Array& arr1, |
| 116 | const native_types::Array& arr2); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 117 | |
| 118 | // CompareValue is a helper function to help with implementing EqualsTo operator |
| 119 | // for various data types. For most scalar types it is using operator==(), |
| 120 | // however, for floating point values, rounding errors in binary representation |
| 121 | // of IEEE floats/doubles can cause straight == comparison to fail for seemingly |
| 122 | // equivalent values. For these, use approximate comparison with the error |
| 123 | // margin equal to the epsilon value defined for the corresponding data type. |
| 124 | // This is used when looking up values for implementation of OneOf constraints |
| 125 | // which should work reliably for floating points also ("number" type). |
| 126 | |
| 127 | // Compare exact types using ==. |
| 128 | template<typename T> |
| 129 | inline typename std::enable_if<!std::is_floating_point<T>::value, bool>::type |
| 130 | CompareValue(const T& v1, const T& v2) { |
| 131 | return v1 == v2; |
| 132 | } |
| 133 | |
| 134 | // Compare non-exact types (such as double) using precision margin (epsilon). |
| 135 | template<typename T> |
| 136 | inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type |
| 137 | CompareValue(const T& v1, const T& v2) { |
| 138 | return std::abs(v1 - v2) <= std::numeric_limits<T>::epsilon(); |
| 139 | } |
| 140 | |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 141 | // Converts PropValue to Any in a format understood by D-Bus data serialization. |
| 142 | // Has special handling for Object types where native_types::Object are |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 143 | // converted to chromeos::VariantDictionary. |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 144 | chromeos::Any PropValueToDBusVariant(const PropValue* value); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 145 | // Converts native_types::Object to chromeos::VariantDictionary |
| 146 | // with proper conversion of all nested properties. |
| 147 | chromeos::VariantDictionary |
| 148 | ObjectToDBusVariant(const native_types::Object& object); |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 149 | // Converts D-Bus variant to PropValue. |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 150 | // Has special handling for Object types where chromeos::VariantDictionary |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 151 | // is converted to native_types::Object. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 152 | std::unique_ptr<const PropValue> PropValueFromDBusVariant( |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 153 | const PropType* type, |
| 154 | const chromeos::Any& value, |
| 155 | chromeos::ErrorPtr* error); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 156 | // Converts D-Bus variant to ObjectValue. |
| 157 | bool ObjectFromDBusVariant(const ObjectSchema* object_schema, |
| 158 | const chromeos::VariantDictionary& dict, |
| 159 | native_types::Object* obj, |
| 160 | chromeos::ErrorPtr* error); |
Alex Vakulenko | a32d83a | 2014-09-19 15:05:24 -0700 | [diff] [blame] | 161 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 162 | } // namespace buffet |
| 163 | |
| 164 | #endif // BUFFET_COMMANDS_SCHEMA_UTILS_H_ |