Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -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_PROP_VALUES_H_ |
| 6 | #define BUFFET_COMMANDS_PROP_VALUES_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
Alex Vakulenko | a6fb4ed | 2014-08-22 15:05:35 -0700 | [diff] [blame] | 12 | #include <chromeos/any.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 13 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 15 | #include "buffet/commands/schema_utils.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | class Value; |
| 19 | class DictionaryValue; |
| 20 | } // namespace base |
| 21 | |
| 22 | namespace buffet { |
| 23 | |
| 24 | // Enumeration to indicate supported command parameter types. |
| 25 | enum class ValueType { |
| 26 | Int, |
| 27 | Double, |
| 28 | String, |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 29 | Boolean, |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 30 | Object, |
| 31 | Array, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 34 | class PropValue; |
| 35 | class IntValue; |
| 36 | class DoubleValue; |
| 37 | class StringValue; |
| 38 | class BooleanValue; |
| 39 | class ObjectValue; |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 40 | class ArrayValue; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 41 | |
| 42 | class PropType; |
| 43 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 44 | // Helper methods to get the parameter type enum value for the given |
| 45 | // native C++ data representation. |
| 46 | // The generic GetValueType<T>() is undefined, however particular |
| 47 | // type specializations return the appropriate ValueType. |
| 48 | template<typename T> ValueType GetValueType(); // Undefined. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 49 | template<> |
| 50 | inline ValueType GetValueType<int>() { return ValueType::Int; } |
| 51 | template<> |
| 52 | inline ValueType GetValueType<double>() { return ValueType::Double; } |
| 53 | template<> |
| 54 | inline ValueType GetValueType<std::string>() { return ValueType::String; } |
| 55 | template<> |
| 56 | inline ValueType GetValueType<bool>() { return ValueType::Boolean; } |
| 57 | template<> |
| 58 | inline ValueType GetValueType<native_types::Object>() { |
| 59 | return ValueType::Object; |
| 60 | } |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 61 | template<> |
| 62 | inline ValueType GetValueType<native_types::Array>() { |
| 63 | return ValueType::Array; |
| 64 | } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 65 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 66 | // The base class for property values. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 67 | // Concrete value classes of various types will be derived from this base. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 68 | // A property value is the actual command parameter value (or a concrete value |
| 69 | // that can be used in constraints and presets). The PropValue is mostly |
| 70 | // just parsed content of base::Value when a command is dispatched, however |
| 71 | // it does have some additional functionality: |
| 72 | // - it has a reference to the type definition (PropType) which is used |
| 73 | // when validating the value, especially for "object" types. |
| 74 | // - it can be compared with another instances of values of the same type. |
| 75 | // This is used to validate the values against "enum"/"one of" constraints. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 76 | class PropValue { |
| 77 | public: |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 78 | explicit PropValue(std::unique_ptr<const PropType> type); |
| 79 | // Special out-of-line constructor to help implement PropValue::Clone(). |
| 80 | // That method needs to clone the underlying type but can't do this in this |
| 81 | // header file since PropType is just forward-declared (it needs PropValue |
| 82 | // fully defined in its own inner workings). |
| 83 | explicit PropValue(const PropType* type_ptr); |
| 84 | virtual ~PropValue(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 85 | |
| 86 | // Gets the type of the value. |
| 87 | virtual ValueType GetType() const = 0; |
| 88 | |
| 89 | // Type conversion methods. Used in lieu of RTTI and dynamic_cast<>. |
| 90 | virtual IntValue* GetInt() { return nullptr; } |
| 91 | virtual IntValue const* GetInt() const { return nullptr; } |
| 92 | virtual DoubleValue* GetDouble() { return nullptr; } |
| 93 | virtual DoubleValue const* GetDouble() const { return nullptr; } |
| 94 | virtual StringValue* GetString() { return nullptr; } |
| 95 | virtual StringValue const* GetString() const { return nullptr; } |
| 96 | virtual BooleanValue* GetBoolean() { return nullptr; } |
| 97 | virtual BooleanValue const* GetBoolean() const { return nullptr; } |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 98 | virtual ObjectValue* GetObject() { return nullptr; } |
| 99 | virtual ObjectValue const* GetObject() const { return nullptr; } |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 100 | virtual ArrayValue* GetArray() { return nullptr; } |
| 101 | virtual ArrayValue const* GetArray() const { return nullptr; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 102 | |
| 103 | // Makes a full copy of this value class. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 104 | virtual std::unique_ptr<PropValue> Clone() const = 0; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 105 | |
| 106 | // Saves the value as a JSON object. |
| 107 | // If it fails, returns nullptr value and fills in the details for the |
| 108 | // failure in the |error| parameter. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 109 | virtual std::unique_ptr<base::Value> ToJson( |
| 110 | chromeos::ErrorPtr* error) const = 0; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 111 | // Parses a value from JSON. |
| 112 | // If it fails, it returns false and provides additional information |
| 113 | // via the |error| parameter. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 114 | virtual bool FromJson(const base::Value* value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 115 | chromeos::ErrorPtr* error) = 0; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 116 | |
| 117 | // Returns the contained C++ value as Any. |
Alex Vakulenko | a6fb4ed | 2014-08-22 15:05:35 -0700 | [diff] [blame] | 118 | virtual chromeos::Any GetValueAsAny() const = 0; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 119 | |
| 120 | // Return the type definition of this value. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 121 | const PropType* GetPropType() const { return type_.get(); } |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 122 | // Compares two values and returns true if they are equal. |
| 123 | virtual bool IsEqual(const PropValue* value) const = 0; |
| 124 | |
| 125 | protected: |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 126 | std::unique_ptr<const PropType> type_; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 129 | // A helper template base class for implementing value classes. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 130 | template<typename Derived, typename T> |
| 131 | class TypedValueBase : public PropValue { |
| 132 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 133 | // To help refer to this base class from derived classes, define Base to |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 134 | // be this class. |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 135 | using Base = TypedValueBase<Derived, T>; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 136 | // Expose the non-default constructor of the base class. |
| 137 | using PropValue::PropValue; |
| 138 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 139 | // Overrides from PropValue base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 140 | ValueType GetType() const override { return GetValueType<T>(); } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 141 | |
| 142 | std::unique_ptr<PropValue> Clone() const override { |
| 143 | std::unique_ptr<Derived> derived{new Derived{type_.get()}}; |
| 144 | derived->value_ = value_; |
| 145 | return std::move(derived); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 148 | std::unique_ptr<base::Value> ToJson( |
| 149 | chromeos::ErrorPtr* error) const override { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 150 | return TypedValueToJson(value_, error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 153 | bool FromJson(const base::Value* value, chromeos::ErrorPtr* error) override { |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 154 | return TypedValueFromJson(value, GetPropType(), &value_, error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 157 | bool IsEqual(const PropValue* value) const override { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 158 | if (GetType() != value->GetType()) |
| 159 | return false; |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 160 | const Base* value_base = static_cast<const Base*>(value); |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 161 | return CompareValue(GetValue(), value_base->GetValue()); |
| 162 | } |
| 163 | |
| 164 | // Helper methods to get and set the C++ representation of the value. |
Alex Vakulenko | a6fb4ed | 2014-08-22 15:05:35 -0700 | [diff] [blame] | 165 | chromeos::Any GetValueAsAny() const override { return value_; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 166 | const T& GetValue() const { return value_; } |
| 167 | void SetValue(T value) { value_ = std::move(value); } |
| 168 | |
| 169 | protected: |
| 170 | T value_{}; // The value of the parameter in C++ data representation. |
| 171 | }; |
| 172 | |
| 173 | // Value of type Integer. |
| 174 | class IntValue final : public TypedValueBase<IntValue, int> { |
| 175 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 176 | using Base::Base; // Expose the custom constructor of the base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 177 | IntValue* GetInt() override { return this; } |
| 178 | IntValue const* GetInt() const override { return this; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // Value of type Number. |
| 182 | class DoubleValue final : public TypedValueBase<DoubleValue, double> { |
| 183 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 184 | using Base::Base; // Expose the custom constructor of the base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 185 | DoubleValue* GetDouble() override { return this; } |
| 186 | DoubleValue const* GetDouble() const override { return this; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | // Value of type String. |
| 190 | class StringValue final : public TypedValueBase<StringValue, std::string> { |
| 191 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 192 | using Base::Base; // Expose the custom constructor of the base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 193 | StringValue* GetString() override { return this; } |
| 194 | StringValue const* GetString() const override { return this; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | // Value of type Boolean. |
| 198 | class BooleanValue final : public TypedValueBase<BooleanValue, bool> { |
| 199 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 200 | using Base::Base; // Expose the custom constructor of the base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 201 | BooleanValue* GetBoolean() override { return this; } |
| 202 | BooleanValue const* GetBoolean() const override { return this; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 205 | // Value of type Object. |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 206 | class ObjectValue final |
| 207 | : public TypedValueBase<ObjectValue, native_types::Object> { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 208 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 209 | using Base::Base; // Expose the custom constructor of the base class. |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 210 | ObjectValue* GetObject() override { return this; } |
| 211 | ObjectValue const* GetObject() const override { return this; } |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 212 | }; |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 213 | |
| 214 | // Value of type Array. |
| 215 | class ArrayValue final |
| 216 | : public TypedValueBase<ArrayValue, native_types::Array> { |
| 217 | public: |
| 218 | using Base::Base; // Expose the custom constructor of the base class. |
| 219 | ArrayValue* GetArray() override { return this; } |
| 220 | ArrayValue const* GetArray() const override { return this; } |
| 221 | }; |
| 222 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 223 | } // namespace buffet |
| 224 | |
| 225 | #endif // BUFFET_COMMANDS_PROP_VALUES_H_ |