Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/BaseSize.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef MOZILLA_GFX_BASESIZE_H_
8
#define MOZILLA_GFX_BASESIZE_H_
9
10
#include <algorithm>
11
#include <ostream>
12
13
#include "mozilla/Attributes.h"
14
15
namespace mozilla {
16
namespace gfx {
17
18
/**
19
 * Do not use this class directly. Subclass it, pass that subclass as the
20
 * Sub parameter, and only use that subclass. This allows methods to safely
21
 * cast 'this' to 'Sub*'.
22
 */
23
template <class T, class Sub>
24
struct BaseSize {
25
  union {
26
    struct {
27
      T width, height;
28
    };
29
    T components[2];
30
  };
31
32
  // Constructors
33
  constexpr BaseSize() : width(0), height(0) {}
34
0
  constexpr BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {}
35
36
  void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; }
37
38
0
  bool IsEmpty() const {
39
0
    return width <= 0 || height <= 0;
40
0
  }
41
42
  bool IsSquare() const {
43
    return width == height;
44
  }
45
46
  // Note that '=' isn't defined so we'll get the
47
  // compiler generated default assignment operator
48
49
  bool operator==(const Sub& aSize) const {
50
    return width == aSize.width && height == aSize.height;
51
  }
52
  bool operator!=(const Sub& aSize) const {
53
    return width != aSize.width || height != aSize.height;
54
  }
55
  bool operator<=(const Sub& aSize) const {
56
    return width <= aSize.width && height <= aSize.height;
57
  }
58
  bool operator<(const Sub& aSize) const {
59
    return *this <= aSize && *this != aSize;
60
  }
61
62
  Sub operator+(const Sub& aSize) const {
63
    return Sub(width + aSize.width, height + aSize.height);
64
  }
65
  Sub operator-(const Sub& aSize) const {
66
    return Sub(width - aSize.width, height - aSize.height);
67
  }
68
  Sub& operator+=(const Sub& aSize) {
69
    width += aSize.width;
70
    height += aSize.height;
71
    return *static_cast<Sub*>(this);
72
  }
73
  Sub& operator-=(const Sub& aSize) {
74
    width -= aSize.width;
75
    height -= aSize.height;
76
    return *static_cast<Sub*>(this);
77
  }
78
79
  Sub operator*(T aScale) const {
80
    return Sub(width * aScale, height * aScale);
81
  }
82
  Sub operator/(T aScale) const {
83
    return Sub(width / aScale, height / aScale);
84
  }
85
  friend Sub operator*(T aScale, const Sub& aSize) {
86
    return Sub(aScale * aSize.width, aScale * aSize.height);
87
  }
88
  void Scale(T aXScale, T aYScale) {
89
    width *= aXScale;
90
    height *= aYScale;
91
  }
92
93
  Sub operator*(const Sub& aSize) const {
94
    return Sub(width * aSize.width, height * aSize.height);
95
  }
96
  Sub operator/(const Sub& aSize) const {
97
    return Sub(width / aSize.width, height / aSize.height);
98
  }
99
100
  friend Sub Min(const Sub& aA, const Sub& aB) {
101
    return Sub(std::min(aA.width, aB.width),
102
               std::min(aA.height, aB.height));
103
  }
104
105
  friend Sub Max(const Sub& aA, const Sub& aB) {
106
    return Sub(std::max(aA.width, aB.width),
107
               std::max(aA.height, aB.height));
108
  }
109
110
  friend std::ostream& operator<<(std::ostream& aStream,
111
      const BaseSize<T, Sub>& aSize) {
112
    return aStream << aSize.width << " x " << aSize.height;
113
  }
114
};
115
116
} // namespace gfx
117
} // namespace mozilla
118
119
#endif /* MOZILLA_GFX_BASESIZE_H_ */