11cb0ef41Sopenharmony_ci// This file is generated by Values_h.template. 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved. 41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 51cb0ef41Sopenharmony_ci// found in the LICENSE file. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci#ifndef {{"_".join(config.protocol.namespace)}}_Values_h 81cb0ef41Sopenharmony_ci#define {{"_".join(config.protocol.namespace)}}_Values_h 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci//#include "Allocator.h" 111cb0ef41Sopenharmony_ci//#include "Forward.h" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %} 141cb0ef41Sopenharmony_cinamespace {{namespace}} { 151cb0ef41Sopenharmony_ci{% endfor %} 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciclass ListValue; 181cb0ef41Sopenharmony_ciclass DictionaryValue; 191cb0ef41Sopenharmony_ciclass Value; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} Value : public Serializable { 221cb0ef41Sopenharmony_ci PROTOCOL_DISALLOW_COPY(Value); 231cb0ef41Sopenharmony_cipublic: 241cb0ef41Sopenharmony_ci virtual ~Value() override { } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci static std::unique_ptr<Value> null() 271cb0ef41Sopenharmony_ci { 281cb0ef41Sopenharmony_ci return std::unique_ptr<Value>(new Value()); 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci enum ValueType { 341cb0ef41Sopenharmony_ci TypeNull = 0, 351cb0ef41Sopenharmony_ci TypeBoolean, 361cb0ef41Sopenharmony_ci TypeInteger, 371cb0ef41Sopenharmony_ci TypeDouble, 381cb0ef41Sopenharmony_ci TypeString, 391cb0ef41Sopenharmony_ci TypeBinary, 401cb0ef41Sopenharmony_ci TypeObject, 411cb0ef41Sopenharmony_ci TypeArray, 421cb0ef41Sopenharmony_ci TypeSerialized, 431cb0ef41Sopenharmony_ci TypeImported 441cb0ef41Sopenharmony_ci }; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci ValueType type() const { return m_type; } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci bool isNull() const { return m_type == TypeNull; } 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci virtual bool asBoolean(bool* output) const; 511cb0ef41Sopenharmony_ci virtual bool asDouble(double* output) const; 521cb0ef41Sopenharmony_ci virtual bool asInteger(int* output) const; 531cb0ef41Sopenharmony_ci virtual bool asString(String* output) const; 541cb0ef41Sopenharmony_ci virtual bool asBinary(Binary* output) const; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci virtual void writeJSON(StringBuilder* output) const; 571cb0ef41Sopenharmony_ci virtual void writeBinary(std::vector<uint8_t>* bytes) const; 581cb0ef41Sopenharmony_ci virtual std::unique_ptr<Value> clone() const; 591cb0ef41Sopenharmony_ci String toJSONString() const; 601cb0ef41Sopenharmony_ci String serializeToJSON() override; 611cb0ef41Sopenharmony_ci std::vector<uint8_t> serializeToBinary() override; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciprotected: 641cb0ef41Sopenharmony_ci Value() : m_type(TypeNull) { } 651cb0ef41Sopenharmony_ci explicit Value(ValueType type) : m_type(type) { } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciprivate: 681cb0ef41Sopenharmony_ci friend class DictionaryValue; 691cb0ef41Sopenharmony_ci friend class ListValue; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci ValueType m_type; 721cb0ef41Sopenharmony_ci}; 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} FundamentalValue : public Value { 751cb0ef41Sopenharmony_cipublic: 761cb0ef41Sopenharmony_ci static std::unique_ptr<FundamentalValue> create(bool value) 771cb0ef41Sopenharmony_ci { 781cb0ef41Sopenharmony_ci return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci static std::unique_ptr<FundamentalValue> create(int value) 821cb0ef41Sopenharmony_ci { 831cb0ef41Sopenharmony_ci return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci static std::unique_ptr<FundamentalValue> create(double value) 871cb0ef41Sopenharmony_ci { 881cb0ef41Sopenharmony_ci return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci bool asBoolean(bool* output) const override; 921cb0ef41Sopenharmony_ci bool asDouble(double* output) const override; 931cb0ef41Sopenharmony_ci bool asInteger(int* output) const override; 941cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 951cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 961cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciprivate: 991cb0ef41Sopenharmony_ci explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { } 1001cb0ef41Sopenharmony_ci explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { } 1011cb0ef41Sopenharmony_ci explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { } 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci union { 1041cb0ef41Sopenharmony_ci bool m_boolValue; 1051cb0ef41Sopenharmony_ci double m_doubleValue; 1061cb0ef41Sopenharmony_ci int m_integerValue; 1071cb0ef41Sopenharmony_ci }; 1081cb0ef41Sopenharmony_ci}; 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} StringValue : public Value { 1111cb0ef41Sopenharmony_cipublic: 1121cb0ef41Sopenharmony_ci static std::unique_ptr<StringValue> create(const String& value) 1131cb0ef41Sopenharmony_ci { 1141cb0ef41Sopenharmony_ci return std::unique_ptr<StringValue>(new StringValue(value)); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci static std::unique_ptr<StringValue> create(const char* value) 1181cb0ef41Sopenharmony_ci { 1191cb0ef41Sopenharmony_ci return std::unique_ptr<StringValue>(new StringValue(value)); 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci bool asString(String* output) const override; 1231cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 1241cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 1251cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciprivate: 1281cb0ef41Sopenharmony_ci explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { } 1291cb0ef41Sopenharmony_ci explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { } 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci String m_stringValue; 1321cb0ef41Sopenharmony_ci}; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} BinaryValue : public Value { 1351cb0ef41Sopenharmony_cipublic: 1361cb0ef41Sopenharmony_ci static std::unique_ptr<BinaryValue> create(const Binary& value) 1371cb0ef41Sopenharmony_ci { 1381cb0ef41Sopenharmony_ci return std::unique_ptr<BinaryValue>(new BinaryValue(value)); 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci bool asBinary(Binary* output) const override; 1421cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 1431cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 1441cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ciprivate: 1471cb0ef41Sopenharmony_ci explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { } 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci Binary m_binaryValue; 1501cb0ef41Sopenharmony_ci}; 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} SerializedValue : public Value { 1531cb0ef41Sopenharmony_cipublic: 1541cb0ef41Sopenharmony_ci static std::unique_ptr<SerializedValue> fromJSON(const String& value) 1551cb0ef41Sopenharmony_ci { 1561cb0ef41Sopenharmony_ci return std::unique_ptr<SerializedValue>(new SerializedValue(value)); 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci static std::unique_ptr<SerializedValue> fromBinary(std::vector<uint8_t> value) 1601cb0ef41Sopenharmony_ci { 1611cb0ef41Sopenharmony_ci return std::unique_ptr<SerializedValue>(new SerializedValue(std::move(value))); 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 1651cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 1661cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciprivate: 1691cb0ef41Sopenharmony_ci explicit SerializedValue(const String& json) : Value(TypeSerialized), m_serializedJSON(json) { } 1701cb0ef41Sopenharmony_ci explicit SerializedValue(std::vector<uint8_t> binary) : Value(TypeSerialized), m_serializedBinary(std::move(binary)) { } 1711cb0ef41Sopenharmony_ci SerializedValue(const String& json, const std::vector<uint8_t>& binary) 1721cb0ef41Sopenharmony_ci : Value(TypeSerialized), m_serializedJSON(json), m_serializedBinary(binary) { } 1731cb0ef41Sopenharmony_ci String m_serializedJSON; 1741cb0ef41Sopenharmony_ci std::vector<uint8_t> m_serializedBinary; 1751cb0ef41Sopenharmony_ci}; 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} DictionaryValue : public Value { 1781cb0ef41Sopenharmony_cipublic: 1791cb0ef41Sopenharmony_ci using Entry = std::pair<String, Value*>; 1801cb0ef41Sopenharmony_ci static std::unique_ptr<DictionaryValue> create() 1811cb0ef41Sopenharmony_ci { 1821cb0ef41Sopenharmony_ci return std::unique_ptr<DictionaryValue>(new DictionaryValue()); 1831cb0ef41Sopenharmony_ci } 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci static DictionaryValue* cast(Value* value) 1861cb0ef41Sopenharmony_ci { 1871cb0ef41Sopenharmony_ci if (!value || value->type() != TypeObject) 1881cb0ef41Sopenharmony_ci return nullptr; 1891cb0ef41Sopenharmony_ci return static_cast<DictionaryValue*>(value); 1901cb0ef41Sopenharmony_ci } 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) 1931cb0ef41Sopenharmony_ci { 1941cb0ef41Sopenharmony_ci return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.release())); 1951cb0ef41Sopenharmony_ci } 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 1981cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 1991cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci size_t size() const { return m_data.size(); } 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci void setBoolean(const String& name, bool); 2041cb0ef41Sopenharmony_ci void setInteger(const String& name, int); 2051cb0ef41Sopenharmony_ci void setDouble(const String& name, double); 2061cb0ef41Sopenharmony_ci void setString(const String& name, const String&); 2071cb0ef41Sopenharmony_ci void setValue(const String& name, std::unique_ptr<Value>); 2081cb0ef41Sopenharmony_ci void setObject(const String& name, std::unique_ptr<DictionaryValue>); 2091cb0ef41Sopenharmony_ci void setArray(const String& name, std::unique_ptr<ListValue>); 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci bool getBoolean(const String& name, bool* output) const; 2121cb0ef41Sopenharmony_ci bool getInteger(const String& name, int* output) const; 2131cb0ef41Sopenharmony_ci bool getDouble(const String& name, double* output) const; 2141cb0ef41Sopenharmony_ci bool getString(const String& name, String* output) const; 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci DictionaryValue* getObject(const String& name) const; 2171cb0ef41Sopenharmony_ci ListValue* getArray(const String& name) const; 2181cb0ef41Sopenharmony_ci Value* get(const String& name) const; 2191cb0ef41Sopenharmony_ci Entry at(size_t index) const; 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci bool booleanProperty(const String& name, bool defaultValue) const; 2221cb0ef41Sopenharmony_ci int integerProperty(const String& name, int defaultValue) const; 2231cb0ef41Sopenharmony_ci double doubleProperty(const String& name, double defaultValue) const; 2241cb0ef41Sopenharmony_ci void remove(const String& name); 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci ~DictionaryValue() override; 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ciprivate: 2291cb0ef41Sopenharmony_ci DictionaryValue(); 2301cb0ef41Sopenharmony_ci template<typename T> 2311cb0ef41Sopenharmony_ci void set(const String& key, std::unique_ptr<T>& value) 2321cb0ef41Sopenharmony_ci { 2331cb0ef41Sopenharmony_ci DCHECK(value); 2341cb0ef41Sopenharmony_ci bool isNew = m_data.find(key) == m_data.end(); 2351cb0ef41Sopenharmony_ci m_data[key] = std::move(value); 2361cb0ef41Sopenharmony_ci if (isNew) 2371cb0ef41Sopenharmony_ci m_order.push_back(key); 2381cb0ef41Sopenharmony_ci } 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>; 2411cb0ef41Sopenharmony_ci Dictionary m_data; 2421cb0ef41Sopenharmony_ci std::vector<String> m_order; 2431cb0ef41Sopenharmony_ci}; 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} ListValue : public Value { 2461cb0ef41Sopenharmony_cipublic: 2471cb0ef41Sopenharmony_ci static std::unique_ptr<ListValue> create() 2481cb0ef41Sopenharmony_ci { 2491cb0ef41Sopenharmony_ci return std::unique_ptr<ListValue>(new ListValue()); 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci static ListValue* cast(Value* value) 2531cb0ef41Sopenharmony_ci { 2541cb0ef41Sopenharmony_ci if (!value || value->type() != TypeArray) 2551cb0ef41Sopenharmony_ci return nullptr; 2561cb0ef41Sopenharmony_ci return static_cast<ListValue*>(value); 2571cb0ef41Sopenharmony_ci } 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) 2601cb0ef41Sopenharmony_ci { 2611cb0ef41Sopenharmony_ci return std::unique_ptr<ListValue>(ListValue::cast(value.release())); 2621cb0ef41Sopenharmony_ci } 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci ~ListValue() override; 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci void writeJSON(StringBuilder* output) const override; 2671cb0ef41Sopenharmony_ci void writeBinary(std::vector<uint8_t>* bytes) const override; 2681cb0ef41Sopenharmony_ci std::unique_ptr<Value> clone() const override; 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci void pushValue(std::unique_ptr<Value>); 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ci Value* at(size_t index); 2731cb0ef41Sopenharmony_ci size_t size() const { return m_data.size(); } 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ciprivate: 2761cb0ef41Sopenharmony_ci ListValue(); 2771cb0ef41Sopenharmony_ci std::vector<std::unique_ptr<Value>> m_data; 2781cb0ef41Sopenharmony_ci}; 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_civoid escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst); 2811cb0ef41Sopenharmony_civoid escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst); 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %} 2841cb0ef41Sopenharmony_ci} // namespace {{namespace}} 2851cb0ef41Sopenharmony_ci{% endfor %} 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci#endif // {{"_".join(config.protocol.namespace)}}_Values_h 288