Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / corereflection / crarray.cxx
blob4bdfa4867054105fadaa6a2937ed2a899a9b6c8e
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 <typelib/typedescription.h>
21 #include <uno/data.h>
22 #include <cppuhelper/queryinterface.hxx>
24 #include "base.hxx"
26 using namespace css::lang;
27 using namespace css::reflection;
28 using namespace css::uno;
30 namespace stoc_corefl
33 // XInterface
35 Any ArrayIdlClassImpl::queryInterface( const Type & rType )
37 Any aRet( ::cppu::queryInterface( rType, static_cast< XIdlArray * >( this ) ) );
38 return (aRet.hasValue() ? aRet : IdlClassImpl::queryInterface( rType ));
41 void ArrayIdlClassImpl::acquire() throw()
43 IdlClassImpl::acquire();
46 void ArrayIdlClassImpl::release() throw()
48 IdlClassImpl::release();
51 // XTypeProvider
53 Sequence< Type > ArrayIdlClassImpl::getTypes()
55 static ::cppu::OTypeCollection * s_pTypes = nullptr;
56 if (! s_pTypes)
58 ::osl::MutexGuard aGuard( getMutexAccess() );
59 if (! s_pTypes)
61 static ::cppu::OTypeCollection s_aTypes(
62 cppu::UnoType<XIdlArray>::get(),
63 IdlClassImpl::getTypes() );
64 s_pTypes = &s_aTypes;
67 return s_pTypes->getTypes();
70 Sequence< sal_Int8 > ArrayIdlClassImpl::getImplementationId()
72 return css::uno::Sequence<sal_Int8>();
75 // XIdlArray
77 void ArrayIdlClassImpl::realloc( Any & rArray, sal_Int32 nLen )
79 TypeClass eTC = rArray.getValueTypeClass();
80 if (eTC != TypeClass_SEQUENCE)
82 throw IllegalArgumentException(
83 "expected sequence, but found " + rArray.getValueType().getTypeName(),
84 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
86 if (nLen < 0)
88 throw IllegalArgumentException(
89 "negative length given!",
90 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
93 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
94 uno_sequence_realloc( ppSeq, &getTypeDescr()->aBase,
95 nLen,
96 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
97 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
98 rArray.pData = ppSeq;
101 sal_Int32 ArrayIdlClassImpl::getLen( const Any & rArray )
103 TypeClass eTC = rArray.getValueTypeClass();
104 if (eTC != TypeClass_SEQUENCE)
106 throw IllegalArgumentException(
107 "expected sequence, but found " + rArray.getValueType().getTypeName(),
108 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
111 return (*static_cast<uno_Sequence * const *>(rArray.getValue()))->nElements;
114 Any ArrayIdlClassImpl::get( const Any & rArray, sal_Int32 nIndex )
116 TypeClass eTC = rArray.getValueTypeClass();
117 if (eTC != TypeClass_SEQUENCE)
119 throw IllegalArgumentException(
120 "expected sequence, but found " + rArray.getValueType().getTypeName(),
121 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
124 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
125 if (pSeq->nElements <= nIndex)
127 throw ArrayIndexOutOfBoundsException(
128 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
129 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
132 Any aRet;
133 typelib_TypeDescription * pElemTypeDescr = nullptr;
134 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
135 uno_any_destruct( &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
136 uno_any_construct( &aRet, &pSeq->elements[nIndex * pElemTypeDescr->nSize],
137 pElemTypeDescr,
138 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
139 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
140 return aRet;
144 void ArrayIdlClassImpl::set( Any & rArray, sal_Int32 nIndex, const Any & rNewValue )
146 TypeClass eTC = rArray.getValueTypeClass();
147 if (eTC != TypeClass_SEQUENCE)
149 throw IllegalArgumentException(
150 "expected sequence, but found " + rArray.getValueType().getTypeName(),
151 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
154 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
155 if (pSeq->nElements <= nIndex)
157 throw ArrayIndexOutOfBoundsException(
158 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
159 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
162 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
163 uno_sequence_reference2One(
164 ppSeq, &getTypeDescr()->aBase,
165 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
166 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
167 rArray.pData = ppSeq;
168 pSeq = *ppSeq;
170 typelib_TypeDescription * pElemTypeDescr = nullptr;
171 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
173 if (! coerce_assign( &pSeq->elements[nIndex * pElemTypeDescr->nSize],
174 pElemTypeDescr, rNewValue, getReflection() ))
176 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
177 throw IllegalArgumentException(
178 "sequence element is not assignable by given value!",
179 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 2 );
181 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
184 // ArrayIdlClassImpl
186 sal_Bool ArrayIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
188 return (xType.is() &&
189 (equals( xType ) ||
190 (xType->getTypeClass() == getTypeClass() && // must be sequence|array
191 getComponentType()->isAssignableFrom( xType->getComponentType() ))));
194 Reference< XIdlClass > ArrayIdlClassImpl::getComponentType()
196 return getReflection()->forType( getTypeDescr()->pType );
199 Reference< XIdlArray > ArrayIdlClassImpl::getArray()
201 return this;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */