]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/pointer.hpp
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / skins2 / utils / pointer.hpp
1 /*****************************************************************************
2  * pointer.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef POINTER_HPP
26 #define POINTER_HPP
27
28
29 /// Reference couting pointer
30 template <class T> class CountedPtr
31 {
32 public:
33     typedef T *pointer;
34     typedef T &reference;
35
36     explicit CountedPtr( pointer pPtr = 0 ): m_pCounter( 0 )
37     {
38         if( pPtr ) m_pCounter = new Counter( pPtr );
39     }
40
41     ~CountedPtr() { release(); }
42
43     CountedPtr( const CountedPtr &rPtr ) { acquire( rPtr.m_pCounter ); }
44
45     CountedPtr &operator=( const CountedPtr &rPtr )
46     {
47         if( this != &rPtr )
48         {
49             release();
50             acquire( rPtr.m_pCounter );
51         }
52         return *this;
53     }
54
55     // XXX Somebody explain why operator* and operator-> don't use get()
56     reference operator*() const { return *m_pCounter->m_pPtr; }
57     pointer   operator->() const { return m_pCounter->m_pPtr; }
58
59     pointer get() const { return m_pCounter ? m_pCounter->m_pPtr : 0; }
60
61     bool unique() const
62     {
63         return ( m_pCounter ? m_pCounter->m_count == 1 : true );
64     }
65
66 private:
67     struct Counter
68     {
69         Counter( pointer pPtr = 0, unsigned int c = 1 )
70                : m_pPtr( pPtr ), m_count( c ) { }
71         pointer m_pPtr;
72         unsigned int m_count;
73     } *m_pCounter;
74
75     void acquire( Counter* pCount )
76     {
77         m_pCounter = pCount;
78         if( pCount ) ++pCount->m_count;
79     }
80
81     void release()
82     {
83         if( m_pCounter )
84         {
85             if( --m_pCounter->m_count == 0 )
86             {
87                 delete m_pCounter->m_pPtr;
88                 delete m_pCounter;
89             }
90             m_pCounter = 0;
91         }
92     }
93 };
94
95
96 class rect
97 {
98 public:
99     rect( int v_x = 0, int v_y = 0, int v_width = 0, int v_height = 0 )
100         : x( v_x ), y( v_y ), width( v_width ), height( v_height ) { }
101     ~rect() { }
102     int x;
103     int y;
104     int width;
105     int height;
106
107     // rect2 fully included in rect1
108     static bool isIncluded( const rect& rect2, const rect& rect1 )
109     {
110         int x1 = rect1.x;
111         int y1 = rect1.y;
112         int w1 = rect1.width;
113         int h1 = rect1.height;
114
115         int x2 = rect2.x;
116         int y2 = rect2.y;
117         int w2 = rect2.width;
118         int h2 = rect2.height;
119
120         return     x2 >= x1 && x2 + w2 <= x1 + w1
121                &&  y2 >= y1 && y2 + h2 <= y1 + h1;
122     }
123
124     static bool areDisjunct( const rect& rect2, const rect& rect1 )
125     {
126         int x1 = rect1.x;
127         int y1 = rect1.y;
128         int w1 = rect1.width;
129         int h1 = rect1.height;
130
131         int x2 = rect2.x;
132         int y2 = rect2.y;
133         int w2 = rect2.width;
134         int h2 = rect2.height;
135
136         return    y2 + h2 -1 < y1  // rect2 above rect1
137                || y2 > y1 + h1 - 1  // rect2 under rect1
138                || x2 > x1 + w1 -1  // rect2 right of rect1
139                || x2 + w2 - 1 < x1; // rect2 left of rect1
140     }
141
142     static bool intersect( const rect& rect1, const rect& rect2, rect* pRect )
143     {
144         int x1 = rect1.x;
145         int y1 = rect1.y;
146         int w1 = rect1.width;
147         int h1 = rect1.height;
148
149         int x2 = rect2.x;
150         int y2 = rect2.y;
151         int w2 = rect2.width;
152         int h2 = rect2.height;
153
154         if( areDisjunct( rect1, rect2 ) )
155             return false;
156         else
157         {
158             int left = max( x1, x2 );
159             int right = min( x1 + w1 - 1, x2 + w2 - 1 );
160             int top = max( y1, y2 );
161             int bottom = min( y1 + h1 - 1, y2 + h2 -1 );
162             pRect->x = left;
163             pRect->y = top;
164             pRect->width = right - left + 1;
165             pRect->height = bottom - top + 1;
166
167             return pRect->width > 0 && pRect->height > 0;
168         }
169     }
170
171     static bool join( const rect& rect1, const rect& rect2, rect* pRect )
172     {
173         int x1 = rect1.x;
174         int y1 = rect1.y;
175         int w1 = rect1.width;
176         int h1 = rect1.height;
177
178         int x2 = rect2.x;
179         int y2 = rect2.y;
180         int w2 = rect2.width;
181         int h2 = rect2.height;
182
183         int left = min( x1, x2 );
184         int right = max( x1 + w1 - 1, x2 + w2 - 1 );
185         int top = min( y1, y2 );
186         int bottom = max( y1 + h1 - 1, y2 + h2 -1 );
187         pRect->x = left;
188         pRect->y = top;
189         pRect->width = right - left + 1;
190         pRect->height = bottom - top + 1;
191
192         return pRect->width > 0 && pRect->height > 0;
193     }
194     static int min( int x, int y ) { return x < y ? x : y; }
195     static int max( int x, int y ) { return x < y ? y : x; }
196
197 };
198
199
200 #endif