bump product version to 4.2.0.1
[LibreOffice.git] / sot / source / base / factory.cxx
blob0cd94cd97439da8c8613699cf1a0edf4f54c0581
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 <sot/factory.hxx>
21 #include <tools/debug.hxx>
22 #include <sot/object.hxx>
23 #include <sot/sotdata.hxx>
24 #include <comphelper/classids.hxx>
25 #include <rtl/instance.hxx>
26 #include <rtl/strbuf.hxx>
28 /************** class SotData_Impl *********************************************/
29 /*************************************************************************
30 |* SotData_Impl::SotData_Impl
32 |* Beschreibung
33 *************************************************************************/
34 SotData_Impl::SotData_Impl()
35 : nSvObjCount( 0 )
36 , pFactoryList( NULL )
37 , pSotObjectFactory( NULL )
38 , pSotStorageStreamFactory( NULL )
39 , pSotStorageFactory( NULL )
40 , pDataFlavorList( NULL )
44 SotData_Impl::~SotData_Impl()
46 if (pDataFlavorList)
48 for( tDataFlavorList::iterator aI = pDataFlavorList->begin(),
49 aEnd = pDataFlavorList->end(); aI != aEnd; ++aI)
51 delete *aI;
53 delete pDataFlavorList;
55 delete pFactoryList;
58 /*************************************************************************
59 |* SOTDATA()
61 |* Beschreibung
62 *************************************************************************/
63 namespace { struct ImplData : public rtl::Static<SotData_Impl, ImplData> {}; }
64 SotData_Impl * SOTDATA()
66 return &ImplData::get();
69 /************** class SotFactory *****************************************/
70 /*************************************************************************
71 |* SotFactory::SotFactory()
73 |* Beschreibung
74 *************************************************************************/
75 TYPEINIT0(SotFactory);
77 SotFactory::SotFactory( const SvGlobalName & rName,
78 const OUString & rClassName,
79 CreateInstanceType pCreateFuncP )
80 : SvGlobalName ( rName )
81 , nSuperCount ( 0 )
82 , pSuperClasses ( NULL )
83 , pCreateFunc ( pCreateFuncP )
84 , aClassName ( rClassName )
86 #ifdef DBG_UTIL
87 SvGlobalName aEmptyName;
88 if( aEmptyName != *this )
89 { // wegen Sfx-BasicFactories
90 DBG_ASSERT( aEmptyName != *this, "create factory without SvGlobalName" );
91 if( Find( *this ) )
93 OSL_FAIL( "create factories with the same unique name" );
96 #endif
97 SotData_Impl * pSotData = SOTDATA();
98 if( !pSotData->pFactoryList )
99 pSotData->pFactoryList = new SotFactoryList();
100 // muss nach hinten, wegen Reihenfolge beim zerstoeren
101 pSotData->pFactoryList->push_back( this );
105 //=========================================================================
106 SotFactory::~SotFactory()
108 delete [] pSuperClasses;
111 /*************************************************************************
112 |* SotFactory::Find()
114 |* Beschreibung
115 *************************************************************************/
116 const SotFactory* SotFactory::Find( const SvGlobalName & rFactName )
118 SvGlobalName aEmpty;
119 SotData_Impl * pSotData = SOTDATA();
120 if( rFactName != aEmpty && pSotData->pFactoryList )
122 for ( size_t i = 0, n = pSotData->pFactoryList->size(); i < n; ++i ) {
123 SotFactory* pFact = (*pSotData->pFactoryList)[ i ];
124 if( *pFact == rFactName )
125 return pFact;
129 return 0;
132 /*************************************************************************
133 |* SotFactory::PutSuperClass()
135 |* Beschreibung
136 *************************************************************************/
137 void SotFactory::PutSuperClass( const SotFactory * pFact )
139 nSuperCount++;
140 if( !pSuperClasses )
141 pSuperClasses = new const SotFactory * [ nSuperCount ];
142 else
144 const SotFactory ** pTmp = new const SotFactory * [ nSuperCount ];
145 memcpy( (void *)pTmp, (void *)pSuperClasses,
146 sizeof( void * ) * (nSuperCount -1) );
147 delete [] pSuperClasses;
148 pSuperClasses = pTmp;
150 pSuperClasses[ nSuperCount -1 ] = pFact;
154 /*************************************************************************
155 |* SotFactory::IncSvObjectCount()
157 |* Beschreibung
158 *************************************************************************/
159 void SotFactory::IncSvObjectCount( SotObject * pObj )
161 SotData_Impl * pSotData = SOTDATA();
162 pSotData->nSvObjCount++;
164 if( pObj )
165 pSotData->aObjectList.push_back( pObj );
169 /*************************************************************************
170 |* SotFactory::DecSvObjectCount()
172 |* Beschreibung
173 *************************************************************************/
174 void SotFactory::DecSvObjectCount( SotObject * pObj )
176 SotData_Impl * pSotData = SOTDATA();
177 pSotData->nSvObjCount--;
178 if( pObj )
179 pSotData->aObjectList.remove( pObj );
180 if( !pSotData->nSvObjCount )
182 //keine internen und externen Referenzen mehr
186 /*************************************************************************
187 |* SotFactory::CreateInstance()
189 |* Beschreibung
190 *************************************************************************/
191 void * SotFactory::CreateInstance( SotObject ** ppObj ) const
193 DBG_ASSERT( pCreateFunc, "SotFactory::CreateInstance: pCreateFunc == 0" );
194 return pCreateFunc( ppObj );
197 /*************************************************************************
198 |* SotFactory::Is()
200 |* Beschreibung
201 *************************************************************************/
202 bool SotFactory::Is( const SotFactory * pSuperCl ) const
204 if( this == pSuperCl )
205 return true;
207 for( sal_uInt16 i = 0; i < nSuperCount; i++ )
209 if( pSuperClasses[ i ]->Is( pSuperCl ) )
210 return true;
212 return false;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */