bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / container / IndexedPropertyValuesContainer.cxx
blob9c54c3375b455df465bbbf2e57e8621324069cd7
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 .
20 #include "comphelper_module.hxx"
21 #include "comphelper_services.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>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <vector>
32 using namespace com::sun::star;
34 typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
36 class IndexedPropertyValuesContainer : public cppu::WeakImplHelper2< container::XIndexContainer, lang::XServiceInfo >
38 public:
39 IndexedPropertyValuesContainer() throw();
40 virtual ~IndexedPropertyValuesContainer() throw();
42 // XIndexContainer
43 virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
44 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
45 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
46 virtual void SAL_CALL removeByIndex( sal_Int32 nIndex )
47 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
48 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
50 // XIndexReplace
51 virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
52 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
53 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 // XIndexAccess
56 virtual sal_Int32 SAL_CALL getCount( )
57 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex )
59 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
60 ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 // XElementAccess
63 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
64 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 virtual sal_Bool SAL_CALL hasElements( )
66 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 //XServiceInfo
69 virtual OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 // XServiceInfo - static versions (used for component registration)
74 static OUString SAL_CALL getImplementationName_static();
75 static uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
76 static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
78 private:
79 IndexedPropertyValues maProperties;
82 IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
86 IndexedPropertyValuesContainer::~IndexedPropertyValuesContainer() throw()
90 // XIndexContainer
91 void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
92 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
93 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
95 sal_Int32 nSize(maProperties.size());
96 if ((nSize >= nIndex) && (nIndex >= 0))
98 uno::Sequence<beans::PropertyValue> aProps;
99 if (!(aElement >>= aProps))
100 throw lang::IllegalArgumentException();
101 if (nSize == nIndex)
102 maProperties.push_back(aProps);
103 else
105 IndexedPropertyValues::iterator aItr;
106 if ((nIndex * 2) < nSize)
108 aItr = maProperties.begin();
109 sal_Int32 i(0);
110 while(i < nIndex)
112 ++i;
113 ++aItr;
116 else
118 aItr = maProperties.end();
119 sal_Int32 i(nSize - 1);
120 while(i > nIndex)
122 --i;
123 --aItr;
126 maProperties.insert(aItr, aProps);
129 else
130 throw lang::IndexOutOfBoundsException();
133 void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
134 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
135 ::com::sun::star::uno::RuntimeException, std::exception)
137 sal_Int32 nSize(maProperties.size());
138 if ((nIndex < nSize) && (nIndex >= 0))
140 IndexedPropertyValues::iterator aItr;
141 if ((nIndex * 2) < nSize)
143 aItr = maProperties.begin();
144 sal_Int32 i(0);
145 while(i < nIndex)
147 ++i;
148 ++aItr;
151 else
153 aItr = maProperties.end();
154 sal_Int32 i(nSize - 1);
155 while(i > nIndex)
157 --i;
158 --aItr;
161 maProperties.erase(aItr);
163 else
164 throw lang::IndexOutOfBoundsException();
167 // XIndexReplace
168 void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& aElement )
169 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException,
170 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
172 sal_Int32 nSize(maProperties.size());
173 if ((nIndex < nSize) && (nIndex >= 0))
175 uno::Sequence<beans::PropertyValue> aProps;
176 if (!(aElement >>= aProps))
177 throw lang::IllegalArgumentException();
178 maProperties[nIndex] = aProps;
180 else
181 throw lang::IndexOutOfBoundsException();
184 // XIndexAccess
185 sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount( )
186 throw(::com::sun::star::uno::RuntimeException, std::exception)
188 return maProperties.size();
191 ::com::sun::star::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
192 throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException,
193 ::com::sun::star::uno::RuntimeException, std::exception)
195 sal_Int32 nSize(maProperties.size());
196 if (!((nIndex < nSize) && (nIndex >= 0)))
197 throw lang::IndexOutOfBoundsException();
199 uno::Any aAny;
200 aAny <<= maProperties[nIndex];
201 return aAny;
204 // XElementAccess
205 ::com::sun::star::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType( )
206 throw(::com::sun::star::uno::RuntimeException, std::exception)
208 return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get();
211 sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements( )
212 throw(::com::sun::star::uno::RuntimeException, std::exception)
214 return !maProperties.empty();
217 //XServiceInfo
218 OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
220 return getImplementationName_static();
223 OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName_static( )
225 return OUString( "IndexedPropertyValuesContainer" );
228 sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
230 return cppu::supportsService(this, ServiceName);
233 ::com::sun::star::uno::Sequence< OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
235 return getSupportedServiceNames_static();
239 ::com::sun::star::uno::Sequence< OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames_static( )
241 const OUString aServiceName( "com.sun.star.document.IndexedPropertyValues" );
242 const uno::Sequence< 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: */