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 <typelib/typedescription.h>
21 #include <uno/data.h>
22 #include <cppuhelper/queryinterface.hxx>
23 #include <cppuhelper/typeprovider.hxx>
24 
25 #include "base.hxx"
26 
27 using namespace css::lang;
28 using namespace css::reflection;
29 using namespace css::uno;
30 
31 namespace stoc_corefl
32 {
33 
34 // XInterface
35 
queryInterface(const Type & rType)36 Any ArrayIdlClassImpl::queryInterface( const Type & rType )
37 {
38     Any aRet( ::cppu::queryInterface( rType, static_cast< XIdlArray * >( this ) ) );
39     return (aRet.hasValue() ? aRet : IdlClassImpl::queryInterface( rType ));
40 }
41 
acquire()42 void ArrayIdlClassImpl::acquire() throw()
43 {
44     IdlClassImpl::acquire();
45 }
46 
release()47 void ArrayIdlClassImpl::release() throw()
48 {
49     IdlClassImpl::release();
50 }
51 
52 // XTypeProvider
53 
getTypes()54 Sequence< Type > ArrayIdlClassImpl::getTypes()
55 {
56     static cppu::OTypeCollection s_aTypes(
57         cppu::UnoType<XIdlArray>::get(),
58         IdlClassImpl::getTypes() );
59 
60     return s_aTypes.getTypes();
61 }
62 
getImplementationId()63 Sequence< sal_Int8 > ArrayIdlClassImpl::getImplementationId()
64 {
65     return css::uno::Sequence<sal_Int8>();
66 }
67 
68 // XIdlArray
69 
realloc(Any & rArray,sal_Int32 nLen)70 void ArrayIdlClassImpl::realloc( Any & rArray, sal_Int32 nLen )
71 {
72     TypeClass eTC = rArray.getValueTypeClass();
73     if (eTC != TypeClass_SEQUENCE)
74     {
75         throw IllegalArgumentException(
76             "expected sequence, but found " + rArray.getValueType().getTypeName(),
77             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
78     }
79     if (nLen < 0)
80     {
81         throw IllegalArgumentException(
82             "negative length given!",
83             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 1 );
84     }
85 
86     uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
87     uno_sequence_realloc( ppSeq, &getTypeDescr()->aBase,
88                           nLen,
89                           reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
90                           reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
91     rArray.pData = ppSeq;
92 }
93 
getLen(const Any & rArray)94 sal_Int32 ArrayIdlClassImpl::getLen( const Any & rArray )
95 {
96     TypeClass eTC = rArray.getValueTypeClass();
97     if (eTC != TypeClass_SEQUENCE)
98     {
99         throw IllegalArgumentException(
100             "expected sequence, but found " + rArray.getValueType().getTypeName(),
101             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
102     }
103 
104     return (*static_cast<uno_Sequence * const *>(rArray.getValue()))->nElements;
105 }
106 
get(const Any & rArray,sal_Int32 nIndex)107 Any ArrayIdlClassImpl::get( const Any & rArray, sal_Int32 nIndex )
108 {
109     TypeClass eTC = rArray.getValueTypeClass();
110     if (eTC != TypeClass_SEQUENCE)
111     {
112         throw IllegalArgumentException(
113             "expected sequence, but found " + rArray.getValueType().getTypeName(),
114             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
115     }
116 
117     uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
118     if (pSeq->nElements <= nIndex)
119     {
120         throw ArrayIndexOutOfBoundsException(
121             "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
122             static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
123     }
124 
125     Any aRet;
126     typelib_TypeDescription * pElemTypeDescr = nullptr;
127     TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
128     uno_any_destruct( &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
129     uno_any_construct( &aRet, &pSeq->elements[nIndex * pElemTypeDescr->nSize],
130                        pElemTypeDescr,
131                        reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
132     TYPELIB_DANGER_RELEASE( pElemTypeDescr );
133     return aRet;
134 }
135 
136 
set(Any & rArray,sal_Int32 nIndex,const Any & rNewValue)137 void ArrayIdlClassImpl::set( Any & rArray, sal_Int32 nIndex, const Any & rNewValue )
138 {
139     TypeClass eTC = rArray.getValueTypeClass();
140     if (eTC != TypeClass_SEQUENCE)
141     {
142         throw IllegalArgumentException(
143             "expected sequence, but found " + rArray.getValueType().getTypeName(),
144             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
145     }
146 
147     uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
148     if (pSeq->nElements <= nIndex)
149     {
150         throw ArrayIndexOutOfBoundsException(
151             "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
152             static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
153     }
154 
155     uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
156     uno_sequence_reference2One(
157         ppSeq, &getTypeDescr()->aBase,
158         reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
159         reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
160     rArray.pData = ppSeq;
161     pSeq = *ppSeq;
162 
163     typelib_TypeDescription * pElemTypeDescr = nullptr;
164     TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
165 
166     if (! coerce_assign( &pSeq->elements[nIndex * pElemTypeDescr->nSize],
167                          pElemTypeDescr, rNewValue, getReflection() ))
168     {
169         TYPELIB_DANGER_RELEASE( pElemTypeDescr );
170         throw IllegalArgumentException(
171             "sequence element is not assignable by given value!",
172             static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 2 );
173     }
174     TYPELIB_DANGER_RELEASE( pElemTypeDescr );
175 }
176 
177 // ArrayIdlClassImpl
178 
isAssignableFrom(const Reference<XIdlClass> & xType)179 sal_Bool ArrayIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
180 {
181     return (xType.is() &&
182             (equals( xType ) ||
183              (xType->getTypeClass() == getTypeClass() && // must be sequence|array
184               getComponentType()->isAssignableFrom( xType->getComponentType() ))));
185 }
186 
getComponentType()187 Reference< XIdlClass > ArrayIdlClassImpl::getComponentType()
188 {
189     return getReflection()->forType( getTypeDescr()->pType );
190 }
191 
getArray()192 Reference< XIdlArray > ArrayIdlClassImpl::getArray()
193 {
194     return this;
195 }
196 
197 }
198 
199 
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
201