xref: /aoo42x/main/sot/source/base/factory.cxx (revision 046d9d1f)
1*046d9d1fSAndrew Rist /**************************************************************
2*046d9d1fSAndrew Rist  *
3*046d9d1fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*046d9d1fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*046d9d1fSAndrew Rist  * distributed with this work for additional information
6*046d9d1fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*046d9d1fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*046d9d1fSAndrew Rist  * "License"); you may not use this file except in compliance
9*046d9d1fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*046d9d1fSAndrew Rist  *
11*046d9d1fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*046d9d1fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*046d9d1fSAndrew Rist  * software distributed under the License is distributed on an
15*046d9d1fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*046d9d1fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*046d9d1fSAndrew Rist  * specific language governing permissions and limitations
18*046d9d1fSAndrew Rist  * under the License.
19*046d9d1fSAndrew Rist  *
20*046d9d1fSAndrew Rist  *************************************************************/
21*046d9d1fSAndrew Rist 
22*046d9d1fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sot.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define _SOT_FACTORY_CXX
28cdf0e10cSrcweir #define SOT_STRING_LIST
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <sot/factory.hxx>
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir #include <tools/string.hxx>
33cdf0e10cSrcweir #include <sot/object.hxx>
34cdf0e10cSrcweir #include <sot/sotdata.hxx>
35cdf0e10cSrcweir #include <sot/clsids.hxx>
36cdf0e10cSrcweir #include <rtl/instance.hxx>
37cdf0e10cSrcweir #include <com/sun/star/datatransfer/DataFlavor.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /************** class SotData_Impl *********************************************/
40cdf0e10cSrcweir /*************************************************************************
41cdf0e10cSrcweir |*    SotData_Impl::SotData_Impl
42cdf0e10cSrcweir |*
43cdf0e10cSrcweir |*    Beschreibung
44cdf0e10cSrcweir *************************************************************************/
SotData_Impl()45cdf0e10cSrcweir SotData_Impl::SotData_Impl()
46cdf0e10cSrcweir     : nSvObjCount( 0 )
47cdf0e10cSrcweir     , pObjectList( NULL )
48cdf0e10cSrcweir     , pFactoryList( NULL )
49cdf0e10cSrcweir     , pSotObjectFactory( NULL )
50cdf0e10cSrcweir     , pSotStorageStreamFactory( NULL )
51cdf0e10cSrcweir     , pSotStorageFactory( NULL )
52cdf0e10cSrcweir     , pDataFlavorList( NULL )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir }
55cdf0e10cSrcweir /*************************************************************************
56cdf0e10cSrcweir |*    SOTDATA()
57cdf0e10cSrcweir |*
58cdf0e10cSrcweir |*    Beschreibung
59cdf0e10cSrcweir *************************************************************************/
60cdf0e10cSrcweir namespace { struct ImplData : public rtl::Static<SotData_Impl, ImplData> {}; }
SOTDATA()61cdf0e10cSrcweir SotData_Impl * SOTDATA()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	return &ImplData::get();
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir /*************************************************************************
67cdf0e10cSrcweir |*    SotFactory::DeInit()
68cdf0e10cSrcweir |*
69cdf0e10cSrcweir |*    Beschreibung
70cdf0e10cSrcweir *************************************************************************/
DeInit()71cdf0e10cSrcweir void SotFactory::DeInit()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if( pSotData->nSvObjCount )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir #ifdef DBG_UTIL
78cdf0e10cSrcweir 		ByteString aStr( "Objects alive: " );
79cdf0e10cSrcweir 		aStr.Append( ByteString::CreateFromInt32( pSotData->nSvObjCount ) );
80cdf0e10cSrcweir         DBG_WARNING(  aStr.GetBuffer()  );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir /*
83cdf0e10cSrcweir         SotObjectList *pObjList = pSotData->pObjectList;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         if( pObjList )
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             SotObject * p = pObjList->First();
88cdf0e10cSrcweir             while( p )
89cdf0e10cSrcweir             {
90cdf0e10cSrcweir                 String aStr( "Factory: " );
91cdf0e10cSrcweir                 aStr += p->GetSvFactory()->GetClassName();
92cdf0e10cSrcweir                 aStr += " Count: ";
93cdf0e10cSrcweir                 aStr += p->GetRefCount();
94cdf0e10cSrcweir                 DBG_TRACE( "\tReferences:" );
95cdf0e10cSrcweir                 p->TestObjRef( sal_False );
96cdf0e10cSrcweir #ifdef TEST_INVARIANT
97cdf0e10cSrcweir                 DBG_TRACE( "\tInvariant:" );
98cdf0e10cSrcweir                 p->TestInvariant( sal_True );
99cdf0e10cSrcweir #endif
100cdf0e10cSrcweir                 p = pObjList->Next();
101cdf0e10cSrcweir             }
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir */
104cdf0e10cSrcweir #endif
105cdf0e10cSrcweir         return;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // Muss von hinten nach vorne zerstoert werden. Das ist die umgekehrte
109cdf0e10cSrcweir     // Reihenfolge der Erzeugung
110cdf0e10cSrcweir     SotFactoryList* pFactoryList = pSotData->pFactoryList;
111cdf0e10cSrcweir     if( pFactoryList )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         SotFactory * pFact = pFactoryList->Last();
114cdf0e10cSrcweir         while( NULL != (pFact = pFactoryList->Remove()) )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             delete pFact;
117cdf0e10cSrcweir             pFact = pFactoryList->Last();
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         delete pFactoryList;
120cdf0e10cSrcweir         pSotData->pFactoryList = NULL;
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     delete pSotData->pObjectList;
124cdf0e10cSrcweir     pSotData->pObjectList = NULL;
125cdf0e10cSrcweir 	if( pSotData->pDataFlavorList )
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		for( sal_uLong i = 0, nMax = pSotData->pDataFlavorList->Count(); i < nMax; i++ )
129cdf0e10cSrcweir 			delete (::com::sun::star::datatransfer::DataFlavor*) pSotData->pDataFlavorList->GetObject( i );
130cdf0e10cSrcweir 		delete pSotData->pDataFlavorList;
131cdf0e10cSrcweir 		pSotData->pDataFlavorList = NULL;
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir     //delete pSOTDATA();
134cdf0e10cSrcweir     //SOTDATA() = NULL;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir /************** class SotFactory *****************************************/
139cdf0e10cSrcweir /*************************************************************************
140cdf0e10cSrcweir |*    SotFactory::SotFactory()
141cdf0e10cSrcweir |*
142cdf0e10cSrcweir |*    Beschreibung
143cdf0e10cSrcweir *************************************************************************/
144cdf0e10cSrcweir TYPEINIT0(SotFactory);
145cdf0e10cSrcweir 
SotFactory(const SvGlobalName & rName,const String & rClassName,CreateInstanceType pCreateFuncP)146cdf0e10cSrcweir SotFactory::SotFactory( const SvGlobalName & rName,
147cdf0e10cSrcweir                       const String & rClassName,
148cdf0e10cSrcweir                       CreateInstanceType pCreateFuncP )
149cdf0e10cSrcweir     : SvGlobalName  ( rName )
150cdf0e10cSrcweir     , nSuperCount   ( 0 )
151cdf0e10cSrcweir     , pSuperClasses ( NULL )
152cdf0e10cSrcweir     , pCreateFunc   ( pCreateFuncP )
153cdf0e10cSrcweir     , aClassName    ( rClassName )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir #ifdef DBG_UTIL
156cdf0e10cSrcweir     SvGlobalName aEmptyName;
157cdf0e10cSrcweir     if( aEmptyName != *this )
158cdf0e10cSrcweir     { // wegen Sfx-BasicFactories
159cdf0e10cSrcweir     DBG_ASSERT( aEmptyName != *this, "create factory without SvGlobalName" );
160cdf0e10cSrcweir     if( Find( *this ) )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir 		/*
163cdf0e10cSrcweir         String aStr( GetClassName() );
164cdf0e10cSrcweir         aStr += ", UniqueName: ";
165cdf0e10cSrcweir         aStr += GetHexName();
166cdf0e10cSrcweir         aStr += ", create factories with the same unique name";
167cdf0e10cSrcweir         DBG_ERROR( aStr );
168cdf0e10cSrcweir 		*/
169cdf0e10cSrcweir         DBG_ERROR( "create factories with the same unique name" );
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir #endif
173cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
174cdf0e10cSrcweir     if( !pSotData->pFactoryList )
175cdf0e10cSrcweir         pSotData->pFactoryList = new SotFactoryList();
176cdf0e10cSrcweir     // muss nach hinten, wegen Reihenfolge beim zerstoeren
177cdf0e10cSrcweir     pSotData->pFactoryList->Insert( this, LIST_APPEND );
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //=========================================================================
~SotFactory()182cdf0e10cSrcweir SotFactory::~SotFactory()
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     delete [] pSuperClasses;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 
188cdf0e10cSrcweir /*************************************************************************
189cdf0e10cSrcweir |*    SotFactory::
190cdf0e10cSrcweir |*
191cdf0e10cSrcweir |*    Beschreibung      Zugriffsmethoden auf SotData_Impl-Daten
192cdf0e10cSrcweir *************************************************************************/
GetSvObjectCount()193cdf0e10cSrcweir sal_uInt32 SotFactory::GetSvObjectCount()
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     return SOTDATA()->nSvObjCount;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
GetFactoryList()199cdf0e10cSrcweir const SotFactoryList * SotFactory::GetFactoryList()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     return SOTDATA()->pFactoryList;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir /*************************************************************************
205cdf0e10cSrcweir |*    SotFactory::Find()
206cdf0e10cSrcweir |*
207cdf0e10cSrcweir |*    Beschreibung
208cdf0e10cSrcweir *************************************************************************/
Find(const SvGlobalName & rFactName)209cdf0e10cSrcweir const SotFactory* SotFactory::Find( const SvGlobalName & rFactName )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     SvGlobalName aEmpty;
212cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
213cdf0e10cSrcweir     if( rFactName != aEmpty && pSotData->pFactoryList )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         SotFactory * pFact = pSotData->pFactoryList->First();
216cdf0e10cSrcweir         while( pFact )
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             if( *pFact == rFactName )
219cdf0e10cSrcweir                 return pFact;
220cdf0e10cSrcweir             pFact = pSotData->pFactoryList->Next();
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	return 0;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir /*************************************************************************
228cdf0e10cSrcweir |*    SotFactory::PutSuperClass()
229cdf0e10cSrcweir |*
230cdf0e10cSrcweir |*    Beschreibung
231cdf0e10cSrcweir *************************************************************************/
PutSuperClass(const SotFactory * pFact)232cdf0e10cSrcweir void SotFactory::PutSuperClass( const SotFactory * pFact )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir     nSuperCount++;
235cdf0e10cSrcweir     if( !pSuperClasses )
236cdf0e10cSrcweir         pSuperClasses = new const SotFactory * [ nSuperCount ];
237cdf0e10cSrcweir     else
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         const SotFactory ** pTmp = new const SotFactory * [ nSuperCount ];
240cdf0e10cSrcweir         memcpy( (void *)pTmp, (void *)pSuperClasses,
241cdf0e10cSrcweir                 sizeof( void * ) * (nSuperCount -1) );
242cdf0e10cSrcweir         delete [] pSuperClasses;
243cdf0e10cSrcweir         pSuperClasses = pTmp;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir     pSuperClasses[ nSuperCount -1 ] = pFact;
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 
249cdf0e10cSrcweir /*************************************************************************
250cdf0e10cSrcweir |*    SotFactory::IncSvObjectCount()
251cdf0e10cSrcweir |*
252cdf0e10cSrcweir |*    Beschreibung
253cdf0e10cSrcweir *************************************************************************/
IncSvObjectCount(SotObject * pObj)254cdf0e10cSrcweir void SotFactory::IncSvObjectCount( SotObject * pObj )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
257cdf0e10cSrcweir     pSotData->nSvObjCount++;
258cdf0e10cSrcweir     if( !pSotData->pObjectList )
259cdf0e10cSrcweir         pSotData->pObjectList = new SotObjectList();
260cdf0e10cSrcweir     if( pObj )
261cdf0e10cSrcweir         pSotData->pObjectList->Insert( pObj );
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 
265cdf0e10cSrcweir /*************************************************************************
266cdf0e10cSrcweir |*    SotFactory::DecSvObjectCount()
267cdf0e10cSrcweir |*
268cdf0e10cSrcweir |*    Beschreibung
269cdf0e10cSrcweir *************************************************************************/
DecSvObjectCount(SotObject * pObj)270cdf0e10cSrcweir void SotFactory::DecSvObjectCount( SotObject * pObj )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
273cdf0e10cSrcweir     pSotData->nSvObjCount--;
274cdf0e10cSrcweir     if( pObj )
275cdf0e10cSrcweir         pSotData->pObjectList->Remove( pObj );
276cdf0e10cSrcweir     if( !pSotData->nSvObjCount )
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         //keine internen und externen Referenzen mehr
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 
283cdf0e10cSrcweir /*************************************************************************
284cdf0e10cSrcweir |*    SotFactory::TestInvariant()
285cdf0e10cSrcweir |*
286cdf0e10cSrcweir |*    Beschreibung
287cdf0e10cSrcweir *************************************************************************/
TestInvariant()288cdf0e10cSrcweir void SotFactory::TestInvariant()
289cdf0e10cSrcweir {
290cdf0e10cSrcweir #ifdef TEST_INVARIANT
291cdf0e10cSrcweir     SotData_Impl * pSotData = SOTDATA();
292cdf0e10cSrcweir     if( pSotData->pObjectList )
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         sal_uLong nCount = pSotData->pObjectList->Count();
295cdf0e10cSrcweir         for( sal_uLong i = 0; i < nCount ; i++ )
296cdf0e10cSrcweir         {
297cdf0e10cSrcweir             pSotData->pObjectList->GetObject( i )->TestInvariant( sal_False );
298cdf0e10cSrcweir         }
299cdf0e10cSrcweir     }
300cdf0e10cSrcweir #endif
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir /*************************************************************************
304cdf0e10cSrcweir |*    SotFactory::CreateInstance()
305cdf0e10cSrcweir |*
306cdf0e10cSrcweir |*    Beschreibung
307cdf0e10cSrcweir *************************************************************************/
CreateInstance(SotObject ** ppObj) const308cdf0e10cSrcweir void * SotFactory::CreateInstance( SotObject ** ppObj ) const
309cdf0e10cSrcweir {
310cdf0e10cSrcweir     DBG_ASSERT( pCreateFunc, "SotFactory::CreateInstance: pCreateFunc == 0" );
311cdf0e10cSrcweir     return pCreateFunc( ppObj );
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir //=========================================================================
CastAndAddRef(SotObject * pObj) const315cdf0e10cSrcweir void * SotFactory::CastAndAddRef
316cdf0e10cSrcweir (
317cdf0e10cSrcweir     SotObject * pObj /* Das Objekt von dem der Typ gepr"uft wird. */
318cdf0e10cSrcweir ) const
319cdf0e10cSrcweir /*  [Beschreibung]
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     Ist eine Optimierung, damit die Ref-Klassen k"urzer implementiert
322cdf0e10cSrcweir     werden k"onnen. pObj wird auf den Typ der Factory gecastet.
323cdf0e10cSrcweir     In c++ (wenn es immer erlaubt w"are) w"urde der void * wie im
324cdf0e10cSrcweir     Beispiel gebildet.
325cdf0e10cSrcweir     Factory der Klasse SvPersist.
326cdf0e10cSrcweir     void * p = (void *)(SvPersist *)pObj;
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     [R"uckgabewert]
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     void *,     NULL, pObj war NULL oder das Objekt war nicht vom Typ
331cdf0e10cSrcweir                 der Factory.
332cdf0e10cSrcweir                 Ansonsten wird pObj zuerst auf den Typ der Factory
333cdf0e10cSrcweir                 gecastet und dann auf void *.
334cdf0e10cSrcweir 
335cdf0e10cSrcweir     [Querverweise]
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     <SotObject::CastAndAddRef>
338cdf0e10cSrcweir */
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     return pObj ? pObj->CastAndAddRef( this ) : NULL;
341cdf0e10cSrcweir }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir /*************************************************************************
344cdf0e10cSrcweir |*    SotFactory::Is()
345cdf0e10cSrcweir |*
346cdf0e10cSrcweir |*    Beschreibung
347cdf0e10cSrcweir *************************************************************************/
Is(const SotFactory * pSuperCl) const348cdf0e10cSrcweir sal_Bool SotFactory::Is( const SotFactory * pSuperCl ) const
349cdf0e10cSrcweir {
350cdf0e10cSrcweir     if( this == pSuperCl )
351cdf0e10cSrcweir         return sal_True;
352cdf0e10cSrcweir 
353cdf0e10cSrcweir     for( sal_uInt16 i = 0; i < nSuperCount; i++ )
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         if( pSuperClasses[ i ]->Is( pSuperCl ) )
356cdf0e10cSrcweir             return sal_True;
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir     return sal_False;
359cdf0e10cSrcweir }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 
364