xref: /trunk/main/stoc/source/corereflection/crenum.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26 
27 #include "base.hxx"
28 
29 namespace stoc_corefl
30 {
31 
32 //==================================================================================================
33 class IdlEnumFieldImpl
34     : public IdlMemberImpl
35     , public XIdlField
36     , public XIdlField2
37 {
38     sal_Int32               _nValue;
39 
40 public:
41     IdlEnumFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
42                       typelib_TypeDescription * pTypeDescr, sal_Int32 nValue )
43         : IdlMemberImpl( pReflection, rName, pTypeDescr, pTypeDescr )
44         , _nValue( nValue )
45         {}
46     virtual ~IdlEnumFieldImpl();
47 
48     // XInterface
49     virtual Any SAL_CALL queryInterface( const Type & rType );
50     virtual void SAL_CALL acquire() throw();
51     virtual void SAL_CALL release() throw();
52 
53     // XTypeProvider
54     virtual Sequence< Type > SAL_CALL getTypes();
55     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId();
56 
57     // XIdlMember
58     virtual Reference< XIdlClass > SAL_CALL getDeclaringClass();
59     virtual OUString SAL_CALL getName();
60     // XIdlField
61     virtual Reference< XIdlClass > SAL_CALL getType();
62     virtual FieldAccessMode SAL_CALL getAccessMode();
63     virtual Any SAL_CALL get( const Any & rObj );
64     virtual void SAL_CALL set( const Any & rObj, const Any & rValue );
65     // XIdlField2: getType, getAccessMode and get are equal to XIdlField
66     virtual void SAL_CALL set( Any & rObj, const Any & rValue );
67 };
68 //__________________________________________________________________________________________________
69 IdlEnumFieldImpl::~IdlEnumFieldImpl()
70 {
71 }
72 
73 // XInterface
74 //__________________________________________________________________________________________________
75 Any IdlEnumFieldImpl::queryInterface( const Type & rType )
76 {
77     Any aRet( ::cppu::queryInterface( rType,
78                                       static_cast< XIdlField * >( this ),
79                                       static_cast< XIdlField2 * >( this ) ) );
80     return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
81 }
82 //__________________________________________________________________________________________________
83 void IdlEnumFieldImpl::acquire() throw()
84 {
85     IdlMemberImpl::acquire();
86 }
87 //__________________________________________________________________________________________________
88 void IdlEnumFieldImpl::release() throw()
89 {
90     IdlMemberImpl::release();
91 }
92 
93 // XTypeProvider
94 //__________________________________________________________________________________________________
95 Sequence< Type > IdlEnumFieldImpl::getTypes()
96 {
97     static OTypeCollection * s_pTypes = 0;
98     if (! s_pTypes)
99     {
100         MutexGuard aGuard( getMutexAccess() );
101         if (! s_pTypes)
102         {
103             static OTypeCollection s_aTypes(
104                 ::getCppuType( (const Reference< XIdlField2 > *)0 ),
105                 ::getCppuType( (const Reference< XIdlField > *)0 ),
106                 IdlMemberImpl::getTypes() );
107             s_pTypes = &s_aTypes;
108         }
109     }
110     return s_pTypes->getTypes();
111 }
112 //__________________________________________________________________________________________________
113 Sequence< sal_Int8 > IdlEnumFieldImpl::getImplementationId()
114 {
115     static OImplementationId * s_pId = 0;
116     if (! s_pId)
117     {
118         MutexGuard aGuard( getMutexAccess() );
119         if (! s_pId)
120         {
121             static OImplementationId s_aId;
122             s_pId = &s_aId;
123         }
124     }
125     return s_pId->getImplementationId();
126 }
127 
128 // XIdlMember
129 //__________________________________________________________________________________________________
130 Reference< XIdlClass > IdlEnumFieldImpl::getDeclaringClass()
131 {
132     return IdlMemberImpl::getDeclaringClass();
133 }
134 //__________________________________________________________________________________________________
135 OUString IdlEnumFieldImpl::getName()
136 {
137     return IdlMemberImpl::getName();
138 }
139 
140 // XIdlField
141 //__________________________________________________________________________________________________
142 Reference< XIdlClass > IdlEnumFieldImpl::getType()
143 {
144     return getDeclaringClass();
145 }
146 //__________________________________________________________________________________________________
147 FieldAccessMode IdlEnumFieldImpl::getAccessMode()
148 {
149     return FieldAccessMode_READONLY;
150 }
151 //__________________________________________________________________________________________________
152 Any IdlEnumFieldImpl::get( const Any & )
153 {
154     return Any( &_nValue, getTypeDescr() );
155 }
156 //__________________________________________________________________________________________________
157 void IdlEnumFieldImpl::set( const Any &, const Any & )
158 {
159     throw IllegalAccessException(
160         OUString( RTL_CONSTASCII_USTRINGPARAM("enum field is constant!") ),
161         (XWeak *)(OWeakObject *)this );
162 }
163 //__________________________________________________________________________________________________
164 void IdlEnumFieldImpl::set( Any &, const Any & )
165 {
166     throw IllegalAccessException(
167         OUString( RTL_CONSTASCII_USTRINGPARAM("enum field is constant!") ),
168         (XWeak *)(OWeakObject *)this );
169 }
170 
171 //##################################################################################################
172 //##################################################################################################
173 //##################################################################################################
174 
175 
176 //__________________________________________________________________________________________________
177 EnumIdlClassImpl::~EnumIdlClassImpl()
178 {
179     delete _pFields;
180 }
181 
182 // IdlClassImpl modifications
183 //__________________________________________________________________________________________________
184 Reference< XIdlField > EnumIdlClassImpl::getField( const OUString & rName )
185 {
186     if (! _pFields)
187         getFields(); // init members
188 
189     const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
190     if (iFind != _aName2Field.end())
191         return (*iFind).second;
192     else
193         return Reference< XIdlField >();
194 }
195 //__________________________________________________________________________________________________
196 Sequence< Reference< XIdlField > > EnumIdlClassImpl::getFields()
197 {
198     if (! _pFields)
199     {
200         MutexGuard aGuard( getMutexAccess() );
201         if (! _pFields)
202         {
203             sal_Int32 nFields = getTypeDescr()->nEnumValues;
204             Sequence< Reference< XIdlField > > * pFields =
205                 new Sequence< Reference< XIdlField > >( nFields );
206             Reference< XIdlField > * pSeq = pFields->getArray();
207 
208             while (nFields--)
209             {
210                 OUString aName( getTypeDescr()->ppEnumNames[nFields] );
211                 _aName2Field[aName] = pSeq[nFields] = new IdlEnumFieldImpl(
212                     getReflection(), aName, IdlClassImpl::getTypeDescr(), getTypeDescr()->pEnumValues[nFields] );
213             }
214 
215             _pFields = pFields;
216         }
217     }
218     return *_pFields;
219 }
220 //__________________________________________________________________________________________________
221 void EnumIdlClassImpl::createObject( Any & rObj )
222 {
223     sal_Int32 eVal =
224         ((typelib_EnumTypeDescription *)IdlClassImpl::getTypeDescr())->nDefaultEnumValue;
225     rObj.setValue( &eVal, IdlClassImpl::getTypeDescr() );
226 }
227 
228 }
229