xref: /trunk/main/chart2/source/inc/DisposeHelper.hxx (revision de7b3f82)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef CHART2_DISPOSEHELPER_HXX
24 #define CHART2_DISPOSEHELPER_HXX
25 
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/lang/XComponent.hpp>
28 
29 #include <functional>
30 #include <algorithm>
31 #include <utility>
32 
33 namespace chart
34 {
35 namespace DisposeHelper
36 {
37 
38 template< class T >
Dispose(const T & xIntf)39     void Dispose( const T & xIntf )
40 {
41     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp(
42         xIntf, ::com::sun::star::uno::UNO_QUERY );
43     if( xComp.is())
44         xComp->dispose();
45 }
46 
47 template< class Intf >
DisposeAndClear(::com::sun::star::uno::Reference<Intf> & rIntf)48     void DisposeAndClear( ::com::sun::star::uno::Reference< Intf > & rIntf )
49 {
50     Dispose< ::com::sun::star::uno::Reference< Intf > >( rIntf );
51     rIntf.set( 0 );
52 }
53 
54 template< class T >
55     struct DisposeFunctor : public ::std::unary_function< T, void >
56 {
operator ()chart::DisposeHelper::DisposeFunctor57     void operator() ( const T & xIntf )
58     {
59         Dispose< T >( xIntf );
60     }
61 };
62 
63 template< typename T >
64     struct DisposeFirstOfPairFunctor : public ::std::unary_function< T, void >
65 {
operator ()chart::DisposeHelper::DisposeFirstOfPairFunctor66     void operator() ( const T & rElem )
67     {
68         Dispose< typename T::first_type >( rElem.first );
69     }
70 };
71 
72 template< typename T >
73     struct DisposeSecondOfPairFunctor : public ::std::unary_function< T, void >
74 {
operator ()chart::DisposeHelper::DisposeSecondOfPairFunctor75     void operator() ( const T & rElem )
76     {
77         Dispose< typename T::second_type >( rElem.second );
78     }
79 };
80 
81 template< class Container >
DisposeAllElements(Container & rContainer)82     void DisposeAllElements( Container & rContainer )
83 {
84     ::std::for_each( rContainer.begin(), rContainer.end(), DisposeFunctor< typename Container::value_type >());
85 }
86 
87 template< class Map >
DisposeAllMapElements(Map & rContainer)88     void DisposeAllMapElements( Map & rContainer )
89 {
90     ::std::for_each( rContainer.begin(), rContainer.end(), DisposeSecondOfPairFunctor< typename Map::value_type >());
91 }
92 
93 } //  namespace DisposeHelper
94 } //  namespace chart
95 
96 // CHART2_DISPOSEHELPER_HXX
97 #endif
98