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-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  * ▕ 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  // Constructors
54  FPoint () noexcept = default;
55  FPoint (int, int) noexcept;
56 
57  // Overloaded operators
58  auto operator += (const FPoint&) -> FPoint&;
59  auto operator -= (const FPoint&) -> FPoint&;
60 
61  // Accessors
62  auto getClassName() const -> FString;
63  auto getX() const noexcept -> int;
64  auto getY() const noexcept -> int;
65  void setX (int) noexcept;
66  void setY (int) noexcept;
67  void setPoint (const FPoint&) noexcept;
68  void setPoint (int, int) noexcept;
69 
70  // Inquiry
71  auto isOrigin() const noexcept -> bool;
72 
73  // Point references
74  auto x_ref() & noexcept -> int&;
75  auto y_ref() & noexcept -> int&;
76 
77  // Methods
78  void move (int, int) noexcept;
79  void move (const FPoint&) noexcept;
80 
81  private:
82  // Data members
83  int xpos{0};
84  int ypos{0};
85 
86  // Friend operator functions
87  friend inline auto operator == (const FPoint& p1, const FPoint& p2) -> bool
88  {
89  return p1.xpos == p2.xpos && p1.ypos == p2.ypos;
90  }
91 
92  friend inline auto operator != (const FPoint& p1, const FPoint& p2) -> bool
93  {
94  return p1.xpos != p2.xpos || p1.ypos != p2.ypos;
95  }
96 
97  friend inline auto operator + (const FPoint& p1, const FPoint& p2) -> FPoint
98  {
99  return {p1.xpos + p2.xpos, p1.ypos + p2.ypos};
100  }
101 
102  friend inline auto operator - (const FPoint& p1, const FPoint& p2) -> FPoint
103  {
104  return {p1.xpos - p2.xpos, p1.ypos - p2.ypos};
105  }
106 
107  friend inline auto operator - (const FPoint& p) -> FPoint
108  {
109  return {-p.xpos, -p.ypos};
110  }
111 
112  friend inline auto operator << (std::ostream& outstr, const FPoint& p) -> std::ostream&
113  {
114  outstr << p.xpos << " " << p.ypos;
115  return outstr;
116  }
117 
118  friend inline auto operator >> (std::istream& instr, FPoint& p) -> std::istream&
119  {
120  int x{};
121  int y{};
122  instr >> x;
123  instr >> y;
124  p.setPoint (x, y);
125  return instr;
126  }
127 };
128 
129 
130 // FPoint inline functions
131 //----------------------------------------------------------------------
132 inline FPoint::FPoint (int x, int y) noexcept
133  : xpos{x}
134  , ypos{y}
135 { }
136 
137 //----------------------------------------------------------------------
138 inline auto FPoint::getClassName() const -> FString
139 { return "FPoint"; }
140 
141 //----------------------------------------------------------------------
142 inline auto FPoint::getX() const noexcept -> int
143 { return xpos; }
144 
145 //----------------------------------------------------------------------
146 inline auto FPoint::getY() const noexcept -> int
147 { return ypos; }
148 
149 //----------------------------------------------------------------------
150 inline void FPoint::setX (int x) noexcept
151 { xpos = x; }
152 
153 //----------------------------------------------------------------------
154 inline void FPoint::setY (int y) noexcept
155 { ypos = y; }
156 
157 //----------------------------------------------------------------------
158 inline void FPoint::setPoint (const FPoint& p) noexcept
159 { setPoint(p.xpos, p.ypos); }
160 
161 //----------------------------------------------------------------------
162 inline void FPoint::setPoint (int x, int y) noexcept
163 {
164  xpos = x;
165  ypos = y;
166 }
167 
168 //----------------------------------------------------------------------
169 inline auto FPoint::isOrigin() const noexcept -> bool
170 { return xpos == 0 && ypos == 0; }
171 
172 //----------------------------------------------------------------------
173 inline auto FPoint::x_ref() & noexcept -> int&
174 { return xpos; }
175 
176 //----------------------------------------------------------------------
177 inline auto FPoint::y_ref() & noexcept -> int&
178 { return ypos; }
179 
180 //----------------------------------------------------------------------
181 inline void FPoint::move (int dx, int dy) noexcept
182 {
183  xpos += dx;
184  ypos += dy;
185 }
186 
187 //----------------------------------------------------------------------
188 inline void FPoint::move (const FPoint& d) noexcept
189 {
190  xpos += d.getX();
191  ypos += d.getY();
192 }
193 
194 } // namespace finalcut
195 
196 #endif // FPOINT_H
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fstring.h:79