2 * Copyright (C) 2013 University of Washington. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef EncodedValue_h
29 #define EncodedValue_h
31 #if ENABLE(WEB_REPLAY)
33 #include "InspectorValues.h"
34 #include <wtf/Forward.h>
35 #include <wtf/Vector.h>
39 class NondeterministicInputBase
;
40 template<typename T
> struct EncodingTraits
;
42 class EncodedValue final
{
44 explicit EncodedValue(PassRefPtr
<Inspector::InspectorValue
> value
)
48 : m_value(nullptr) { }
50 static EncodedValue
createObject()
52 return EncodedValue(Inspector::InspectorObject::create());
55 static EncodedValue
createArray()
57 return EncodedValue(Inspector::InspectorArray::create());
60 static EncodedValue
createString(const String
& value
)
62 return EncodedValue(Inspector::InspectorString::create(value
));
65 static EncodedValue
createString(const char* value
)
67 return EncodedValue(Inspector::InspectorString::create(value
));
71 void put(const String
&, const typename EncodingTraits
<T
>::DecodedType
&);
74 void append(const typename EncodingTraits
<T
>::DecodedType
&);
76 template<typename T
> bool get(const String
&, typename EncodingTraits
<T
>::DecodedType
&);
77 template<typename T
> bool get(const String
&, std::unique_ptr
<typename EncodingTraits
<T
>::DecodedType
>&);
79 template<typename T
> T
convertTo();
81 JS_EXPORT_PRIVATE PassRefPtr
<Inspector::InspectorObject
> asObject();
82 JS_EXPORT_PRIVATE PassRefPtr
<Inspector::InspectorArray
> asArray();
85 RefPtr
<Inspector::InspectorValue
> m_value
;
88 template<> JS_EXPORT_PRIVATE
bool EncodedValue::convertTo
<bool>();
89 template<> JS_EXPORT_PRIVATE
double EncodedValue::convertTo
<double>();
90 template<> JS_EXPORT_PRIVATE
float EncodedValue::convertTo
<float>();
91 template<> JS_EXPORT_PRIVATE
int32_t EncodedValue::convertTo
<int32_t>();
92 template<> JS_EXPORT_PRIVATE
int64_t EncodedValue::convertTo
<int64_t>();
93 template<> JS_EXPORT_PRIVATE
uint32_t EncodedValue::convertTo
<uint32_t>();
94 template<> JS_EXPORT_PRIVATE
uint64_t EncodedValue::convertTo
<uint64_t>();
95 template<> JS_EXPORT_PRIVATE
unsigned long EncodedValue::convertTo
<unsigned long>();
96 template<> JS_EXPORT_PRIVATE String
EncodedValue::convertTo
<String
>();
99 struct EncodingTraits
{
100 typedef T DecodedType
;
102 static EncodedValue
encodeValue(const DecodedType
&);
104 static bool decodeValue(EncodedValue
&, DecodedType
&);
105 static bool decodeValue(EncodedValue
&, std::unique_ptr
<DecodedType
>&);
108 template<typename T
, size_t inlineCapacity
, typename OverflowHandler
>
109 struct EncodingTraits
<Vector
<T
, inlineCapacity
, OverflowHandler
>> {
110 typedef Vector
<typename EncodingTraits
<T
>::DecodedType
, inlineCapacity
, OverflowHandler
> DecodedType
;
112 static EncodedValue
encodeValue(const DecodedType
& vectorOfValues
)
114 EncodedValue encodedVector
= EncodedValue::createArray();
115 for (const typename EncodingTraits
<T
>::DecodedType
& value
: vectorOfValues
)
116 encodedVector
.append
<typename EncodingTraits
<T
>::DecodedType
>(value
);
118 return WTF::move(encodedVector
);
121 static bool decodeValue(EncodedValue
& encodedVector
, DecodedType
& decodedValue
)
123 RefPtr
<Inspector::InspectorArray
> inspectorArray
= encodedVector
.asArray();
124 decodedValue
= Vector
<typename EncodingTraits
<T
>::DecodedType
, inlineCapacity
, OverflowHandler
>(inspectorArray
->length());
125 for (size_t i
= 0; i
< inspectorArray
->length(); ++i
) {
126 EncodedValue
encodedElement(inspectorArray
->get(i
));
127 if (!EncodingTraits
<T
>::decodeValue(encodedElement
, decodedValue
.at(i
)))
134 template<> struct EncodingTraits
<EncodedValue
> {
135 typedef EncodedValue DecodedType
;
136 // We should never attempt to decode or encode an encoded value,
137 // so encodeValue and decodeValue are intentionally omitted here.
141 struct ScalarEncodingTraits
{
142 typedef T DecodedType
;
144 static JS_EXPORT_PRIVATE EncodedValue
encodeValue(const DecodedType
& decodedValue
);
145 static bool decodeValue(EncodedValue
& encodedValue
, DecodedType
& decodedValue
)
147 decodedValue
= encodedValue
.convertTo
<DecodedType
>();
152 template<> struct EncodingTraits
<bool> : public ScalarEncodingTraits
<bool> { };
153 template<> struct EncodingTraits
<double> : public ScalarEncodingTraits
<double> { };
154 template<> struct EncodingTraits
<float> : public ScalarEncodingTraits
<float> { };
155 template<> struct EncodingTraits
<int32_t> : public ScalarEncodingTraits
<int32_t> { };
156 template<> struct EncodingTraits
<int64_t> : public ScalarEncodingTraits
<int64_t> { };
157 template<> struct EncodingTraits
<uint32_t> : public ScalarEncodingTraits
<uint32_t> { };
158 template<> struct EncodingTraits
<uint64_t> : public ScalarEncodingTraits
<uint64_t> { };
159 template<> struct EncodingTraits
<unsigned long> : public ScalarEncodingTraits
<unsigned long> { };
161 template<> struct EncodingTraits
<String
> : public ScalarEncodingTraits
<String
> {
162 static EncodedValue
encodeValue(const String
& value
)
164 return EncodedValue::createString(value
);
168 // Base cases for loading and storing values.
169 template<> JS_EXPORT_PRIVATE
170 void EncodedValue::put
<EncodedValue
>(const String
& key
, const typename EncodingTraits
<EncodedValue
>::DecodedType
&);
172 template<> JS_EXPORT_PRIVATE
173 void EncodedValue::append
<EncodedValue
>(const typename EncodingTraits
<EncodedValue
>::DecodedType
&);
175 template<> JS_EXPORT_PRIVATE
176 bool EncodedValue::get
<EncodedValue
>(const String
& key
, typename EncodingTraits
<EncodedValue
>::DecodedType
&);
178 // Load and store types with an accompanying EncodingTraits implementation.
180 void EncodedValue::put(const String
& key
, const typename EncodingTraits
<T
>::DecodedType
& value
)
182 EncodedValue encodedValue
= EncodingTraits
<T
>::encodeValue(value
);
183 put
<EncodedValue
>(key
, encodedValue
);
187 void EncodedValue::append(const typename EncodingTraits
<T
>::DecodedType
& value
)
189 EncodedValue encodedValue
= EncodingTraits
<T
>::encodeValue(value
);
190 append
<EncodedValue
>(encodedValue
);
194 bool EncodedValue::get(const String
& key
, typename EncodingTraits
<T
>::DecodedType
& decodedValue
)
196 EncodedValue encodedValue
;
197 if (!get
<EncodedValue
>(key
, encodedValue
))
200 return EncodingTraits
<T
>::decodeValue(encodedValue
, decodedValue
);
204 bool EncodedValue::get(const String
& key
, std::unique_ptr
<typename EncodingTraits
<T
>::DecodedType
>& decodedValue
)
206 EncodedValue encodedValue
;
207 if (!get
<EncodedValue
>(key
, encodedValue
))
210 return EncodingTraits
<T
>::decodeValue(encodedValue
, decodedValue
);
215 using JSC::EncodedValue
;
216 using JSC::EncodingTraits
;
218 #endif // ENABLE(WEB_REPLAY)
220 #endif // EncodedValue_h