nomlib
Loading...
Searching...
No Matches
Size2.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_MATH_SIZE2_HEADERS
30#define NOMLIB_MATH_SIZE2_HEADERS
31
32#include <iostream>
33#include <algorithm>
34
35#include "nomlib/config.hpp"
36#include "export.hpp"
37
38// FIXME: The following declaration is necessary in order to avoid a very
39// nasty compiling conflict that can happen under Windows anytime the
40// windef.h header file is included (commonly from windows.h), due to min and
41// max macros being declared there. This is why macros are evil.
42//
43// http://support.microsoft.com/kb/143208
44// http://stackoverflow.com/questions/5004858/stdmin-gives-error
45#if defined( NOM_PLATFORM_WINDOWS )
46 #undef min
47 #undef max
48#endif
49
50namespace nom {
51
53const std::string SIZE2_DELIMITER = ", ";
54
56template <typename T>
57struct NOM_EXPORT Size2
58{
60 Size2 ( void ) :
61 w ( -1 ),
62 h ( -1 )
63 {
64 //NOM_LOG_TRACE(NOM);
65 }
66
68 ~Size2 ( void )
69 {
70 //NOM_LOG_TRACE(NOM);
71 }
72
74 Size2 ( T w, T h ) :
75 w ( w ),
76 h ( h )
77 {
78 //NOM_LOG_TRACE(NOM);
79 }
80
86 template <typename U>
87 explicit Size2 ( const Size2<U>& copy ) :
88 w { static_cast<T> ( copy.w ) },
89 h { static_cast<T> ( copy.h ) }
90 {
91 this->w = static_cast<T> ( copy.w );
92 this->h = static_cast<T> ( copy.h );
93 }
94
97 template <typename U>
98 Size2 max( const Size2<U>& rhs )
99 {
100 return Size2<T>( std::max( this->w, rhs.w ), std::max( this->h, rhs.h ) );
101 }
102
105 template <typename U>
106 Size2 min( const Size2<U>& rhs )
107 {
108 return Size2<T>( std::min( this->w, rhs.w ), std::min( this->h, rhs.h ) );
109 }
110
112 void swap( void )
113 {
114 std::swap( this->w, this->h );
115 }
116
120 static const Size2 null;
121
123 static const Size2 zero;
124
126 T w;
127
129 T h;
130};
131
139template <typename T>
140NOM_EXPORT
141inline std::ostream& operator <<( std::ostream& os, const Size2<T>& pos )
142{
143 os
144 << pos.w
145 << SIZE2_DELIMITER
146 << pos.h;
147
148 return os;
149}
150
151template <typename T>
152NOM_EXPORT
153inline bool operator ==( const Size2<T>& lhs, const Size2<T>& rhs )
154{
155 return ( lhs.w == rhs.w ) && ( lhs.h == rhs.h );
156}
157
158template <typename T>
159NOM_EXPORT
160inline bool operator !=( const Size2<T>& lhs, const Size2<T>& rhs )
161{
162 return ! ( lhs == rhs );
163}
164
171template <typename T>
172NOM_EXPORT
173inline Size2<T> operator +( const Size2<T>& lhs, const Size2<T>& rhs )
174{
175 return Size2<T> (
176 lhs.w + rhs.w,
177 lhs.h + rhs.h
178 );
179}
180
187template <typename T>
188NOM_EXPORT inline Size2<T> operator +(int lhs, const Size2<T>& rhs)
189{
190 return Size2<T>(lhs + rhs.w, lhs + rhs.h);
191}
192
199template <typename T>
200NOM_EXPORT inline Size2<T> operator +(const Size2<T>& lhs, int rhs)
201{
202 return Size2<T>(lhs.w + rhs, lhs.h + rhs);
203}
204
210template <typename T>
211NOM_EXPORT inline Size2<T> operator ++( const Size2<T>& rhs )
212{
213 return Size2<T> (
214 ++rhs.w,
215 ++rhs.h
216 );
217}
218
224template <typename T>
225NOM_EXPORT inline Size2<T> operator -( const Size2<T>& rhs )
226{
227 return Size2<T> (
228 -rhs.w
229 -rhs.h
230 );
231}
232
239template <typename T>
240NOM_EXPORT
241inline Size2<T> operator -( const Size2<T>& lhs, const Size2<T>& rhs )
242{
243 return Size2<T> (
244 lhs.w - rhs.w,
245 lhs.h - rhs.h
246 );
247}
248
255template <typename T>
256NOM_EXPORT
257inline Size2<T> operator -(int lhs, const Size2<T>& rhs)
258{
259 return Size2<T>(lhs - rhs.w, lhs - rhs.h);
260}
261
268template <typename T>
269NOM_EXPORT inline Size2<T> operator -(const Size2<T>& lhs, int rhs)
270{
271 return Size2<T>(lhs.w - rhs, lhs.h - rhs);
272}
273
279template <typename T>
280NOM_EXPORT inline Size2<T> operator --( const Size2<T>& rhs )
281{
282 return Size2<T> (
283 --rhs.w,
284 --rhs.h
285 );
286}
287
294template <typename T>
295NOM_EXPORT
296inline Size2<T> operator *( const Size2<T>& lhs, const Size2<T>& rhs )
297{
298 return Size2<T> ( lhs.w * rhs.w,
299 lhs.h * rhs.h
300 );
301}
302
309template <typename T>
310NOM_EXPORT inline Size2<T> operator *(int lhs, const Size2<T>& rhs)
311{
312 return Size2<T>(lhs * rhs.w, lhs * rhs.h);
313}
314
321template <typename T>
322NOM_EXPORT inline Size2<T> operator *(const Size2<T>& lhs, int rhs)
323{
324 return Size2<T>(lhs.w * rhs, lhs.h * rhs);
325}
326
336template <typename T>
337NOM_EXPORT
338inline Size2<T>& operator +=( Size2<T>& lhs, const Size2<T>& rhs )
339{
340 lhs.w += rhs.w;
341 lhs.h += rhs.h;
342
343 return lhs;
344}
345
355template <typename T>
356NOM_EXPORT inline Size2<T>& operator +=(int lhs, const Size2<T>& rhs)
357{
358 lhs += rhs.w;
359 lhs += rhs.h;
360
361 return lhs;
362}
363
373template <typename T>
374NOM_EXPORT inline Size2<T>& operator +=(Size2<T>& lhs, int rhs)
375{
376 lhs.w += rhs;
377 lhs.h += rhs;
378
379 return lhs;
380}
381
391template <typename T>
392NOM_EXPORT
393inline Size2<T>& operator -=( Size2<T>& lhs, const Size2<T>& rhs )
394{
395 lhs.w -= rhs.w;
396 lhs.h -= rhs.h;
397
398 return lhs;
399}
400
410template <typename T>
411NOM_EXPORT inline Size2<T>& operator -=(int lhs, const Size2<T>& rhs)
412{
413 lhs -= rhs.w;
414 lhs -= rhs.h;
415
416 return lhs;
417}
418
428template <typename T>
429NOM_EXPORT inline Size2<T>& operator -=(Size2<T>& lhs, int rhs)
430{
431 lhs.w -= rhs;
432 lhs.h -= rhs;
433
434 return lhs;
435}
436
446template <typename T>
447NOM_EXPORT
448inline Size2<T>& operator *=( Size2<T>& lhs, const Size2<T>& rhs )
449{
450 lhs.w *= rhs.w;
451 lhs.h *= rhs.h;
452
453 return lhs;
454}
455
465template <typename T>
466NOM_EXPORT
467inline Size2<T>& operator /=( Size2<T>& lhs, const Size2<T>& rhs )
468{
469 lhs.w /= rhs.w;
470 lhs.h /= rhs.h;
471
472 return lhs;
473}
474
484template <typename T>
485NOM_EXPORT inline Size2<T>& operator /=(int lhs, const Size2<T>& rhs)
486{
487 lhs /= rhs.w;
488 lhs /= rhs.h;
489
490 return lhs;
491}
492
502template <typename T>
503NOM_EXPORT inline Size2<T>& operator /=(Size2<T>& lhs, int rhs)
504{
505 lhs.w /= rhs;
506 lhs.h /= rhs;
507
508 return lhs;
509}
510
517template <typename T>
518NOM_EXPORT
519inline Size2<T> operator /( const Size2<T>& lhs, const Size2<T>& rhs )
520{
521 Size2<T> ret;
522 ret.w = lhs.w / rhs.w;
523 ret.h = lhs.h / rhs.h;
524
525 return ret;
526}
527
534template <typename T>
535NOM_EXPORT inline Size2<T> operator /(int lhs, const Size2<T>& rhs)
536{
537 Size2<T> ret;
538 ret.w = lhs / rhs.w;
539 ret.h = lhs / rhs.h;
540
541 return ret;
542}
543
550template <typename T>
551NOM_EXPORT inline Size2<T> operator /(const Size2<T>& lhs, int rhs)
552{
553 Size2<T> ret;
554 ret.w = lhs.w / rhs;
555 ret.h = lhs.h / rhs;
556
557 return ret;
558}
559
564template <typename T>
565NOM_EXPORT
566inline bool operator <( const Size2<T> lhs, const Size2<T>& rhs )
567{
568 return ( lhs.w < rhs.w ) && ( lhs.h < rhs.h );
569}
570
575template <typename T>
576NOM_EXPORT
577inline bool operator >( const Size2<T>& lhs, const Size2<T>& rhs )
578{
579 return ( rhs.w < lhs.w ) && ( rhs.h < lhs.h );
580}
581
586template <typename T>
587NOM_EXPORT
588inline bool operator <=( const Size2<T>& lhs, const Size2<T>& rhs )
589{
590 return ( lhs.w <= rhs.w ) && ( lhs.h <= rhs.h );
591}
592
597template <typename T>
598NOM_EXPORT
599inline bool operator >=( const Size2<T>& lhs, const Size2<T>& rhs )
600{
601 return ( rhs.w <= lhs.w ) && ( rhs.h <= lhs.h );
602}
603
605typedef Size2<int> Size2i;
606
608typedef Size2<float> Size2f;
609
611typedef Size2<double> Size2d;
612
614template <> NOM_EXPORT const Size2i Size2i::null;
615
617template <> NOM_EXPORT const Size2f Size2f::null;
618
620template <> NOM_EXPORT const Size2d Size2d::null;
621
623template <> const Size2i Size2i::zero;
624
626template <> const Size2f Size2f::zero;
627
630template <> const Size2d Size2d::zero;
631
632} // namespace nom
633
634#endif // include guard defined
Size coordinates (width & height) container.
Definition Size2.hpp:58
Size2(void)
Default constructor; initialize values to Size2<T>::null.
Definition Size2.hpp:60
Size2 min(const Size2< U > &rhs)
Compare two Size2 objects and return the smaller width and height of the two objects.
Definition Size2.hpp:106
Size2 max(const Size2< U > &rhs)
Compare two Size2 objects and return the larger width and height of the two objects.
Definition Size2.hpp:98
void swap(void)
Transpose width and height dimensions.
Definition Size2.hpp:112
Size2(const Size2< U > &copy)
Copy constructor.
Definition Size2.hpp:87
~Size2(void)
Destructor.
Definition Size2.hpp:68
Size2(T w, T h)
Constructor variant for initializing width & height at construction.
Definition Size2.hpp:74
static const Size2 zero
Definition Size2.hpp:123
static const Size2 null
Definition Size2.hpp:120