xref: /trunk/main/comphelper/source/container/IndexedPropertyValuesContainer.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_comphelper.hxx"
26 
27 #include "comphelper_module.hxx"
28 
29 #include <com/sun/star/container/XIndexContainer.hpp>
30 #include <com/sun/star/uno/Sequence.h>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <cppuhelper/implbase2.hxx>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 
36 
37 #ifndef __SGI_STL_VECTOR
38 #include <vector>
39 #endif
40 
41 using namespace com::sun::star;
42 
43 typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
44 
45 class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
46 {
47 public:
48     IndexedPropertyValuesContainer() throw();
49     virtual ~IndexedPropertyValuesContainer() throw();
50 
51     // XIndexContainer
52     virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement );
53     virtual void SAL_CALL removeByIndex( sal_Int32 nIndex );
54 
55     // XIndexReplace
56     virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement );
57 
58     // XIndexAccess
59     virtual sal_Int32 SAL_CALL getCount(  );
60     virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex );
61 
62     // XElementAccess
63     virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  );
64     virtual sal_Bool SAL_CALL hasElements(  );
65 
66     //XServiceInfo
67     virtual ::rtl::OUString SAL_CALL getImplementationName(  );
68     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName );
69     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  );
70 
71     // XServiceInfo - static versions (used for component registration)
72     static ::rtl::OUString SAL_CALL getImplementationName_static();
73     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
74     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
75 
76 private:
77     IndexedPropertyValues maProperties;
78 };
79 
IndexedPropertyValuesContainer()80 IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
81 {
82 }
83 
~IndexedPropertyValuesContainer()84 IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
85 {
86 }
87 
88 // XIndexContainer
insertByIndex(sal_Int32 nIndex,const::com::sun::star::uno::Any & aElement)89 void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
90 {
91     sal_Int32 nSize(maProperties.size());
92     if ((nSize >= nIndex) && (nIndex >= 0))
93     {
94         uno::Sequence<beans::PropertyValue> aProps;
95         if (!(aElement >>= aProps))
96             throw lang::IllegalArgumentException();
97         if (nSize == nIndex)
98             maProperties.push_back(aProps);
99         else
100         {
101             IndexedPropertyValues::iterator aItr;
102             if ((nIndex * 2) < nSize)
103             {
104                 aItr = maProperties.begin();
105                 sal_Int32 i(0);
106                 while(i < nIndex)
107                 {
108                     i++;
109                     aItr++;
110                 }
111             }
112             else
113             {
114                 aItr = maProperties.end();
115                 sal_Int32 i(nSize - 1);
116                 while(i > nIndex)
117                 {
118                     i--;
119                     aItr--;
120                 }
121             }
122             maProperties.insert(aItr, aProps);
123         }
124     }
125     else
126         throw lang::IndexOutOfBoundsException();
127 }
128 
removeByIndex(sal_Int32 nIndex)129 void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
130 {
131     sal_Int32 nSize(maProperties.size());
132     if ((nIndex < nSize) && (nIndex >= 0))
133     {
134         IndexedPropertyValues::iterator aItr;
135         if ((nIndex * 2) < nSize)
136         {
137             aItr = maProperties.begin();
138             sal_Int32 i(0);
139             while(i < nIndex)
140             {
141                 i++;
142                 aItr++;
143             }
144         }
145         else
146         {
147             aItr = maProperties.end();
148             sal_Int32 i(nSize - 1);
149             while(i > nIndex)
150             {
151                 i--;
152                 aItr--;
153             }
154         }
155         maProperties.erase(aItr);
156     }
157     else
158         throw lang::IndexOutOfBoundsException();
159 }
160 
161 // XIndexReplace
replaceByIndex(sal_Int32 nIndex,const::com::sun::star::uno::Any & aElement)162 void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
163 {
164     sal_Int32 nSize(maProperties.size());
165     if ((nIndex < nSize) && (nIndex >= 0))
166     {
167         uno::Sequence<beans::PropertyValue> aProps;
168         if (!(aElement >>= aProps))
169             throw lang::IllegalArgumentException();
170         maProperties[nIndex] = aProps;
171     }
172     else
173         throw lang::IndexOutOfBoundsException();
174 }
175 
176 // XIndexAccess
getCount()177 sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount(  )
178 {
179     return maProperties.size();
180 }
181 
getByIndex(sal_Int32 nIndex)182 ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
183 {
184     sal_Int32 nSize(maProperties.size());
185     if (!((nIndex < nSize) && (nIndex >= 0)))
186         throw lang::IndexOutOfBoundsException();
187 
188     uno::Any aAny;
189     aAny <<= maProperties[nIndex];
190     return aAny;
191 }
192 
193 // XElementAccess
getElementType()194 ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType(  )
195 {
196     return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
197 }
198 
hasElements()199 sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements(  )
200 {
201     return !maProperties.empty();
202 }
203 
204 //XServiceInfo
getImplementationName()205 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName(  )
206 {
207     return getImplementationName_static();
208 }
209 
getImplementationName_static()210 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static(  )
211 {
212     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IndexedPropertyValuesContainer" ) );
213 }
214 
supportsService(const::rtl::OUString & ServiceName)215 sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName )
216 {
217     rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
218     return aServiceName == ServiceName;
219 }
220 
getSupportedServiceNames()221 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames(  )
222 {
223     return getSupportedServiceNames_static();
224 }
225 
226 
getSupportedServiceNames_static()227 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static(  )
228 {
229     const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
230     const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
231     return aSeq;
232 }
233 
234 
Create(const uno::Reference<uno::XComponentContext> &)235 uno::Reference< uno::XInterface > SAL_CALL IndexedPropertyValuesContainer::Create(
236                 const uno::Reference< uno::XComponentContext >&)
237 {
238     return (cppu::OWeakObject*)new IndexedPropertyValuesContainer();
239 }
240 
createRegistryInfo_IndexedPropertyValuesContainer()241 void createRegistryInfo_IndexedPropertyValuesContainer()
242 {
243     static ::comphelper::module::OAutoRegistration< IndexedPropertyValuesContainer > aAutoRegistration;
244 }
245