Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / comphelper / source / container / IndexedPropertyValuesContainer.cxx
blob5e6ffe03c449dea378f484d886220125696783a0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "comphelper_module.hxx"
23 #include <com/sun/star/container/XIndexContainer.hpp>
24 #include <com/sun/star/uno/Sequence.h>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <cppuhelper/implbase2.hxx>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <vector>
31 using namespace com::sun::star;
33 typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
35 class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
37 public:
38 IndexedPropertyValuesContainer() throw();
39 virtual ~IndexedPropertyValuesContainer() throw();
41 // XIndexContainer
42 virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
43 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
44 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
45 virtual void SAL_CALL removeByIndex( sal_Int32 nIndex )
46 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
47 ::com::sun::star::uno::RuntimeException);
49 // XIndexReplace
50 virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
51 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
52 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
54 // XIndexAccess
55 virtual sal_Int32 SAL_CALL getCount( )
56 throw(::com::sun::star::uno::RuntimeException);
57 virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex )
58 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
59 ::com::sun::star::uno::RuntimeException);
61 // XElementAccess
62 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
63 throw(::com::sun::star::uno::RuntimeException);
64 virtual sal_Bool SAL_CALL hasElements( )
65 throw(::com::sun::star::uno::RuntimeException);
67 //XServiceInfo
68 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
69 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
70 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException);
72 // XServiceInfo - static versions (used for component registration)
73 static ::rtl::OUString SAL_CALL getImplementationName_static();
74 static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
75 static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
77 private:
78 IndexedPropertyValues maProperties;
81 IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
85 IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
89 // XIndexContainer
90 void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
91 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
92 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
94 sal_Int32 nSize(maProperties.size());
95 if ((nSize >= nIndex) && (nIndex >= 0))
97 uno::Sequence<beans::PropertyValue> aProps;
98 if (!(aElement >>= aProps))
99 throw lang::IllegalArgumentException();
100 if (nSize == nIndex)
101 maProperties.push_back(aProps);
102 else
104 IndexedPropertyValues::iterator aItr;
105 if ((nIndex * 2) < nSize)
107 aItr = maProperties.begin();
108 sal_Int32 i(0);
109 while(i < nIndex)
111 ++i;
112 ++aItr;
115 else
117 aItr = maProperties.end();
118 sal_Int32 i(nSize - 1);
119 while(i > nIndex)
121 --i;
122 --aItr;
125 maProperties.insert(aItr, aProps);
128 else
129 throw lang::IndexOutOfBoundsException();
132 void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
133 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
134 ::com::sun::star::uno::RuntimeException)
136 sal_Int32 nSize(maProperties.size());
137 if ((nIndex < nSize) && (nIndex >= 0))
139 IndexedPropertyValues::iterator aItr;
140 if ((nIndex * 2) < nSize)
142 aItr = maProperties.begin();
143 sal_Int32 i(0);
144 while(i < nIndex)
146 ++i;
147 ++aItr;
150 else
152 aItr = maProperties.end();
153 sal_Int32 i(nSize - 1);
154 while(i > nIndex)
156 --i;
157 --aItr;
160 maProperties.erase(aItr);
162 else
163 throw lang::IndexOutOfBoundsException();
166 // XIndexReplace
167 void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
168 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
169 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
171 sal_Int32 nSize(maProperties.size());
172 if ((nIndex < nSize) && (nIndex >= 0))
174 uno::Sequence<beans::PropertyValue> aProps;
175 if (!(aElement >>= aProps))
176 throw lang::IllegalArgumentException();
177 maProperties[nIndex] = aProps;
179 else
180 throw lang::IndexOutOfBoundsException();
183 // XIndexAccess
184 sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount( )
185 throw(::com::sun::star::uno::RuntimeException)
187 return maProperties.size();
190 ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
191 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
192 ::com::sun::star::uno::RuntimeException)
194 sal_Int32 nSize(maProperties.size());
195 if (!((nIndex < nSize) && (nIndex >= 0)))
196 throw lang::IndexOutOfBoundsException();
198 uno::Any aAny;
199 aAny <<= maProperties[nIndex];
200 return aAny;
203 // XElementAccess
204 ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType( )
205 throw(::com::sun::star::uno::RuntimeException)
207 return ::getCppuType((uno::Sequence<beans::PropertyValue> *)0);
210 sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements( )
211 throw(::com::sun::star::uno::RuntimeException)
213 return !maProperties.empty();
216 //XServiceInfo
217 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException)
219 return getImplementationName_static();
222 ::rtl::OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static( )
224 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IndexedPropertyValuesContainer" ) );
227 sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
229 rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
230 return aServiceName == ServiceName;
233 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException)
235 return getSupportedServiceNames_static();
239 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static( )
241 const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) );
242 const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
243 return aSeq;
247 uno::Reference< uno::XInterface > SAL_CALL IndexedPropertyValuesContainer::Create(
248 SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >&)
250 return (cppu::OWeakObject*)new IndexedPropertyValuesContainer();
253 void createRegistryInfo_IndexedPropertyValuesContainer()
255 static ::comphelper::module::OAutoRegistration< IndexedPropertyValuesContainer > aAutoRegistration;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */