Bump version to 24.04.3.4
[LibreOffice.git] / idl / source / objects / types.cxx
blob1089e929043ca3552389092401054249eba8c187
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"_ostr );
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 size_t 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 SvMetaType::SvMetaType()
123 : nType( MetaTypeType::Base )
124 , bIsItem( false )
128 SvMetaType::SvMetaType( const OString& rName )
129 : SvMetaType()
131 SetName( rName );
134 SvMetaType::~SvMetaType()
137 void SvMetaType::SetType( MetaTypeType nT )
139 nType = nT;
142 SvMetaType * SvMetaType::GetBaseType() const
144 if( GetRef() && GetMetaTypeType() == MetaTypeType::Base )
145 return static_cast<SvMetaType *>(GetRef())->GetBaseType();
146 return const_cast<SvMetaType *>(this);
149 SvMetaType * SvMetaType::GetReturnType() const
151 DBG_ASSERT( GetMetaTypeType() == MetaTypeType::Method, "no method" );
152 DBG_ASSERT( GetRef(), "no return type" );
153 return static_cast<SvMetaType *>(GetRef());
156 bool SvMetaType::ReadHeaderSvIdl( SvTokenStream & rInStm )
158 bool bOk = false;
159 sal_uInt32 nTokPos = rInStm.Tell();
160 SvToken& rTok = rInStm.GetToken_Next();
162 if( rTok.Is( SvHash_interface() ) )
164 SetType( MetaTypeType::Interface );
165 bOk = ReadNameSvIdl( rInStm );
167 else if( rTok.Is( SvHash_shell() ) )
169 SetType( MetaTypeType::Shell );
170 bOk = ReadNameSvIdl( rInStm );
172 if( !bOk )
173 rInStm.Seek( nTokPos );
174 return bOk;
177 bool SvMetaType::ReadSvIdl( SvIdlDataBase & rBase,
178 SvTokenStream & rInStm )
180 if( ReadHeaderSvIdl( rInStm ) )
182 rBase.Write(OString('.'));
183 return SvMetaReference::ReadSvIdl( rBase, rInStm );
185 return false;
188 void SvMetaType::ReadContextSvIdl( SvIdlDataBase & rBase,
189 SvTokenStream & rInStm )
191 tools::SvRef<SvMetaAttribute> xAttr( new SvMetaAttribute() );
192 if( xAttr->ReadSvIdl( rBase, rInStm ) )
194 if( xAttr->Test( rInStm ) )
195 GetAttrList().push_back( xAttr.get() );
199 size_t SvMetaType::MakeSfx( OStringBuffer& rAttrArray )
201 size_t nC = 0;
203 if( GetBaseType()->GetMetaTypeType() == MetaTypeType::Struct )
205 size_t nAttrCount = GetAttrCount();
206 // write the single attributes
207 for( size_t n = 0; n < nAttrCount; n++ )
209 nC += aAttrList[n]->MakeSfx( rAttrArray );
210 if( n +1 < nAttrCount )
211 rAttrArray.append(", ");
214 return nC;
217 void SvMetaType::WriteSfxItem(
218 std::string_view rItemName, SvIdlDataBase const & rBase, SvStream& rOutStm )
220 WriteStars( rOutStm );
221 OString aVarName = OString::Concat(" a") + rItemName + "_Impl";
223 OStringBuffer aAttrArray(1024);
224 size_t nAttrCount = MakeSfx( aAttrArray );
225 OString aAttrCount( OString::number(nAttrCount));
226 OString aTypeName = "SfxType" + aAttrCount;
228 bool bExport = false, bReturn = false;
229 // these are exported from sfx library
230 if (rItemName == "SfxBoolItem" ||
231 rItemName == "SfxInt16Item" ||
232 rItemName == "SfxStringItem" ||
233 rItemName == "SfxUInt16Item" ||
234 rItemName == "SfxUInt32Item" ||
235 rItemName == "SfxVoidItem")
237 bExport = true;
238 if (!rBase.sSlotMapFile.endsWith("sfxslots.hxx"))
239 bReturn = true;
242 rOutStm.WriteOString( "extern " );
243 if (bExport)
244 rOutStm.WriteOString( "SFX2_DLLPUBLIC " );
245 rOutStm.WriteOString( aTypeName )
246 .WriteOString( aVarName ).WriteChar( ';' ) << endl;
247 if (bReturn)
248 return;
250 // write the implementation part
251 rOutStm.WriteOString( "#ifdef SFX_TYPEMAP" ) << endl;
252 rOutStm.WriteOString( "#if !defined(_WIN32) && (defined(DISABLE_DYNLOADING) && (defined(ANDROID) || defined(IOS) || defined(EMSCRIPTEN) || defined(LINUX)))" ) << endl;
253 rOutStm.WriteOString( "__attribute__((__weak__))" ) << endl;
254 rOutStm.WriteOString( "#endif" ) << endl;
255 rOutStm.WriteOString( aTypeName ).WriteOString( aVarName )
256 .WriteOString( " = " ) << endl;
257 rOutStm.WriteChar( '{' ) << endl;
259 rOutStm.WriteOString( "\tcreateSfxPoolItem<" ).WriteOString( rItemName )
260 .WriteOString(">, &typeid(").WriteOString( rItemName ).WriteOString( "), " );
261 rOutStm.WriteOString( aAttrCount );
262 if( nAttrCount )
264 rOutStm.WriteOString( ", { " );
265 // write the single attributes
266 rOutStm.WriteOString( aAttrArray );
267 rOutStm.WriteOString( " }" );
269 rOutStm << endl;
270 rOutStm.WriteOString( "};" ) << endl;
271 rOutStm.WriteOString( "#endif" ) << endl << endl;
274 void SvMetaType::WriteSfx( SvIdlDataBase & rBase, SvStream & rOutStm )
276 if( IsItem() )
278 if( GetBaseType()->GetMetaTypeType() == MetaTypeType::Struct )
279 GetBaseType()->WriteSfxItem( GetName(), rBase, rOutStm );
280 else
281 WriteSfxItem( GetName(), rBase, rOutStm );
285 bool SvMetaType::ReadMethodArgs( SvIdlDataBase & rBase,
286 SvTokenStream & rInStm )
288 sal_uInt32 nTokPos = rInStm.Tell();
289 if( rInStm.ReadIf( '(' ) )
291 DoReadContextSvIdl( rBase, rInStm );
292 if( rInStm.ReadIf( ')' ) )
294 SetType( MetaTypeType::Method );
295 return true;
298 rInStm.Seek( nTokPos );
299 return false;
302 SvMetaTypeString::SvMetaTypeString()
303 : SvMetaType( "String"_ostr )
307 SvMetaEnumValue::SvMetaEnumValue()
311 SvMetaTypeEnum::SvMetaTypeEnum()
315 SvMetaTypevoid::SvMetaTypevoid()
316 : SvMetaType( "void"_ostr )
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */