1// This file is generated by Values_h.template. 2 3// Copyright 2016 The Chromium Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style license that can be 5// found in the LICENSE file. 6 7#ifndef {{"_".join(config.protocol.namespace)}}_Values_h 8#define {{"_".join(config.protocol.namespace)}}_Values_h 9 10//#include "Allocator.h" 11//#include "Forward.h" 12 13{% for namespace in config.protocol.namespace %} 14namespace {{namespace}} { 15{% endfor %} 16 17class ListValue; 18class DictionaryValue; 19class Value; 20 21class {{config.lib.export_macro}} Value : public Serializable { 22 PROTOCOL_DISALLOW_COPY(Value); 23public: 24 virtual ~Value() override { } 25 26 static std::unique_ptr<Value> null() 27 { 28 return std::unique_ptr<Value>(new Value()); 29 } 30 31 static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size); 32 33 enum ValueType { 34 TypeNull = 0, 35 TypeBoolean, 36 TypeInteger, 37 TypeDouble, 38 TypeString, 39 TypeBinary, 40 TypeObject, 41 TypeArray, 42 TypeSerialized, 43 TypeImported 44 }; 45 46 ValueType type() const { return m_type; } 47 48 bool isNull() const { return m_type == TypeNull; } 49 50 virtual bool asBoolean(bool* output) const; 51 virtual bool asDouble(double* output) const; 52 virtual bool asInteger(int* output) const; 53 virtual bool asString(String* output) const; 54 virtual bool asBinary(Binary* output) const; 55 56 virtual void writeJSON(StringBuilder* output) const; 57 virtual void writeBinary(std::vector<uint8_t>* bytes) const; 58 virtual std::unique_ptr<Value> clone() const; 59 String toJSONString() const; 60 String serializeToJSON() override; 61 std::vector<uint8_t> serializeToBinary() override; 62 63protected: 64 Value() : m_type(TypeNull) { } 65 explicit Value(ValueType type) : m_type(type) { } 66 67private: 68 friend class DictionaryValue; 69 friend class ListValue; 70 71 ValueType m_type; 72}; 73 74class {{config.lib.export_macro}} FundamentalValue : public Value { 75public: 76 static std::unique_ptr<FundamentalValue> create(bool value) 77 { 78 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 79 } 80 81 static std::unique_ptr<FundamentalValue> create(int value) 82 { 83 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 84 } 85 86 static std::unique_ptr<FundamentalValue> create(double value) 87 { 88 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); 89 } 90 91 bool asBoolean(bool* output) const override; 92 bool asDouble(double* output) const override; 93 bool asInteger(int* output) const override; 94 void writeJSON(StringBuilder* output) const override; 95 void writeBinary(std::vector<uint8_t>* bytes) const override; 96 std::unique_ptr<Value> clone() const override; 97 98private: 99 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { } 100 explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { } 101 explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { } 102 103 union { 104 bool m_boolValue; 105 double m_doubleValue; 106 int m_integerValue; 107 }; 108}; 109 110class {{config.lib.export_macro}} StringValue : public Value { 111public: 112 static std::unique_ptr<StringValue> create(const String& value) 113 { 114 return std::unique_ptr<StringValue>(new StringValue(value)); 115 } 116 117 static std::unique_ptr<StringValue> create(const char* value) 118 { 119 return std::unique_ptr<StringValue>(new StringValue(value)); 120 } 121 122 bool asString(String* output) const override; 123 void writeJSON(StringBuilder* output) const override; 124 void writeBinary(std::vector<uint8_t>* bytes) const override; 125 std::unique_ptr<Value> clone() const override; 126 127private: 128 explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { } 129 explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { } 130 131 String m_stringValue; 132}; 133 134class {{config.lib.export_macro}} BinaryValue : public Value { 135public: 136 static std::unique_ptr<BinaryValue> create(const Binary& value) 137 { 138 return std::unique_ptr<BinaryValue>(new BinaryValue(value)); 139 } 140 141 bool asBinary(Binary* output) const override; 142 void writeJSON(StringBuilder* output) const override; 143 void writeBinary(std::vector<uint8_t>* bytes) const override; 144 std::unique_ptr<Value> clone() const override; 145 146private: 147 explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { } 148 149 Binary m_binaryValue; 150}; 151 152class {{config.lib.export_macro}} SerializedValue : public Value { 153public: 154 static std::unique_ptr<SerializedValue> fromJSON(const String& value) 155 { 156 return std::unique_ptr<SerializedValue>(new SerializedValue(value)); 157 } 158 159 static std::unique_ptr<SerializedValue> fromBinary(std::vector<uint8_t> value) 160 { 161 return std::unique_ptr<SerializedValue>(new SerializedValue(std::move(value))); 162 } 163 164 void writeJSON(StringBuilder* output) const override; 165 void writeBinary(std::vector<uint8_t>* bytes) const override; 166 std::unique_ptr<Value> clone() const override; 167 168private: 169 explicit SerializedValue(const String& json) : Value(TypeSerialized), m_serializedJSON(json) { } 170 explicit SerializedValue(std::vector<uint8_t> binary) : Value(TypeSerialized), m_serializedBinary(std::move(binary)) { } 171 SerializedValue(const String& json, const std::vector<uint8_t>& binary) 172 : Value(TypeSerialized), m_serializedJSON(json), m_serializedBinary(binary) { } 173 String m_serializedJSON; 174 std::vector<uint8_t> m_serializedBinary; 175}; 176 177class {{config.lib.export_macro}} DictionaryValue : public Value { 178public: 179 using Entry = std::pair<String, Value*>; 180 static std::unique_ptr<DictionaryValue> create() 181 { 182 return std::unique_ptr<DictionaryValue>(new DictionaryValue()); 183 } 184 185 static DictionaryValue* cast(Value* value) 186 { 187 if (!value || value->type() != TypeObject) 188 return nullptr; 189 return static_cast<DictionaryValue*>(value); 190 } 191 192 static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) 193 { 194 return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.release())); 195 } 196 197 void writeJSON(StringBuilder* output) const override; 198 void writeBinary(std::vector<uint8_t>* bytes) const override; 199 std::unique_ptr<Value> clone() const override; 200 201 size_t size() const { return m_data.size(); } 202 203 void setBoolean(const String& name, bool); 204 void setInteger(const String& name, int); 205 void setDouble(const String& name, double); 206 void setString(const String& name, const String&); 207 void setValue(const String& name, std::unique_ptr<Value>); 208 void setObject(const String& name, std::unique_ptr<DictionaryValue>); 209 void setArray(const String& name, std::unique_ptr<ListValue>); 210 211 bool getBoolean(const String& name, bool* output) const; 212 bool getInteger(const String& name, int* output) const; 213 bool getDouble(const String& name, double* output) const; 214 bool getString(const String& name, String* output) const; 215 216 DictionaryValue* getObject(const String& name) const; 217 ListValue* getArray(const String& name) const; 218 Value* get(const String& name) const; 219 Entry at(size_t index) const; 220 221 bool booleanProperty(const String& name, bool defaultValue) const; 222 int integerProperty(const String& name, int defaultValue) const; 223 double doubleProperty(const String& name, double defaultValue) const; 224 void remove(const String& name); 225 226 ~DictionaryValue() override; 227 228private: 229 DictionaryValue(); 230 template<typename T> 231 void set(const String& key, std::unique_ptr<T>& value) 232 { 233 DCHECK(value); 234 bool isNew = m_data.find(key) == m_data.end(); 235 m_data[key] = std::move(value); 236 if (isNew) 237 m_order.push_back(key); 238 } 239 240 using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>; 241 Dictionary m_data; 242 std::vector<String> m_order; 243}; 244 245class {{config.lib.export_macro}} ListValue : public Value { 246public: 247 static std::unique_ptr<ListValue> create() 248 { 249 return std::unique_ptr<ListValue>(new ListValue()); 250 } 251 252 static ListValue* cast(Value* value) 253 { 254 if (!value || value->type() != TypeArray) 255 return nullptr; 256 return static_cast<ListValue*>(value); 257 } 258 259 static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) 260 { 261 return std::unique_ptr<ListValue>(ListValue::cast(value.release())); 262 } 263 264 ~ListValue() override; 265 266 void writeJSON(StringBuilder* output) const override; 267 void writeBinary(std::vector<uint8_t>* bytes) const override; 268 std::unique_ptr<Value> clone() const override; 269 270 void pushValue(std::unique_ptr<Value>); 271 272 Value* at(size_t index); 273 size_t size() const { return m_data.size(); } 274 275private: 276 ListValue(); 277 std::vector<std::unique_ptr<Value>> m_data; 278}; 279 280void escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst); 281void escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst); 282 283{% for namespace in config.protocol.namespace %} 284} // namespace {{namespace}} 285{% endfor %} 286 287#endif // {{"_".join(config.protocol.namespace)}}_Values_h 288