Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / stoc / source / corereflection / crenum.cxx
blobca090763fd129c75090b30fe7bb66376151ee227
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 "base.hxx"
22 #include <cppuhelper/queryinterface.hxx>
24 using namespace css::lang;
25 using namespace css::reflection;
26 using namespace css::uno;
28 namespace stoc_corefl
32 class IdlEnumFieldImpl
33 : public IdlMemberImpl
34 , public XIdlField
35 , public XIdlField2
37 sal_Int32 _nValue;
39 public:
40 IdlEnumFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
41 typelib_TypeDescription * pTypeDescr, sal_Int32 nValue )
42 : IdlMemberImpl( pReflection, rName, pTypeDescr, pTypeDescr )
43 , _nValue( nValue )
46 // XInterface
47 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
48 virtual void SAL_CALL acquire() throw() override;
49 virtual void SAL_CALL release() throw() override;
51 // XTypeProvider
52 virtual Sequence< Type > SAL_CALL getTypes() override;
53 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
55 // XIdlMember
56 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
57 virtual OUString SAL_CALL getName() override;
58 // XIdlField
59 virtual Reference< XIdlClass > SAL_CALL getType() override;
60 virtual FieldAccessMode SAL_CALL getAccessMode() override;
61 virtual Any SAL_CALL get( const Any & rObj ) override;
62 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
63 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
64 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
67 // XInterface
69 Any IdlEnumFieldImpl::queryInterface( const Type & rType )
71 Any aRet( ::cppu::queryInterface( rType,
72 static_cast< XIdlField * >( this ),
73 static_cast< XIdlField2 * >( this ) ) );
74 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
77 void IdlEnumFieldImpl::acquire() throw()
79 IdlMemberImpl::acquire();
82 void IdlEnumFieldImpl::release() throw()
84 IdlMemberImpl::release();
87 // XTypeProvider
89 Sequence< Type > IdlEnumFieldImpl::getTypes()
91 static ::cppu::OTypeCollection * s_pTypes = nullptr;
92 if (! s_pTypes)
94 ::osl::MutexGuard aGuard( getMutexAccess() );
95 if (! s_pTypes)
97 static ::cppu::OTypeCollection s_aTypes(
98 cppu::UnoType<XIdlField2>::get(),
99 cppu::UnoType<XIdlField>::get(),
100 IdlMemberImpl::getTypes() );
101 s_pTypes = &s_aTypes;
104 return s_pTypes->getTypes();
107 Sequence< sal_Int8 > IdlEnumFieldImpl::getImplementationId()
109 return css::uno::Sequence<sal_Int8>();
112 // XIdlMember
114 Reference< XIdlClass > IdlEnumFieldImpl::getDeclaringClass()
116 return IdlMemberImpl::getDeclaringClass();
119 OUString IdlEnumFieldImpl::getName()
121 return IdlMemberImpl::getName();
124 // XIdlField
126 Reference< XIdlClass > IdlEnumFieldImpl::getType()
128 return getDeclaringClass();
131 FieldAccessMode IdlEnumFieldImpl::getAccessMode()
133 return FieldAccessMode_READONLY;
136 Any IdlEnumFieldImpl::get( const Any & )
138 return Any( &_nValue, getTypeDescr() );
141 void IdlEnumFieldImpl::set( const Any &, const Any & )
143 throw IllegalAccessException(
144 "cannot set enum field, it is constant",
145 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
148 void IdlEnumFieldImpl::set( Any &, const Any & )
150 throw IllegalAccessException(
151 "cannot set enum field, it is constant",
152 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
156 EnumIdlClassImpl::~EnumIdlClassImpl()
160 // IdlClassImpl modifications
162 Reference< XIdlField > EnumIdlClassImpl::getField( const OUString & rName )
164 if (! _pFields)
165 getFields(); // init members
167 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
168 if (iFind != _aName2Field.end())
169 return (*iFind).second;
170 else
171 return Reference< XIdlField >();
174 Sequence< Reference< XIdlField > > EnumIdlClassImpl::getFields()
176 if (! _pFields)
178 ::osl::MutexGuard aGuard( getMutexAccess() );
179 if (! _pFields)
181 sal_Int32 nFields = getTypeDescr()->nEnumValues;
182 Sequence< Reference< XIdlField > > * pFields =
183 new Sequence< Reference< XIdlField > >( nFields );
184 Reference< XIdlField > * pSeq = pFields->getArray();
186 while (nFields--)
188 OUString aName( getTypeDescr()->ppEnumNames[nFields] );
189 _aName2Field[aName] = pSeq[nFields] = new IdlEnumFieldImpl(
190 getReflection(), aName, IdlClassImpl::getTypeDescr(), getTypeDescr()->pEnumValues[nFields] );
193 _pFields.reset( pFields );
196 return *_pFields;
199 void EnumIdlClassImpl::createObject( Any & rObj )
201 sal_Int32 eVal =
202 reinterpret_cast<typelib_EnumTypeDescription *>(IdlClassImpl::getTypeDescr())->nDefaultEnumValue;
203 rObj.setValue( &eVal, IdlClassImpl::getTypeDescr() );
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */