scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
size_2d.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_SPECIAL_UNITS_SIZE_2D_HPP_INCLUDED
2#define SCIFIR_UNITS_SPECIAL_UNITS_SIZE_2D_HPP_INCLUDED
3
4#include "../derived_units/space_units.hpp"
5#include "../util/types.hpp"
6
7#include "boost/algorithm/string.hpp"
8
9#include <string>
10
11using namespace std;
12
13namespace scifir
14{
15 template<typename T = length>
16 class size_2d
17 {
18 public:
20 {}
21
23 {}
24
25 size_2d(size_2d<T>&& x) : width(std::move(x.width)),height(std::move(x.height))
26 {}
27
30
31 explicit size_2d(const string& new_width,const string& new_height) : width(new_width),height(new_height)
32 {}
33
34 explicit size_2d(const string& init_size_2d) : size_2d()
35 {
37 }
38
40 {
41 width = x.width;
42 height = x.height;
43 return *this;
44 }
45
47 {
48 width = std::move(x.width);
49 height = std::move(x.height);
50 return *this;
51 }
52
54 {
56 return *this;
57 }
58
59 template<typename U>
61 {
62 return size_2d<T>(width + x.width,height + x.height);
63 }
64
65 template<typename U>
67 {
68 return size_2d<T>(width - x.width,height - x.height);
69 }
70
71 template<typename U>
72 void operator +=(const size_2d<U>& x)
73 {
74 width += x.width;
75 height += x.height;
76 }
77
78 template<typename U>
79 void operator -=(const size_2d<U>& x)
80 {
81 width -= x.width;
82 height -= x.height;
83 }
84
86 {
87 return scalar_unit(width * height);
88 }
89
90 string display() const
91 {
93 output << width << " * " << height;
94 return output.str();
95 }
96
99
100 private:
102 {
103 vector<string> widths;
104 boost::split(widths,init_size_2d,boost::is_any_of("*"));
105 if (widths.size() == 2)
106 {
107 boost::trim(widths[0]);
108 boost::trim(widths[1]);
109 width = T(widths[0]);
110 height = T(widths[1]);
111 }
112 }
113 };
114
115 template<>
117 {
118 public:
120 {}
121
123 {}
124
125 size_2d(size_2d<float>&& x) : width(std::move(x.width)),height(std::move(x.height))
126 {}
127
128 explicit size_2d(const float& new_width,const float& new_height) : width(new_width),height(new_height)
129 {}
130
131 explicit size_2d(const string& new_width,const string& new_height) : width(stof(new_width)),height(stof(new_height))
132 {}
133
134 explicit size_2d(const string& init_size_2d) : size_2d()
135 {
137 }
138
140 {
141 width = x.width;
142 height = x.height;
143 return *this;
144 }
145
147 {
148 width = std::move(x.width);
149 height = std::move(x.height);
150 return *this;
151 }
152
154 {
156 return *this;
157 }
158
160 {
161 return size_2d<float>(width + x.width,height + x.height);
162 }
163
165 {
166 return size_2d<float>(width - x.width,height - x.height);
167 }
168
170 {
171 width += x.width;
172 height += x.height;
173 }
174
176 {
177 width -= x.width;
178 height -= x.height;
179 }
180
181 float get_area() const
182 {
183 return width * height;
184 }
185
186 string display() const
187 {
189 output << display_float(width,2) << " * " << display_float(height,2);
190 return output.str();
191 }
192
193 float width;
194 float height;
195
196 private:
198 {
199 vector<string> widths;
200 boost::split(widths,init_size_2d,boost::is_any_of("*"));
201 if (widths.size() == 2)
202 {
203 boost::trim(widths[0]);
204 boost::trim(widths[1]);
205 width = stof(widths[0]);
206 height = stof(widths[1]);
207 }
208 }
209 };
210
211 template<typename T>
212 string to_string(const size_2d<T>& x)
213 {
214 return x.display();
215 }
216
217 string to_string(const size_2d<float>& x);
218}
219
220template<typename T>
222{
223 if (x.width == y.width and x.height == y.height)
224 {
225 return true;
226 }
227 else
228 {
229 return false;
230 }
231}
232
233template<typename T>
235{
236 return !(x == y);
237}
238
239template<typename T>
240bool operator ==(const scifir::size_2d<T>& x, const string& init_size_2d)
241{
242 scifir::size_2d<T> y(init_size_2d);
243 return (x == y);
244}
245
246template<typename T>
247bool operator !=(const scifir::size_2d<T>& x, const string& init_size_2d)
248{
249 return !(x == init_size_2d);
250}
251
252template<typename T>
253bool operator ==(const string& init_size_2d, const scifir::size_2d<T>& x)
254{
255 scifir::size_2d<T> y(init_size_2d);
256 return (x == y);
257}
258
259template<typename T>
260bool operator !=(const string& init_size_2d, const scifir::size_2d<T>& x)
261{
262 return !(init_size_2d == x);
263}
264
265template<typename T>
266void operator +=(string& x, const scifir::size_2d<T>& y)
267{
268 x += to_string(y);
269}
270
271template<typename T>
272string operator +(const string& x, const scifir::size_2d<T>& y)
273{
274 return x + to_string(y);
275}
276
277template<typename T>
278string operator +(const scifir::size_2d<T>& x, const string& y)
279{
280 return to_string(x) + y;
281}
282
283template<typename T>
284ostream& operator <<(ostream& os, const scifir::size_2d<T>& x)
285{
286 return os << to_string(x);
287}
288
289template<typename T>
290istream& operator >>(istream& is, scifir::size_2d<T>& x)
291{
292 char a[256];
293 is.getline(a, 256);
294 string b(a);
295 x = scifir::size_2d<T>(b);
296 return is;
297}
298
299#endif // SCIFIR_UNITS_SPECIAL_UNITS_SIZE_2D_HPP_INCLUDED
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
void initialize_from_string(const string &init_size_2d)
Internal function. Initializes the member-variables with an initialization string of size_2d<float>.
Definition size_2d.hpp:197
size_2d(const float &new_width, const float &new_height)
Constructor. width is new_width and height is new_height.
Definition size_2d.hpp:128
string display() const
Returns a string representation of size_2d<float>, with his width and height.
Definition size_2d.hpp:186
float width
Width, stored as a float. The advantage related to size_2d<float> over size_2d<T> is that it sizes le...
Definition size_2d.hpp:193
size_2d(const size_2d< float > &x)
Copy constructor. The width and height are copied from the size_2d<float> x.
Definition size_2d.hpp:122
size_2d()
Default constructor. Initializes width and height to 0.
Definition size_2d.hpp:119
size_2d(const string &new_width, const string &new_height)
Constructor. width is new_width and height is new_height, they are initialized by string.
Definition size_2d.hpp:131
float get_area() const
Calculates the area as the multiplication of the width and height.
Definition size_2d.hpp:181
float height
Height, stored as a float. The advantage related to size_2d<float> over size_2d<T> is that it sizes l...
Definition size_2d.hpp:194
size_2d(const string &init_size_2d)
Constructor. The member-variables are initialized by the initialization string init_size_2d specializ...
Definition size_2d.hpp:134
size_2d(size_2d< float > &&x)
Move constructor. The width and height are moved from the size_2d<float> x.
Definition size_2d.hpp:125
Class that allows to store and calculate size in 2D, with width and height. The template parameter T ...
Definition size_2d.hpp:17
size_2d()
Default constructor. Both width and height are initialized to 0, with the dimensions to the default o...
Definition size_2d.hpp:19
size_2d< T > operator+(const size_2d< U > &x) const
Creates a new size_2d<T> by the addition of two size_2d<T> classes. The width is the result of the ad...
Definition size_2d.hpp:60
size_2d< T > operator-(const size_2d< U > &x) const
Creates a new size_2d<T> by the difference of two size_2d<T> classes. The width is the result of the ...
Definition size_2d.hpp:66
size_2d(size_2d< T > &&x)
Move constructor. The width and height are moved from the size_2d<T> x.
Definition size_2d.hpp:25
size_2d(const size_2d< T > &x)
Copy constructor. The width and height are copied from the size_2d<T> x.
Definition size_2d.hpp:22
size_2d(const string &new_width, const string &new_height)
Constructor. width is new_width and height is new_height, they are initialized by string.
Definition size_2d.hpp:31
T height
Height, usually stored as length.
Definition size_2d.hpp:98
size_2d(const string &init_size_2d)
Constructor. The member-variables are initialized by the initialization string init_size_2d.
Definition size_2d.hpp:34
scalar_unit get_area() const
Calculates the area as the multiplication of the width and height.
Definition size_2d.hpp:85
size_2d(const scalar_unit &new_width, const scalar_unit &new_height)
Definition size_2d.hpp:28
void operator+=(const size_2d< U > &x)
Sums the width of size_2d<T> x to the width and sums the height of size_2d<T> x to the height.
Definition size_2d.hpp:72
void initialize_from_string(const string &init_size_2d)
Internal function. Initializes the member-variables with an initialization string of size_2d<T>.
Definition size_2d.hpp:101
void operator-=(const size_2d< U > &x)
Substracts the width of size_2d<T> x to the width and substracts the height of size_2d<T> x to the he...
Definition size_2d.hpp:79
string display() const
Returns a string representation of size_2d<T>, with his width and height.
Definition size_2d.hpp:90
size_2d< T > & operator=(const size_2d< T > &x)
Copy assignment. The width and height are copied from the size_2d<T> x.
Definition size_2d.hpp:39
T width
Width, usually stored as length.
Definition size_2d.hpp:97
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
bool operator==(const scifir::size_2d< T > &x, const scifir::size_2d< T > &y)
Returns true if the width and height of x and y are both equal.
Definition size_2d.hpp:221
void operator+=(string &x, const scifir::size_2d< T > &y)
Concatenates the string x with the string representation of size_2d<T> y.
Definition size_2d.hpp:266
string operator+(const string &x, const scifir::size_2d< T > &y)
Creates a new string as the concatenation of the string x with the string representation of size_2d<T...
Definition size_2d.hpp:272
ostream & operator<<(ostream &os, const scifir::size_2d< T > &x)
Adds the string representation of the size_2d<T> x to an output stream os.
Definition size_2d.hpp:284
istream & operator>>(istream &is, scifir::size_2d< T > &x)
Allows that an istream initializes by string a size_2d<T> x.
Definition size_2d.hpp:290
bool operator!=(const scifir::size_2d< T > &x, const scifir::size_2d< T > &y)
Returns true if the width and height of x and y are any of them different.
Definition size_2d.hpp:234