Values_h.template 7.98 KB
Newer Older
1 2
// This file is generated by Values_h.template.

3 4 5 6 7 8 9 10 11 12
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef {{"_".join(config.protocol.namespace)}}_Values_h
#define {{"_".join(config.protocol.namespace)}}_Values_h

//#include "Allocator.h"
//#include "Forward.h"

13 14 15 16 17
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>

18 19
#include {{format_include(config.protocol.package, "Forward")}}

20 21 22 23 24 25 26 27
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}

class ListValue;
class DictionaryValue;
class Value;

28 29 30 31 32
#define PROTOCOL_DISALLOW_COPY(ClassName) \
 private:                                 \
  ClassName(const ClassName&) = delete;   \
  ClassName& operator=(const ClassName&) = delete

33
class {{config.lib.export_macro}} Value : public Serializable {
34 35
    PROTOCOL_DISALLOW_COPY(Value);
public:
36
    virtual ~Value() override { }
37 38 39

    static std::unique_ptr<Value> null()
    {
40
        return std::unique_ptr<Value>(new Value());
41 42
    }

43
    static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size);
44

45 46 47 48 49 50
    enum ValueType {
        TypeNull = 0,
        TypeBoolean,
        TypeInteger,
        TypeDouble,
        TypeString,
51
        TypeBinary,
52 53
        TypeObject,
        TypeArray,
54
        TypeImported
55 56 57 58 59 60 61 62 63 64
    };

    ValueType type() const { return m_type; }

    bool isNull() const { return m_type == TypeNull; }

    virtual bool asBoolean(bool* output) const;
    virtual bool asDouble(double* output) const;
    virtual bool asInteger(int* output) const;
    virtual bool asString(String* output) const;
65
    virtual bool asBinary(Binary* output) const;
66

67
    virtual void AppendSerialized(std::vector<uint8_t>* bytes) const override;
68
    virtual std::unique_ptr<Value> clone() const;
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
protected:
    Value() : m_type(TypeNull) { }
    explicit Value(ValueType type) : m_type(type) { }

private:
    friend class DictionaryValue;
    friend class ListValue;

    ValueType m_type;
};

class {{config.lib.export_macro}} FundamentalValue : public Value {
public:
    static std::unique_ptr<FundamentalValue> create(bool value)
    {
85
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
86 87 88 89
    }

    static std::unique_ptr<FundamentalValue> create(int value)
    {
90
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
91 92 93 94
    }

    static std::unique_ptr<FundamentalValue> create(double value)
    {
95
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
96 97 98 99 100
    }

    bool asBoolean(bool* output) const override;
    bool asDouble(double* output) const override;
    bool asInteger(int* output) const override;
101
    void AppendSerialized(std::vector<uint8_t>* bytes) const override;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    std::unique_ptr<Value> clone() const override;

private:
    explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { }
    explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { }
    explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { }

    union {
        bool m_boolValue;
        double m_doubleValue;
        int m_integerValue;
    };
};

class {{config.lib.export_macro}} StringValue : public Value {
public:
    static std::unique_ptr<StringValue> create(const String& value)
    {
120
        return std::unique_ptr<StringValue>(new StringValue(value));
121 122 123 124
    }

    static std::unique_ptr<StringValue> create(const char* value)
    {
125
        return std::unique_ptr<StringValue>(new StringValue(value));
126 127 128
    }

    bool asString(String* output) const override;
129
    void AppendSerialized(std::vector<uint8_t>* bytes) const override;
130 131 132 133 134 135 136 137 138
    std::unique_ptr<Value> clone() const override;

private:
    explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { }
    explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { }

    String m_stringValue;
};

139 140 141 142 143 144 145 146
class {{config.lib.export_macro}} BinaryValue : public Value {
public:
    static std::unique_ptr<BinaryValue> create(const Binary& value)
    {
        return std::unique_ptr<BinaryValue>(new BinaryValue(value));
    }

    bool asBinary(Binary* output) const override;
147
    void AppendSerialized(std::vector<uint8_t>* bytes) const override;
148 149 150 151 152 153 154 155
    std::unique_ptr<Value> clone() const override;

private:
    explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { }

    Binary m_binaryValue;
};

156 157 158 159 160
class {{config.lib.export_macro}} DictionaryValue : public Value {
public:
    using Entry = std::pair<String, Value*>;
    static std::unique_ptr<DictionaryValue> create()
    {
161
        return std::unique_ptr<DictionaryValue>(new DictionaryValue());
162 163 164 165 166 167 168 169 170 171 172
    }

    static DictionaryValue* cast(Value* value)
    {
        if (!value || value->type() != TypeObject)
            return nullptr;
        return static_cast<DictionaryValue*>(value);
    }

    static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value)
    {
173 174 175
        DictionaryValue* dictionaryValue = cast(value.get());
        if (dictionaryValue) value.release();
        return std::unique_ptr<DictionaryValue>(dictionaryValue);
176 177
    }

178
    void AppendSerialized(std::vector<uint8_t>* bytes) const override;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    std::unique_ptr<Value> clone() const override;

    size_t size() const { return m_data.size(); }

    void setBoolean(const String& name, bool);
    void setInteger(const String& name, int);
    void setDouble(const String& name, double);
    void setString(const String& name, const String&);
    void setValue(const String& name, std::unique_ptr<Value>);
    void setObject(const String& name, std::unique_ptr<DictionaryValue>);
    void setArray(const String& name, std::unique_ptr<ListValue>);

    bool getBoolean(const String& name, bool* output) const;
    bool getInteger(const String& name, int* output) const;
    bool getDouble(const String& name, double* output) const;
    bool getString(const String& name, String* output) const;

    DictionaryValue* getObject(const String& name) const;
    ListValue* getArray(const String& name) const;
    Value* get(const String& name) const;
    Entry at(size_t index) const;

    bool booleanProperty(const String& name, bool defaultValue) const;
    int integerProperty(const String& name, int defaultValue) const;
    double doubleProperty(const String& name, double defaultValue) const;
    void remove(const String& name);

    ~DictionaryValue() override;

private:
    DictionaryValue();
    template<typename T>
    void set(const String& key, std::unique_ptr<T>& value)
    {
        DCHECK(value);
        bool isNew = m_data.find(key) == m_data.end();
        m_data[key] = std::move(value);
        if (isNew)
            m_order.push_back(key);
    }

220
    using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>;
221 222 223 224 225 226 227 228
    Dictionary m_data;
    std::vector<String> m_order;
};

class {{config.lib.export_macro}} ListValue : public Value {
public:
    static std::unique_ptr<ListValue> create()
    {
229
        return std::unique_ptr<ListValue>(new ListValue());
230 231 232 233 234 235 236 237 238 239 240
    }

    static ListValue* cast(Value* value)
    {
        if (!value || value->type() != TypeArray)
            return nullptr;
        return static_cast<ListValue*>(value);
    }

    static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
    {
241 242 243
        ListValue* listValue = cast(value.get());
        if (listValue) value.release();
        return std::unique_ptr<ListValue>(listValue);
244 245 246 247
    }

    ~ListValue() override;

248
    void AppendSerialized(std::vector<uint8_t>* bytes) const override;
249 250 251 252 253 254
    std::unique_ptr<Value> clone() const override;

    void pushValue(std::unique_ptr<Value>);

    Value* at(size_t index);
    size_t size() const { return m_data.size(); }
255
    void reserve(size_t capacity) { m_data.reserve(capacity); }
256 257 258 259 260 261 262 263 264 265 266

private:
    ListValue();
    std::vector<std::unique_ptr<Value>> m_data;
};

{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}

#endif // {{"_".join(config.protocol.namespace)}}_Values_h