• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // Note: ported from Chromium commit head: 4db7af61f923
5 // Note: only necessary functions are ported from gfx::Size
6 
7 #ifndef SIZE_H_
8 #define SIZE_H_
9 
10 #include <string>
11 
12 #include "base/numerics/safe_math.h"
13 #include "base/strings/stringprintf.h"
14 
15 namespace media {
16 
17 // Helper struct for size to replace gfx::size usage from original code.
18 // Only partial functions of gfx::size is implemented here.
19 struct Size {
20  public:
SizeSize21   constexpr Size() : width_(0), height_(0) {}
SizeSize22   constexpr Size(int width, int height)
23       : width_(std::max(0, width)), height_(std::max(0, height)) {}
24 
25   Size& operator=(const Size& ps) {
26     set_width(ps.width());
27     set_height(ps.height());
28     return *this;
29   }
30 
widthSize31   constexpr int width() const { return width_; }
heightSize32   constexpr int height() const { return height_; }
33 
set_widthSize34   void set_width(int width) { width_ = std::max(0, width); }
set_heightSize35   void set_height(int height) { height_ = std::max(0, height); }
36 
37   // This call will CHECK if the area of this size would overflow int.
GetAreaSize38   int GetArea() const { return GetCheckedArea().ValueOrDie(); }
39 
40   // Returns a checked numeric representation of the area.
GetCheckedAreaSize41   base::CheckedNumeric<int> GetCheckedArea() const {
42     base::CheckedNumeric<int> checked_area = width();
43     checked_area *= height();
44     return checked_area;
45   }
46 
SetSizeSize47   void SetSize(int width, int height) {
48     set_width(width);
49     set_height(height);
50   }
51 
IsEmptySize52   bool IsEmpty() const { return !width() || !height(); }
53 
ToStringSize54   std::string ToString() const {
55     return base::StringPrintf("%dx%d", width(), height());
56   }
57 
58  private:
59   int width_;
60   int height_;
61 };
62 
63 inline bool operator==(const Size& lhs, const Size& rhs) {
64   return lhs.width() == rhs.width() && lhs.height() == rhs.height();
65 }
66 
67 inline bool operator!=(const Size& lhs, const Size& rhs) {
68   return !(lhs == rhs);
69 }
70 
71 }  // namespace media
72 
73 #endif  // SIZE_H_
74