1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
29 /// Reference couting pointer
30 template <class T> class CountedPtr
36 explicit CountedPtr( pointer pPtr = 0 ): m_pCounter( 0 )
38 if( pPtr ) m_pCounter = new Counter( pPtr );
41 ~CountedPtr() { release(); }
43 CountedPtr( const CountedPtr &rPtr ) { acquire( rPtr.m_pCounter ); }
45 CountedPtr &operator=( const CountedPtr &rPtr )
50 acquire( rPtr.m_pCounter );
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; }
59 pointer get() const { return m_pCounter ? m_pCounter->m_pPtr : 0; }
63 return ( m_pCounter ? m_pCounter->m_count == 1 : true );
69 Counter( pointer pPtr = 0, unsigned int c = 1 )
70 : m_pPtr( pPtr ), m_count( c ) { }
75 void acquire( Counter* pCount )
78 if( pCount ) ++pCount->m_count;
85 if( --m_pCounter->m_count == 0 )
87 delete m_pCounter->m_pPtr;
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 ) { }
107 // rect2 fully included in rect1
108 static bool isIncluded( const rect& rect2, const rect& rect1 )
112 int w1 = rect1.width;
113 int h1 = rect1.height;
117 int w2 = rect2.width;
118 int h2 = rect2.height;
120 return x2 >= x1 && x2 + w2 <= x1 + w1
121 && y2 >= y1 && y2 + h2 <= y1 + h1;
124 static bool areDisjunct( const rect& rect2, const rect& rect1 )
128 int w1 = rect1.width;
129 int h1 = rect1.height;
133 int w2 = rect2.width;
134 int h2 = rect2.height;
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
142 static bool intersect( const rect& rect1, const rect& rect2, rect* pRect )
146 int w1 = rect1.width;
147 int h1 = rect1.height;
151 int w2 = rect2.width;
152 int h2 = rect2.height;
154 if( areDisjunct( rect1, rect2 ) )
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 );
164 pRect->width = right - left + 1;
165 pRect->height = bottom - top + 1;
167 return pRect->width > 0 && pRect->height > 0;
171 static bool join( const rect& rect1, const rect& rect2, rect* pRect )
175 int w1 = rect1.width;
176 int h1 = rect1.height;
180 int w2 = rect2.width;
181 int h2 = rect2.height;
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 );
189 pRect->width = right - left + 1;
190 pRect->height = bottom - top + 1;
192 return pRect->width > 0 && pRect->height > 0;
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; }