LibreOffice Module framework (master) 1
itemcontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26#include <rtl/ref.hxx>
27
28using namespace cppu;
29using namespace com::sun::star::uno;
30using namespace com::sun::star::lang;
31using namespace com::sun::star::beans;
32using namespace com::sun::star::container;
33
34constexpr OUStringLiteral WRONG_TYPE_EXCEPTION
35 = u"Type must be css::uno::Sequence< css::beans::PropertyValue >";
36
37namespace framework
38{
39
40// XInterface, XTypeProvider
41
43 m_aShareMutex( rMutex )
44{
45}
46
47ItemContainer::ItemContainer( const ConstItemContainer& rConstItemContainer, const ShareableMutex& rMutex ) : m_aShareMutex( rMutex )
48{
49 copyItemContainer( rConstItemContainer.m_aItemVector, rMutex );
50}
51
52ItemContainer::ItemContainer( const Reference< XIndexAccess >& rSourceContainer, const ShareableMutex& rMutex ) :
53 m_aShareMutex( rMutex )
54{
55 if ( !rSourceContainer.is() )
56 return;
57
58 sal_Int32 nCount = rSourceContainer->getCount();
59 try
60 {
61 for ( sal_Int32 i = 0; i < nCount; i++ )
62 {
64 if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
65 {
66 sal_Int32 nContainerIndex = -1;
67 Reference< XIndexAccess > xIndexAccess;
68 for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
69 {
70 if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
71 {
72 aPropSeq[j].Value >>= xIndexAccess;
73 nContainerIndex = j;
74 break;
75 }
76 }
77
78 if ( xIndexAccess.is() && nContainerIndex >= 0 )
79 aPropSeq.getArray()[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
80
81 m_aItemVector.push_back( aPropSeq );
82 }
83 }
84 }
85 catch ( const IndexOutOfBoundsException& )
86 {
87 }
88}
89
91{
92}
93
94// private
95void ItemContainer::copyItemContainer( const std::vector< Sequence< PropertyValue > >& rSourceVector, const ShareableMutex& rMutex )
96{
97 const sal_uInt32 nCount = rSourceVector.size();
98 for ( sal_uInt32 i = 0; i < nCount; ++i )
99 {
100 sal_Int32 nContainerIndex = -1;
101 Sequence< PropertyValue > aPropSeq( rSourceVector[i] );
102 Reference< XIndexAccess > xIndexAccess;
103 for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
104 {
105 if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
106 {
107 aPropSeq[j].Value >>= xIndexAccess;
108 nContainerIndex = j;
109 break;
110 }
111 }
112
113 if ( xIndexAccess.is() && nContainerIndex >= 0 )
114 aPropSeq.getArray()[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
115
116 m_aItemVector.push_back( aPropSeq );
117 }
118}
119
121{
123 if ( rSubContainer.is() )
124 {
125 ConstItemContainer* pSource = dynamic_cast<ConstItemContainer*>( rSubContainer.get() );
126 rtl::Reference<ItemContainer> pSubContainer;
127 if ( pSource )
128 pSubContainer = new ItemContainer( *pSource, rMutex );
129 else
130 pSubContainer = new ItemContainer( rSubContainer, rMutex );
131 xReturn = pSubContainer;
132 }
133
134 return xReturn;
135}
136
137// XElementAccess
139{
140 ShareGuard aLock( m_aShareMutex );
141 return ( !m_aItemVector.empty() );
142}
143
144// XIndexAccess
145sal_Int32 SAL_CALL ItemContainer::getCount()
146{
147 ShareGuard aLock( m_aShareMutex );
148 return m_aItemVector.size();
149}
150
152{
153 ShareGuard aLock( m_aShareMutex );
154 if ( sal_Int32( m_aItemVector.size()) <= Index )
155 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
156
157 return Any( m_aItemVector[Index] );
158}
159
160// XIndexContainer
161void SAL_CALL ItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
162{
164 if ( !(aItem >>= aSeq) )
165 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION,
166 static_cast<OWeakObject *>(this), 2 );
167
168 ShareGuard aLock( m_aShareMutex );
169 if ( sal_Int32( m_aItemVector.size()) == Index )
170 m_aItemVector.push_back( aSeq );
171 else if ( sal_Int32( m_aItemVector.size()) >Index )
172 {
173 std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
174 aIter += Index;
175 m_aItemVector.insert( aIter, aSeq );
176 }
177 else
178 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
179}
180
181void SAL_CALL ItemContainer::removeByIndex( sal_Int32 nIndex )
182{
183 ShareGuard aLock( m_aShareMutex );
184 if ( static_cast<sal_Int32>(m_aItemVector.size()) <= nIndex )
185 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
186
187 m_aItemVector.erase(m_aItemVector.begin() + nIndex);
188}
189
190void SAL_CALL ItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
191{
193 if ( !(aItem >>= aSeq) )
194 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION,
195 static_cast<OWeakObject *>(this), 2 );
196
197 ShareGuard aLock( m_aShareMutex );
198 if ( sal_Int32( m_aItemVector.size()) <= Index )
199 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
200
202}
203
204} // namespace framework
205
206/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
std::vector< css::uno::Sequence< css::beans::PropertyValue > > m_aItemVector
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
virtual ~ItemContainer() override
virtual sal_Int32 SAL_CALL getCount() override
css::uno::Reference< css::container::XIndexAccess > deepCopyContainer(const css::uno::Reference< css::container::XIndexAccess > &rSubContainer, const ShareableMutex &rMutex)
virtual void SAL_CALL insertByIndex(sal_Int32 Index, const css::uno::Any &Element) override
ShareableMutex m_aShareMutex
ItemContainer(const ShareableMutex &)
virtual void SAL_CALL removeByIndex(sal_Int32 Index) override
virtual void SAL_CALL replaceByIndex(sal_Int32 Index, const css::uno::Any &Element) override
virtual sal_Bool SAL_CALL hasElements() override
void copyItemContainer(const std::vector< css::uno::Sequence< css::beans::PropertyValue > > &rSourceVector, const ShareableMutex &rMutex)
std::vector< css::uno::Sequence< css::beans::PropertyValue > > m_aItemVector
This acts like a rtl::Reference<osl::Mutex>
int nCount
float u
sal_Int32 nIndex
constexpr OUStringLiteral WRONG_TYPE_EXCEPTION
Sequence< sal_Int8 > aSeq
int i
OUString Name
unsigned char sal_Bool