FINAL CUT
fpoint.h
1 /***********************************************************************
2 * fpoint.h - Point with an x and y coordinate *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2014-2025 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  * ▕ FPoint ▏
28  * ▕▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FPOINT_H
32 #define FPOINT_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 <utility>
40 
41 #include "final/util/fstring.h"
42 
43 namespace finalcut
44 {
45 
46 //----------------------------------------------------------------------
47 // class FPoint
48 //----------------------------------------------------------------------
49 
50 class FPoint
51 {
52  public:
53  // Using-declarations
54  using coordinate_type = int;
55  using coordinate_reference = coordinate_type&;
56 
57  // Constructors
58  constexpr FPoint () noexcept = default;
59  constexpr FPoint (coordinate_type, coordinate_type) noexcept;
60 
61  // Copy constructor
62  constexpr FPoint (const FPoint&) noexcept = default;
63 
64  // Move constructor
65  constexpr FPoint (FPoint&&) noexcept = default;
66 
67  // Destructor
68  ~FPoint() noexcept = default;
69 
70  // Copy assignment operator (=)
71  constexpr auto operator = (const FPoint&) noexcept -> FPoint& = default;
72 
73  // Move assignment operator (=)
74  constexpr auto operator = (FPoint&&) noexcept -> FPoint& = default;
75 
76  // Overloaded operators
77  auto operator += (const FPoint&) -> FPoint&;
78  auto operator -= (const FPoint&) -> FPoint&;
79 
80  // Accessors
81  auto getClassName() const -> FString;
82  constexpr auto getX() const noexcept -> coordinate_type;
83  constexpr auto getY() const noexcept -> coordinate_type;
84 
85  // Mutators
86  constexpr void setX (coordinate_type) noexcept;
87  constexpr void setY (coordinate_type) noexcept;
88  constexpr void setPoint (const FPoint&) noexcept;
89  constexpr void setPoint (coordinate_type, coordinate_type) noexcept;
90 
91  // Predicate
92  constexpr auto isOrigin() const noexcept -> bool;
93 
94  // Point references
95  constexpr auto x_ref() & noexcept -> coordinate_type&;
96  constexpr auto y_ref() & noexcept -> coordinate_type&;
97 
98  // Methods
99  constexpr void move (coordinate_type, coordinate_type) noexcept;
100  constexpr void move (const FPoint&) noexcept;
101  constexpr void swap (FPoint&) noexcept;
102 
103  private:
104  // Data members
105  coordinate_type xpos{0};
106  coordinate_type ypos{0};
107 
108  // Friend operator functions
109  friend constexpr auto operator == (const FPoint& p1, const FPoint& p2) -> bool
110  {
111  return p1.xpos == p2.xpos && p1.ypos == p2.ypos;
112  }
113 
114  friend constexpr auto operator != (const FPoint& p1, const FPoint& p2) -> bool
115  {
116  return ! ( p1 == p2 );
117  }
118 
119  friend constexpr auto operator + (const FPoint& p1, const FPoint& p2) -> FPoint
120  {
121  return {p1.xpos + p2.xpos, p1.ypos + p2.ypos};
122  }
123 
124  friend constexpr auto operator - (const FPoint& p1, const FPoint& p2) -> FPoint
125  {
126  return {p1.xpos - p2.xpos, p1.ypos - p2.ypos};
127  }
128 
129  friend constexpr auto operator - (const FPoint& p) -> FPoint
130  {
131  return {-p.xpos, -p.ypos};
132  }
133 
134  friend inline auto operator << (std::ostream& outstr, const FPoint& p) -> std::ostream&
135  {
136  outstr << p.xpos << " " << p.ypos;
137  return outstr;
138  }
139 
140  friend inline auto operator >> (std::istream& instr, FPoint& p) -> std::istream&
141  {
142  coordinate_type x{};
143  coordinate_type y{};
144 
145  if ( instr >> x >> y )
146  p.setPoint (x, y);
147 
148  return instr;
149  }
150 };
151 
152 
153 // FPoint inline functions
154 //----------------------------------------------------------------------
155 constexpr FPoint::FPoint (coordinate_type x, coordinate_type y) noexcept
156  : xpos{x}
157  , ypos{y}
158 { }
159 
160 //----------------------------------------------------------------------
161 inline auto FPoint::getClassName() const -> FString
162 { return "FPoint"; }
163 
164 //----------------------------------------------------------------------
165 constexpr auto FPoint::getX() const noexcept -> coordinate_type
166 { return xpos; }
167 
168 //----------------------------------------------------------------------
169 constexpr auto FPoint::getY() const noexcept -> coordinate_type
170 { return ypos; }
171 
172 //----------------------------------------------------------------------
173 constexpr void FPoint::setX (coordinate_type x) noexcept
174 { xpos = x; }
175 
176 //----------------------------------------------------------------------
177 constexpr void FPoint::setY (coordinate_type y) noexcept
178 { ypos = y; }
179 
180 //----------------------------------------------------------------------
181 constexpr void FPoint::setPoint (const FPoint& p) noexcept
182 { setPoint(p.xpos, p.ypos); }
183 
184 //----------------------------------------------------------------------
185 constexpr void FPoint::setPoint (coordinate_type x, coordinate_type y) noexcept
186 {
187  xpos = x;
188  ypos = y;
189 }
190 
191 //----------------------------------------------------------------------
192 constexpr auto FPoint::isOrigin() const noexcept -> bool
193 { return xpos == 0 && ypos == 0; }
194 
195 //----------------------------------------------------------------------
196 constexpr auto FPoint::x_ref() & noexcept -> coordinate_reference
197 { return xpos; }
198 
199 //----------------------------------------------------------------------
200 constexpr auto FPoint::y_ref() & noexcept -> coordinate_reference
201 { return ypos; }
202 
203 //----------------------------------------------------------------------
204 constexpr void FPoint::move (coordinate_type dx, coordinate_type dy) noexcept
205 {
206  xpos += dx;
207  ypos += dy;
208 }
209 
210 //----------------------------------------------------------------------
211 constexpr void FPoint::move (const FPoint& d) noexcept
212 {
213  xpos += d.getX();
214  ypos += d.getY();
215 }
216 
217 //----------------------------------------------------------------------
218 constexpr void FPoint::swap (FPoint& p) noexcept
219 {
220  coordinate_type tmp_x{xpos};
221  coordinate_type tmp_y{ypos};
222  xpos = p.xpos;
223  ypos = p.ypos;
224  p.xpos = tmp_x;
225  p.ypos = tmp_y;
226 }
227 
228 } // namespace finalcut
229 
230 #endif // FPOINT_H
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fstring.h:82