trunk 20080912
[gitenigma.git] / include / lib / base / esize.h
blob037b7577f69aae10609434ffdfe92da896ad17d6
1 #ifndef ESIZE_H
2 #define ESIZE_H
4 #include <iostream>
6 #ifndef MIN
7 #define MIN(a,b) ((a) < (b) ? (a) : (b))
8 #endif
10 #ifndef MAX
11 #define MAX(a,b) ((a) > (b) ? (a) : (b))
12 #endif
14 class eSize
16 public:
17 eSize();
18 eSize( int w, int h );
20 bool isNull() const;
21 bool isEmpty() const;
22 bool isValid() const;
24 int width() const;
25 int height() const;
26 void setWidth( int w );
27 void setHeight( int h );
28 void transpose();
30 eSize expandedTo( const eSize & ) const;
31 eSize boundedTo( const eSize & ) const;
33 int &rwidth();
34 int &rheight();
36 eSize &operator+=( const eSize & );
37 eSize &operator-=( const eSize & );
38 eSize &operator*=( int c );
39 eSize &operator*=( double c );
40 eSize &operator/=( int c );
41 eSize &operator/=( double c );
43 friend inline bool operator==( const eSize &, const eSize & );
44 friend inline bool operator!=( const eSize &, const eSize & );
45 friend inline eSize operator+( const eSize &, const eSize & );
46 friend inline eSize operator-( const eSize &, const eSize & );
47 friend inline eSize operator*( const eSize &, int );
48 friend inline eSize operator*( int, const eSize & );
49 friend inline eSize operator*( const eSize &, double );
50 friend inline eSize operator*( double, const eSize & );
51 friend inline eSize operator/( const eSize &, int );
52 friend inline eSize operator/( const eSize &, double );
54 private:
55 int wd;
56 int ht;
60 /*****************************************************************************
61 eSize stream functions
62 *****************************************************************************/
64 namespace std
66 inline ostream &operator<<( ostream &s, const eSize &sz )
68 s << sz.width() << sz.height();
69 return s;
72 inline istream &operator>>( istream &s, eSize &sz )
74 s >> sz.rwidth() >> sz.rheight();
75 return s;
80 /*****************************************************************************
81 eSize inline functions
82 *****************************************************************************/
84 inline eSize::eSize()
85 { wd = ht = -1; }
87 inline eSize::eSize( int w, int h )
88 { wd=w; ht=h; }
90 inline bool eSize::isNull() const
91 { return wd==0 && ht==0; }
93 inline bool eSize::isEmpty() const
94 { return wd<1 || ht<1; }
96 inline bool eSize::isValid() const
97 { return wd>=0 && ht>=0; }
99 inline int eSize::width() const
100 { return wd; }
102 inline int eSize::height() const
103 { return ht; }
105 inline void eSize::setWidth( int w )
106 { wd=w; }
108 inline void eSize::setHeight( int h )
109 { ht=h; }
111 inline int &eSize::rwidth()
112 { return wd; }
114 inline int &eSize::rheight()
115 { return ht; }
117 inline eSize &eSize::operator+=( const eSize &s )
118 { wd+=s.wd; ht+=s.ht; return *this; }
120 inline eSize &eSize::operator-=( const eSize &s )
121 { wd-=s.wd; ht-=s.ht; return *this; }
123 inline eSize &eSize::operator*=( int c )
124 { wd*=c; ht*=c; return *this; }
126 inline eSize &eSize::operator*=( double c )
127 { wd=(int)(wd*c); ht=(int)(ht*c); return *this; }
129 inline bool operator==( const eSize &s1, const eSize &s2 )
130 { return s1.wd == s2.wd && s1.ht == s2.ht; }
132 inline bool operator!=( const eSize &s1, const eSize &s2 )
133 { return s1.wd != s2.wd || s1.ht != s2.ht; }
135 inline eSize operator+( const eSize & s1, const eSize & s2 )
136 { return eSize(s1.wd+s2.wd, s1.ht+s2.ht); }
138 inline eSize operator-( const eSize &s1, const eSize &s2 )
139 { return eSize(s1.wd-s2.wd, s1.ht-s2.ht); }
141 inline eSize operator*( const eSize &s, int c )
142 { return eSize(s.wd*c, s.ht*c); }
144 inline eSize operator*( int c, const eSize &s )
145 { return eSize(s.wd*c, s.ht*c); }
147 inline eSize operator*( const eSize &s, double c )
148 { return eSize((int)(s.wd*c), (int)(s.ht*c)); }
150 inline eSize operator*( double c, const eSize &s )
151 { return eSize((int)(s.wd*c), (int)(s.ht*c)); }
153 inline eSize &eSize::operator/=( int c )
155 wd/=c; ht/=c;
156 return *this;
159 inline eSize &eSize::operator/=( double c )
161 wd=(int)(wd/c); ht=(int)(ht/c);
162 return *this;
165 inline eSize operator/( const eSize &s, int c )
167 return eSize(s.wd/c, s.ht/c);
170 inline eSize operator/( const eSize &s, double c )
172 return eSize((int)(s.wd/c), (int)(s.ht/c));
175 inline eSize eSize::expandedTo( const eSize & otherSize ) const
177 return eSize( MAX(wd,otherSize.wd), MAX(ht,otherSize.ht) );
180 inline eSize eSize::boundedTo( const eSize & otherSize ) const
182 return eSize( MIN(wd,otherSize.wd), MIN(ht,otherSize.ht) );
185 inline void eSize::transpose()
187 int tmp = wd;
188 wd = ht;
189 ht = tmp;
192 #endif // ESIZE_H