FINAL CUT
fsize.h
1 /***********************************************************************
2 * fsize.h - Height and width of a two-dimensional surface *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2014-2024 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Standalone class
24  * ════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▏
27  * ▕ FSize ▏
28  * ▕▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FSIZE_H
32 #define FSIZE_H
33 
34 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
35  #error "Only <final/final.h> can be included directly."
36 #endif
37 
38 #include <iostream>
39 #include <limits>
40 #include <utility>
41 
42 #include "final/ftypes.h"
43 #include "final/util/fstring.h"
44 
45 namespace finalcut
46 {
47 
48 // class forward declaration
49 class FPoint;
50 
51 //----------------------------------------------------------------------
52 // class FSize
53 //----------------------------------------------------------------------
54 
55 class FSize
56 {
57  public:
58  // Constructors
59  FSize () noexcept = default;
60  template<typename WidthT, typename HeightT>
61  FSize (WidthT, HeightT) noexcept;
62 
63  // Overloaded operators
64  auto operator += (const FSize&) -> FSize&;
65  auto operator -= (const FSize&) -> FSize&;
66 
67  // Accessors
68  auto getClassName() const -> FString;
69  auto getWidth() const noexcept -> std::size_t;
70  auto getHeight() const noexcept -> std::size_t;
71  auto getArea() const noexcept -> std::size_t;
72  void setWidth (std::size_t) noexcept;
73  void setHeight (std::size_t) noexcept;
74  void setSize (const FSize&) noexcept;
75  void setSize (std::size_t, std::size_t) noexcept;
76 
77  // Inquiry
78  auto isEmpty() const noexcept -> bool;
79 
80  // Side references
81  auto width_ref() & noexcept -> std::size_t&;
82  auto height_ref() & noexcept -> std::size_t&;
83 
84  // Methods
85  void scaleBy (int, int) noexcept;
86  void scaleBy (const FPoint&) noexcept;
87 
88  private:
89  // Data members
90  std::size_t width{0};
91  std::size_t height{0};
92 
93  // Friend operator functions
94  friend inline auto operator < (const FSize& s1, const FSize& s2) -> bool
95  {
96  return s1.width < s2.width && s1.height < s2.height;
97  }
98 
99  friend inline auto operator <= (const FSize& s1, const FSize& s2) -> bool
100  {
101  return s1.width <= s2.width && s1.height <= s2.height;
102  }
103 
104  friend inline auto operator == (const FSize& s1, const FSize& s2) -> bool
105  {
106  return s1.width == s2.width && s1.height == s2.height;
107  }
108 
109  friend inline auto operator != (const FSize& s1, const FSize& s2) -> bool
110  {
111  return s1.width != s2.width || s1.height != s2.height;
112  }
113 
114  friend inline auto operator >= (const FSize& s1, const FSize& s2) -> bool
115  {
116  return s1.width >= s2.width && s1.height >= s2.height;
117  }
118 
119  friend inline auto operator > (const FSize& s1, const FSize& s2) -> bool
120  {
121  return s1.width > s2.width && s1.height > s2.height;
122  }
123 
124  friend inline auto operator + (const FSize& s1, const FSize& s2) -> FSize
125  {
126  constexpr std::size_t max = std::numeric_limits<std::size_t>::max();
127  const std::size_t w = ( s1.width < max - s2.width) ? s1.width + s2.width : max;
128  const std::size_t h = ( s1.height < max - s2.height) ? s1.height + s2.height : max;
129  return { FSize{w, h} };
130  }
131 
132  friend inline auto operator - (const FSize& s1, const FSize& s2) -> FSize
133  {
134  const std::size_t w = ( s1.width >= s2.width ) ? s1.width - s2.width : 0;
135  const std::size_t h = ( s1.height >= s2.height ) ? s1.height - s2.height : 0;
136  return { FSize{w, h} };
137  }
138 
139  friend inline auto operator << (std::ostream& outstr, const FSize& s) -> std::ostream&
140  {
141  outstr << s.width << " " << s.height;
142  return outstr;
143  }
144 
145  friend inline auto operator >> (std::istream& instr, FSize& s) -> std::istream&
146  {
147  std::size_t w;
148  std::size_t h;
149  instr >> w;
150  instr >> h;
151  s.setSize (w, h);
152  return instr;
153  }
154 };
155 
156 // FSize inline functions
157 //----------------------------------------------------------------------
158 template<typename WidthT, typename HeightT>
159 inline FSize::FSize (WidthT w, HeightT h) noexcept
160  : width{static_cast<std::size_t>(w > 0 ? w : 0)}
161  , height{static_cast<std::size_t>(h > 0 ? h : 0)}
162 { }
163 
164 //----------------------------------------------------------------------
165 inline auto FSize::getClassName() const -> FString
166 { return "FSize"; }
167 
168 //----------------------------------------------------------------------
169 inline auto FSize::getWidth() const noexcept -> std::size_t
170 { return width; }
171 
172 //----------------------------------------------------------------------
173 inline auto FSize::getHeight() const noexcept -> std::size_t
174 { return height; }
175 
176 //----------------------------------------------------------------------
177 inline auto FSize::getArea() const noexcept -> std::size_t
178 { return width * height; }
179 
180 //----------------------------------------------------------------------
181 inline void FSize::setWidth (std::size_t w) noexcept
182 { width = w; }
183 
184 //----------------------------------------------------------------------
185 inline void FSize::setHeight (std::size_t h) noexcept
186 { height = h; }
187 
188 //----------------------------------------------------------------------
189 inline void FSize::setSize (const FSize& s) noexcept
190 {
191  width = s.width;
192  height = s.height;
193 }
194 
195 //----------------------------------------------------------------------
196 inline void FSize::setSize (std::size_t w, std::size_t h) noexcept
197 {
198  width = w;
199  height = h;
200 }
201 
202 //----------------------------------------------------------------------
203 inline auto FSize::isEmpty() const noexcept -> bool
204 { return width == 0 && height == 0; }
205 
206 //----------------------------------------------------------------------
207 inline auto FSize::width_ref() & noexcept -> std::size_t&
208 { return width; }
209 
210 //----------------------------------------------------------------------
211 inline auto FSize::height_ref() & noexcept -> std::size_t&
212 { return height; }
213 
214 } // namespace finalcut
215 
216 #endif // FSIZE_H
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fsize.h:55
Definition: fstring.h:79