Bug 1876335 - use GRADLE_MAVEN_REPOSITORIES in more places. r=owlish,geckoview-review...
[gecko.git] / gfx / 2d / RecordingTypes.h
blob94325b129599f871d28f501358ec207904a54d22
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_
10 #include <ostream>
11 #include <vector>
13 #include "Logging.h"
15 namespace mozilla {
16 namespace gfx {
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));
27 template <class S>
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) {
34 char boolChar;
35 aStream.read(&boolChar, sizeof(boolChar));
36 switch (boolChar) {
37 case '\x00':
38 aElement = false;
39 break;
40 case '\x01':
41 aElement = true;
42 break;
43 default:
44 aStream.SetIsBad();
45 break;
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);
58 if (size) {
59 aStream.write(reinterpret_cast<const char*>(aVector.data()),
60 sizeof(T) * size);
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,
72 const T& aMaxValue) {
73 ElementStreamFormat<S, T>::Read(aStream, aElement);
74 if (aElement < aMinValue || aElement > aMaxValue) {
75 aStream.SetIsBad();
78 template <class S, class T>
79 void ReadVector(S& aStream, std::vector<T>& aVector) {
80 size_t size;
81 ReadElement(aStream, size);
82 if (size && aStream.good()) {
83 aVector.resize(size);
84 aStream.read(reinterpret_cast<char*>(aVector.data()), sizeof(T) * size);
85 } else {
86 aVector.clear();
90 } // namespace gfx
91 } // namespace mozilla
93 #endif /* MOZILLA_GFX_RECORDINGTYPES_H_ */