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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "ImplOPropertySet.hxx"
27 #include "CloneHelper.hxx"
28 
29 #include <algorithm>
30 #include <functional>
31 #include <com/sun/star/beans/XFastPropertySet.hpp>
32 
33 using namespace ::com::sun::star;
34 
35 using ::rtl::OUString;
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Any;
39 
40 namespace
41 {
42 
43 struct lcl_getPropertyStateByHandle :
44         public ::std::unary_function< sal_Int32,  beans::PropertyState >
45 {
lcl_getPropertyStateByHandle__anonea5d743d0111::lcl_getPropertyStateByHandle46     lcl_getPropertyStateByHandle(
47         const ::property::impl::ImplOPropertySet::tPropertyMap & rMap )
48             : m_rMap( rMap )
49     {}
50 
operator ()__anonea5d743d0111::lcl_getPropertyStateByHandle51     inline beans::PropertyState operator() ( sal_Int32 nHandle )
52     {
53         if( m_rMap.end() == m_rMap.find( nHandle ))
54             return beans::PropertyState_DEFAULT_VALUE;
55         return beans::PropertyState_DIRECT_VALUE;
56     }
57 
58 private:
59     const ::property::impl::ImplOPropertySet::tPropertyMap & m_rMap;
60 };
61 
62 template< typename K, typename V >
63 struct lcl_eraseMapEntry :
64         public ::std::unary_function< K, void >
65 {
lcl_eraseMapEntry__anonea5d743d0111::lcl_eraseMapEntry66     lcl_eraseMapEntry( ::std::map< K, V > & rMap )
67             : m_rMap( rMap )
68     {}
69 
operator ()__anonea5d743d0111::lcl_eraseMapEntry70     inline void operator() ( const K & aKey )
71     {
72         m_rMap.erase( aKey );
73     }
74 
75 private:
76     ::std::map< K, V > m_rMap;
77 };
78 
79 struct lcl_replaceInterfacePropertiesByClones :
80     public ::std::unary_function< ::property::impl::ImplOPropertySet::tPropertyMap::value_type, void >
81 {
operator ()__anonea5d743d0111::lcl_replaceInterfacePropertiesByClones82     inline void operator() ( ::property::impl::ImplOPropertySet::tPropertyMap::value_type & rProp )
83     {
84         if( rProp.second.hasValue() &&
85             rProp.second.getValueType().getTypeClass() == uno::TypeClass_INTERFACE )
86         {
87             Reference< util::XCloneable > xCloneable;
88             if( rProp.second >>= xCloneable )
89                 rProp.second <<= xCloneable->createClone();
90         }
91     }
92 };
93 
94 } //  anonymous namespace
95 
96 namespace property
97 {
98 namespace impl
99 {
100 
ImplOPropertySet()101 ImplOPropertySet::ImplOPropertySet()
102 {}
103 
ImplOPropertySet(const ImplOPropertySet & rOther)104 ImplOPropertySet::ImplOPropertySet( const ImplOPropertySet & rOther )
105 {
106     ::std::copy( rOther.m_aProperties.begin(), rOther.m_aProperties.end(),
107                  ::std::inserter( m_aProperties, m_aProperties.begin() ));
108     cloneInterfaceProperties();
109     m_xStyle.set( ::chart::CloneHelper::CreateRefClone< Reference< style::XStyle > >()( rOther.m_xStyle ));
110 }
111 
GetPropertyStateByHandle(sal_Int32 nHandle) const112 beans::PropertyState ImplOPropertySet::GetPropertyStateByHandle( sal_Int32 nHandle ) const
113 {
114     return lcl_getPropertyStateByHandle( m_aProperties ) ( nHandle );
115 }
116 
GetPropertyStatesByHandle(const::std::vector<sal_Int32> & aHandles) const117 Sequence< beans::PropertyState > ImplOPropertySet::GetPropertyStatesByHandle(
118     const ::std::vector< sal_Int32 > & aHandles ) const
119 {
120     Sequence< beans::PropertyState > aResult( aHandles.size());
121 
122     ::std::transform( aHandles.begin(), aHandles.end(),
123                       aResult.getArray(),
124                       lcl_getPropertyStateByHandle( m_aProperties ));
125 
126     return aResult;
127 }
128 
SetPropertyToDefault(sal_Int32 nHandle)129 void ImplOPropertySet::SetPropertyToDefault( sal_Int32 nHandle )
130 {
131     tPropertyMap::iterator aFoundIter( m_aProperties.find( nHandle ) );
132 
133     if( m_aProperties.end() != aFoundIter )
134     {
135         m_aProperties.erase( aFoundIter );
136     }
137 }
138 
SetPropertiesToDefault(const::std::vector<sal_Int32> & aHandles)139 void ImplOPropertySet::SetPropertiesToDefault(
140     const ::std::vector< sal_Int32 > & aHandles )
141 {
142     ::std::for_each( aHandles.begin(), aHandles.end(),
143                      lcl_eraseMapEntry< sal_Int32, Any >( m_aProperties ) );
144 }
145 
SetAllPropertiesToDefault()146 void ImplOPropertySet::SetAllPropertiesToDefault()
147 {
148     m_aProperties.clear();
149 }
150 
GetPropertyValueByHandle(Any & rValue,sal_Int32 nHandle) const151 bool ImplOPropertySet::GetPropertyValueByHandle(
152     Any & rValue,
153     sal_Int32 nHandle ) const
154 {
155     bool bResult = false;
156 
157     tPropertyMap::const_iterator aFoundIter( m_aProperties.find( nHandle ) );
158 
159     if( m_aProperties.end() != aFoundIter )
160     {
161         rValue = (*aFoundIter).second;
162         bResult = true;
163     }
164 
165     return bResult;
166 }
167 
SetPropertyValueByHandle(sal_Int32 nHandle,const Any & rValue,Any * pOldValue)168 void ImplOPropertySet::SetPropertyValueByHandle(
169     sal_Int32 nHandle, const Any & rValue, Any * pOldValue )
170 {
171     if( pOldValue != NULL )
172     {
173         tPropertyMap::const_iterator aFoundIter( m_aProperties.find( nHandle ) );
174         if( m_aProperties.end() != aFoundIter )
175             (*pOldValue) = (*aFoundIter).second;
176     }
177 
178     m_aProperties[ nHandle ] = rValue;
179 }
180 
SetStyle(const Reference<style::XStyle> & xStyle)181 bool ImplOPropertySet::SetStyle( const Reference< style::XStyle > & xStyle )
182 {
183     if( ! xStyle.is())
184         return false;
185 
186     m_xStyle = xStyle;
187     return true;
188 }
189 
GetStyle() const190 Reference< style::XStyle > ImplOPropertySet::GetStyle() const
191 {
192     return m_xStyle;
193 }
194 
cloneInterfaceProperties()195 void ImplOPropertySet::cloneInterfaceProperties()
196 {
197     ::std::for_each( m_aProperties.begin(), m_aProperties.end(),
198                      lcl_replaceInterfacePropertiesByClones());
199 }
200 
201 
202 } //  namespace impl
203 } //  namespace chart
204