1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_RECORDINGTYPES_H_
8 #define MOZILLA_GFX_RECORDINGTYPES_H_
18 template <class S
, class T
>
19 struct ElementStreamFormat
{
20 static void Write(S
& aStream
, const T
& aElement
) {
21 aStream
.write(reinterpret_cast<const char*>(&aElement
), sizeof(T
));
23 static void Read(S
& aStream
, T
& aElement
) {
24 aStream
.read(reinterpret_cast<char*>(&aElement
), sizeof(T
));
28 struct ElementStreamFormat
<S
, bool> {
29 static void Write(S
& aStream
, const bool& aElement
) {
30 char boolChar
= aElement
? '\x01' : '\x00';
31 aStream
.write(&boolChar
, sizeof(boolChar
));
33 static void Read(S
& aStream
, bool& aElement
) {
35 aStream
.read(&boolChar
, sizeof(boolChar
));
50 template <class S
, class T
>
51 void WriteElement(S
& aStream
, const T
& aElement
) {
52 ElementStreamFormat
<S
, T
>::Write(aStream
, aElement
);
54 template <class S
, class T
>
55 void WriteVector(S
& aStream
, const std::vector
<T
>& aVector
) {
56 size_t size
= aVector
.size();
57 WriteElement(aStream
, size
);
59 aStream
.write(reinterpret_cast<const char*>(aVector
.data()),
64 // ReadElement is disabled for enum types. Use ReadElementConstrained instead.
65 template <class S
, class T
,
66 typename
= typename
std::enable_if
<!std::is_enum
<T
>::value
>::type
>
67 void ReadElement(S
& aStream
, T
& aElement
) {
68 ElementStreamFormat
<S
, T
>::Read(aStream
, aElement
);
70 template <class S
, class T
>
71 void ReadElementConstrained(S
& aStream
, T
& aElement
, const T
& aMinValue
,
73 ElementStreamFormat
<S
, T
>::Read(aStream
, aElement
);
74 if (aElement
< aMinValue
|| aElement
> aMaxValue
) {
78 template <class S
, class T
>
79 void ReadVector(S
& aStream
, std::vector
<T
>& aVector
) {
81 ReadElement(aStream
, size
);
82 if (size
&& aStream
.good()) {
84 aStream
.read(reinterpret_cast<char*>(aVector
.data()), sizeof(T
) * size
);
91 } // namespace mozilla
93 #endif /* MOZILLA_GFX_RECORDINGTYPES_H_ */