Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / idl / source / objects / types.cxx
blob34b0e3b788474d0a9931a14840ca82725c999745
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 <sal/config.h>
22 #include <algorithm>
24 #include <tools/debug.hxx>
26 #include <types.hxx>
27 #include <globals.hxx>
28 #include <database.hxx>
30 SvMetaAttribute::SvMetaAttribute()
34 SvMetaAttribute::SvMetaAttribute( SvMetaType * pType )
35 : aType( pType )
39 SvMetaType * SvMetaAttribute::GetType() const
41 if( aType.is() || !GetRef() ) return aType.get();
42 return static_cast<SvMetaAttribute *>(GetRef())->GetType();
45 const SvIdentifier & SvMetaAttribute::GetSlotId() const
47 if( aSlotId.IsSet() || !GetRef() ) return aSlotId;
48 return static_cast<SvMetaAttribute *>(GetRef())->GetSlotId();
51 bool SvMetaAttribute::Test( SvTokenStream & rInStm )
53 if( GetType()->IsItem() && !GetSlotId().IsSet() )
55 throw SvParseException( rInStm, "slot without id declared" );
57 return true;
60 bool SvMetaAttribute::ReadSvIdl( SvIdlDataBase & rBase,
61 SvTokenStream & rInStm )
63 sal_uInt32 nTokPos = rInStm.Tell();
64 if( !GetType() )
65 // no type in ctor passed on
66 aType = rBase.ReadKnownType( rInStm );
67 bool bOk = false;
68 if( GetType() )
70 ReadNameSvIdl( rInStm );
71 aSlotId.ReadSvIdl( rBase, rInStm );
73 bOk = true;
74 SvToken& rTok = rInStm.GetToken();
75 if( rTok.IsChar() && rTok.GetChar() == '(' )
77 tools::SvRef<SvMetaType> xT(new SvMetaType() );
78 xT->SetRef( GetType() );
79 aType = xT;
80 bOk = aType->ReadMethodArgs( rBase, rInStm );
82 if( bOk )
83 bOk = SvMetaObject::ReadSvIdl( rBase, rInStm );
85 else
87 SvToken& rTok = rInStm.GetToken();
88 rBase.SetError( "unknown type of token. Each new SID needs an "
89 "item statement in an SDI file, eg. "
90 "SfxVoidItem FooItem " + rTok.GetTokenAsString() +
91 " ... which describes the slot more fully", rTok );
94 if( !bOk )
95 rInStm.Seek( nTokPos );
96 return bOk;
99 sal_uLong SvMetaAttribute::MakeSfx( OStringBuffer& rAttrArray ) const
101 SvMetaType * pType = GetType();
102 DBG_ASSERT( pType, "no type for attribute" );
103 SvMetaType * pBaseType = pType->GetBaseType();
104 DBG_ASSERT( pBaseType, "no base type for attribute" );
105 if( pBaseType->GetMetaTypeType() == MetaTypeType::Struct )
106 return pBaseType->MakeSfx( rAttrArray );
107 else
109 rAttrArray.append('{');
110 rAttrArray.append(GetSlotId().getString());
111 rAttrArray.append(",\"");
112 rAttrArray.append(GetName());
113 rAttrArray.append("\"}");
114 return 1;
118 void SvMetaAttribute::Insert(SvSlotElementList&)
122 #define CTOR \
123 : nType( MetaTypeType::Base ) \
124 , bIsItem( false ) \
126 SvMetaType::SvMetaType()
127 CTOR
131 SvMetaType::SvMetaType( const OString& rName )
132 CTOR
134 SetName( rName );
137 SvMetaType::~SvMetaType()
140 void SvMetaType::SetType( MetaTypeType nT )
142 nType = nT;
145 SvMetaType * SvMetaType::GetBaseType() const
147 if( GetRef() && GetMetaTypeType() == MetaTypeType::Base )
148 return static_cast<SvMetaType *>(GetRef())->GetBaseType();
149 return const_cast<SvMetaType *>(this);
152 SvMetaType * SvMetaType::GetReturnType() const
154 DBG_ASSERT( GetMetaTypeType() == MetaTypeType::Method, "no method" );
155 DBG_ASSERT( GetRef(), "no return type" );
156 return static_cast<SvMetaType *>(GetRef());
159 bool SvMetaType::ReadHeaderSvIdl( SvTokenStream & rInStm )
161 bool bOk = false;
162 sal_uInt32 nTokPos = rInStm.Tell();
163 SvToken& rTok = rInStm.GetToken_Next();
165 if( rTok.Is( SvHash_interface() ) )
167 SetType( MetaTypeType::Interface );
168 bOk = ReadNameSvIdl( rInStm );
170 else if( rTok.Is( SvHash_shell() ) )
172 SetType( MetaTypeType::Shell );
173 bOk = ReadNameSvIdl( rInStm );
175 if( !bOk )
176 rInStm.Seek( nTokPos );
177 return bOk;
180 bool SvMetaType::ReadSvIdl( SvIdlDataBase & rBase,
181 SvTokenStream & rInStm )
183 if( ReadHeaderSvIdl( rInStm ) )
185 rBase.Write(OString('.'));
186 return SvMetaReference::ReadSvIdl( rBase, rInStm );
188 return false;
191 void SvMetaType::ReadContextSvIdl( SvIdlDataBase & rBase,
192 SvTokenStream & rInStm )
194 tools::SvRef<SvMetaAttribute> xAttr( new SvMetaAttribute() );
195 if( xAttr->ReadSvIdl( rBase, rInStm ) )
197 if( xAttr->Test( rInStm ) )
198 GetAttrList().push_back( xAttr.get() );
202 sal_uLong SvMetaType::MakeSfx( OStringBuffer& rAttrArray )
204 sal_uLong nC = 0;
206 if( GetBaseType()->GetMetaTypeType() == MetaTypeType::Struct )
208 sal_uLong nAttrCount = GetAttrCount();
209 // write the single attributes
210 for( sal_uLong n = 0; n < nAttrCount; n++ )
212 nC += aAttrList[n]->MakeSfx( rAttrArray );
213 if( n +1 < nAttrCount )
214 rAttrArray.append(", ");
217 return nC;
220 void SvMetaType::WriteSfxItem(
221 const OString& rItemName, SvIdlDataBase const & rBase, SvStream& rOutStm )
223 WriteStars( rOutStm );
224 OString aVarName = " a" + rItemName + "_Impl";
226 OStringBuffer aAttrArray(1024);
227 sal_uLong nAttrCount = MakeSfx( aAttrArray );
228 OString aAttrCount( OString::number(nAttrCount));
229 OString aTypeName = "SfxType" + aAttrCount;
231 bool bExport = false, bReturn = false;
232 // these are exported from sfx library
233 if (rItemName == "SfxBoolItem" ||
234 rItemName == "SfxStringItem" ||
235 rItemName == "SfxUInt16Item" ||
236 rItemName == "SfxUInt32Item" ||
237 rItemName == "SfxVoidItem")
239 bExport = true;
240 if (!rBase.sSlotMapFile.endsWith("sfxslots.hxx"))
241 bReturn = true;
244 rOutStm.WriteCharPtr( "extern " );
245 if (bExport)
246 rOutStm.WriteCharPtr( "SFX2_DLLPUBLIC " );
247 rOutStm.WriteOString( aTypeName )
248 .WriteOString( aVarName ).WriteChar( ';' ) << endl;
249 if (bReturn)
250 return;
252 // write the implementation part
253 rOutStm.WriteCharPtr( "#ifdef SFX_TYPEMAP" ) << endl;
254 rOutStm.WriteCharPtr( "#if !defined(_WIN32) && ((defined(DISABLE_DYNLOADING) && (defined(ANDROID) || defined(IOS) || defined(LINUX))) || STATIC_LINKING)" ) << endl;
255 rOutStm.WriteCharPtr( "__attribute__((__weak__))" ) << endl;
256 rOutStm.WriteCharPtr( "#endif" ) << endl;
257 rOutStm.WriteOString( aTypeName ).WriteOString( aVarName )
258 .WriteCharPtr( " = " ) << endl;
259 rOutStm.WriteChar( '{' ) << endl;
261 rOutStm.WriteCharPtr( "\tcreateSfxPoolItem<" ).WriteOString( rItemName )
262 .WriteCharPtr(">, &typeid(").WriteOString( rItemName ).WriteCharPtr( "), " );
263 rOutStm.WriteOString( aAttrCount );
264 if( nAttrCount )
266 rOutStm.WriteCharPtr( ", { " );
267 // write the single attributes
268 rOutStm.WriteCharPtr( aAttrArray.getStr() );
269 rOutStm.WriteCharPtr( " }" );
271 rOutStm << endl;
272 rOutStm.WriteCharPtr( "};" ) << endl;
273 rOutStm.WriteCharPtr( "#endif" ) << endl << endl;
276 void SvMetaType::WriteSfx( SvIdlDataBase & rBase, SvStream & rOutStm )
278 if( IsItem() )
280 if( GetBaseType()->GetMetaTypeType() == MetaTypeType::Struct )
281 GetBaseType()->WriteSfxItem( GetName(), rBase, rOutStm );
282 else
283 WriteSfxItem( GetName(), rBase, rOutStm );
287 bool SvMetaType::ReadMethodArgs( SvIdlDataBase & rBase,
288 SvTokenStream & rInStm )
290 sal_uInt32 nTokPos = rInStm.Tell();
291 if( rInStm.ReadIf( '(' ) )
293 DoReadContextSvIdl( rBase, rInStm );
294 if( rInStm.ReadIf( ')' ) )
296 SetType( MetaTypeType::Method );
297 return true;
300 rInStm.Seek( nTokPos );
301 return false;
304 SvMetaTypeString::SvMetaTypeString()
305 : SvMetaType( "String" )
309 SvMetaEnumValue::SvMetaEnumValue()
313 SvMetaTypeEnum::SvMetaTypeEnum()
317 SvMetaTypevoid::SvMetaTypevoid()
318 : SvMetaType( "void" )
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */