xref: /trunk/main/sd/source/ui/unoidl/unowcntr.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org 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 Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <tools/list.hxx>
32 
33 #include <unowcntr.hxx>
34 
35 using namespace ::rtl;
36 using namespace ::com::sun::star;
37 
38 DECLARE_LIST( WeakRefList, uno::WeakReference< uno::XInterface >* )
39 
40 SvUnoWeakContainer::SvUnoWeakContainer() throw()
41 {
42 	mpList = new WeakRefList;
43 }
44 
45 SvUnoWeakContainer::~SvUnoWeakContainer() throw()
46 {
47 	uno::WeakReference< uno::XInterface >* pRef = mpList->First();
48 	while( pRef )
49 	{
50 		delete mpList->Remove();
51 		pRef = mpList->GetCurObject();
52 	}
53 	delete mpList;
54 }
55 
56 /** inserts the given ref into this container */
57 void SvUnoWeakContainer::insert( uno::WeakReference< uno::XInterface > xRef ) throw()
58 {
59 	uno::WeakReference< uno::XInterface >* pRef = mpList->First();
60 	while( pRef )
61 	{
62 		::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >  xTestRef( *pRef );
63 		if(! xTestRef.is() )
64 		{
65 			delete mpList->Remove();
66 			pRef = mpList->GetCurObject();
67 		}
68 		else
69 		{
70 			if( *pRef == xRef )
71 				return;
72 
73 			pRef = mpList->Next();
74 		}
75 	}
76 
77 	mpList->Insert( new uno::WeakReference< uno::XInterface >( xRef ) );
78 }
79 
80 /** searches the container for a ref that returns true on the given
81 	search function
82 */
83 sal_Bool SvUnoWeakContainer::findRef( uno::WeakReference< uno::XInterface >& rRef, void* pSearchData, weakref_searchfunc pSearchFunc )
84 {
85 	uno::WeakReference< uno::XInterface >* pRef = mpList->First();
86 	while( pRef )
87 	{
88 		uno::Reference< ::com::sun::star::uno::XInterface > xTestRef( *pRef );
89 		if(!xTestRef.is())
90 		{
91 			delete mpList->Remove();
92 			pRef = mpList->GetCurObject();
93 		}
94 		else
95 		{
96 			if( (*pSearchFunc)( *pRef, pSearchData ) )
97 			{
98 				rRef = *pRef;
99 				return sal_True;
100 			}
101 
102 			pRef = mpList->Next();
103 		}
104 	}
105 
106 	return sal_False;
107 }
108 
109 void SvUnoWeakContainer::dispose()
110 {
111 	uno::WeakReference< uno::XInterface >* pRef = mpList->First();
112 	while( pRef )
113 	{
114 		uno::Reference< uno::XInterface > xTestRef( *pRef );
115 		if(xTestRef.is())
116 		{
117 			uno::Reference< lang::XComponent > xComp( xTestRef, uno::UNO_QUERY );
118 			if( xComp.is() )
119 				xComp->dispose();
120 		}
121 
122 		pRef = mpList->Next();
123 	}
124 }
125 
126