LibreOffice Module stoc (master) 1
crarray.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 <typelib/typedescription.h>
21#include <uno/data.h>
24
25#include "base.hxx"
26
27using namespace css::lang;
28using namespace css::reflection;
29using namespace css::uno;
30
31namespace stoc_corefl
32{
33
34// XIdlArray
35
36void ArrayIdlClassImpl::realloc( Any & rArray, sal_Int32 nLen )
37{
38 TypeClass eTC = rArray.getValueTypeClass();
39 if (eTC != TypeClass_SEQUENCE)
40 {
41 throw IllegalArgumentException(
42 "expected sequence, but found " + rArray.getValueType().getTypeName(),
43 getXWeak(), 0 );
44 }
45 if (nLen < 0)
46 {
47 throw IllegalArgumentException(
48 "negative length given!",
49 getXWeak(), 1 );
50 }
51
52 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
53 uno_sequence_realloc( ppSeq, &getTypeDescr()->aBase,
54 nLen,
55 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
56 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
57 rArray.pData = ppSeq;
58}
59
60sal_Int32 ArrayIdlClassImpl::getLen( const Any & rArray )
61{
62 TypeClass eTC = rArray.getValueTypeClass();
63 if (eTC != TypeClass_SEQUENCE)
64 {
65 throw IllegalArgumentException(
66 "expected sequence, but found " + rArray.getValueType().getTypeName(),
67 getXWeak(), 0 );
68 }
69
70 return (*static_cast<uno_Sequence * const *>(rArray.getValue()))->nElements;
71}
72
73Any ArrayIdlClassImpl::get( const Any & rArray, sal_Int32 nIndex )
74{
75 TypeClass eTC = rArray.getValueTypeClass();
76 if (eTC != TypeClass_SEQUENCE)
77 {
78 throw IllegalArgumentException(
79 "expected sequence, but found " + rArray.getValueType().getTypeName(),
80 getXWeak(), 0 );
81 }
82
83 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
84 if (pSeq->nElements <= nIndex)
85 {
86 throw ArrayIndexOutOfBoundsException(
87 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
88 getXWeak() );
89 }
90
91 Any aRet;
92 typelib_TypeDescription * pElemTypeDescr = nullptr;
93 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
94 uno_any_destruct( &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
95 uno_any_construct( &aRet, &pSeq->elements[nIndex * pElemTypeDescr->nSize],
96 pElemTypeDescr,
97 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
98 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
99 return aRet;
100}
101
102
103void ArrayIdlClassImpl::set( Any & rArray, sal_Int32 nIndex, const Any & rNewValue )
104{
105 TypeClass eTC = rArray.getValueTypeClass();
106 if (eTC != TypeClass_SEQUENCE)
107 {
108 throw IllegalArgumentException(
109 "expected sequence, but found " + rArray.getValueType().getTypeName(),
110 getXWeak(), 0 );
111 }
112
113 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
114 if (pSeq->nElements <= nIndex)
115 {
116 throw ArrayIndexOutOfBoundsException(
117 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
118 getXWeak() );
119 }
120
121 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
123 ppSeq, &getTypeDescr()->aBase,
124 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
125 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
126 rArray.pData = ppSeq;
127 pSeq = *ppSeq;
128
129 typelib_TypeDescription * pElemTypeDescr = nullptr;
130 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
131
132 if (! coerce_assign( &pSeq->elements[nIndex * pElemTypeDescr->nSize],
133 pElemTypeDescr, rNewValue, getReflection() ))
134 {
135 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
136 throw IllegalArgumentException(
137 "sequence element is not assignable by given value!",
138 getXWeak(), 2 );
139 }
140 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
141}
142
143// ArrayIdlClassImpl
144
145sal_Bool ArrayIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
146{
147 return (xType.is() &&
148 (equals( xType ) ||
149 (xType->getTypeClass() == getTypeClass() && // must be sequence|array
150 getComponentType()->isAssignableFrom( xType->getComponentType() ))));
151}
152
154{
155 return getReflection()->forType( getTypeDescr()->pType );
156}
157
158Reference< XIdlArray > ArrayIdlClassImpl::getArray()
159{
160 return this;
161}
162
163}
164
165
166/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
virtual css::uno::Any SAL_CALL get(const css::uno::Any &rArray, sal_Int32 nIndex) override
Definition: crarray.cxx:73
virtual void SAL_CALL set(css::uno::Any &rArray, sal_Int32 nIndex, const css::uno::Any &rNewValue) override
Definition: crarray.cxx:103
typelib_IndirectTypeDescription * getTypeDescr() const
Definition: base.hxx:253
virtual sal_Int32 SAL_CALL getLen(const css::uno::Any &rArray) override
Definition: crarray.cxx:60
virtual void SAL_CALL realloc(css::uno::Any &rArray, sal_Int32 nLen) override
Definition: crarray.cxx:36
virtual sal_Bool SAL_CALL isAssignableFrom(const css::uno::Reference< css::reflection::XIdlClass > &xType) override
Definition: crarray.cxx:145
virtual css::uno::Reference< css::reflection::XIdlClass > SAL_CALL getComponentType() override
Definition: crarray.cxx:153
virtual css::uno::Reference< css::reflection::XIdlArray > SAL_CALL getArray() override
Definition: crarray.cxx:158
sal_Int32 nIndex
Definition: invocation.cxx:697
struct _typelib_TypeDescription typelib_TypeDescription
PyRef getTypeClass(const Runtime &)
bool coerce_assign(void *pDest, typelib_TypeDescription *pTD, const css::uno::Any &rSource, IdlReflectionServiceImpl *pRefl)
Definition: base.hxx:363
eTC
sal_Bool SAL_CALL uno_sequence_realloc(uno_Sequence **ppSequence, typelib_TypeDescription *pTypeDescr, sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
sal_Bool SAL_CALL uno_sequence_reference2One(uno_Sequence **ppSequence, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
unsigned char sal_Bool