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 #ifndef FORMS_MODULE_IMPLEMENTATION_INCLUDE_CONTEXT
23     #error "not to be included directly! use 'foo_module.hxx instead'!"
24 #endif
25 
26 #ifndef FORMS_MODULE_NAMESPACE
27     #error "set FORMS_MODULE_NAMESPACE to your namespace identifier!"
28 #endif
29 
30 #include <comphelper/sequence.hxx>
31 
32 //.........................................................................
33 namespace FORMS_MODULE_NAMESPACE
34 {
35 //.........................................................................
36 
37 	using namespace ::com::sun::star::uno;
38 	using namespace ::com::sun::star::lang;
39 	using namespace ::com::sun::star::registry;
40 	using namespace ::comphelper;
41 	using namespace ::cppu;
42 
43 	//=========================================================================
44 	//= OFormsModule
45 	//=========================================================================
46 
47 	//--------------------------------------------------------------------------
48 	//- registration helper
49 	//--------------------------------------------------------------------------
50 
51 	Sequence< ::rtl::OUString >*				OFormsModule::s_pImplementationNames = NULL;
52 	Sequence< Sequence< ::rtl::OUString > >*	OFormsModule::s_pSupportedServices = NULL;
53 	Sequence< sal_Int64 >*						OFormsModule::s_pCreationFunctionPointers = NULL;
54 	Sequence< sal_Int64 >*						OFormsModule::s_pFactoryFunctionPointers = NULL;
55 
56 	//--------------------------------------------------------------------------
registerComponent(const::rtl::OUString & _rImplementationName,const Sequence<::rtl::OUString> & _rServiceNames,ComponentInstantiation _pCreateFunction,FactoryInstantiation _pFactoryFunction)57 	void OFormsModule::registerComponent(
58 		const ::rtl::OUString& _rImplementationName,
59 		const Sequence< ::rtl::OUString >& _rServiceNames,
60 		ComponentInstantiation _pCreateFunction,
61 		FactoryInstantiation _pFactoryFunction)
62 	{
63 		if (!s_pImplementationNames)
64 		{
65 			OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
66 				"OFormsModule::registerComponent : inconsistent state (the pointers (1)) !");
67 			s_pImplementationNames = new Sequence< ::rtl::OUString >;
68 			s_pSupportedServices = new Sequence< Sequence< ::rtl::OUString > >;
69 			s_pCreationFunctionPointers = new Sequence< sal_Int64 >;
70 			s_pFactoryFunctionPointers = new Sequence< sal_Int64 >;
71 		}
72 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
73 			"OFormsModule::registerComponent : inconsistent state (the pointers (2)) !");
74 
75 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
76 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
77 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
78 			"OFormsModule::registerComponent : inconsistent state !");
79 
80 		sal_Int32 nOldLen = s_pImplementationNames->getLength();
81 		s_pImplementationNames->realloc(nOldLen + 1);
82 		s_pSupportedServices->realloc(nOldLen + 1);
83 		s_pCreationFunctionPointers->realloc(nOldLen + 1);
84 		s_pFactoryFunctionPointers->realloc(nOldLen + 1);
85 
86 		s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
87 		s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
88 		s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
89 		s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
90 	}
91 
92 	//--------------------------------------------------------------------------
revokeComponent(const::rtl::OUString & _rImplementationName)93 	void OFormsModule::revokeComponent(const ::rtl::OUString& _rImplementationName)
94 	{
95 		if (!s_pImplementationNames)
96 		{
97 			OSL_ASSERT("OFormsModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
98 			return;
99 		}
100 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
101 			"OFormsModule::revokeComponent : inconsistent state (the pointers) !");
102 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
103 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
104 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
105 			"OFormsModule::revokeComponent : inconsistent state !");
106 
107 		sal_Int32 nLen = s_pImplementationNames->getLength();
108 		const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
109 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
110 		{
111 			if (pImplNames->equals(_rImplementationName))
112 			{
113 				removeElementAt(*s_pImplementationNames, i);
114 				removeElementAt(*s_pSupportedServices, i);
115 				removeElementAt(*s_pCreationFunctionPointers, i);
116 				removeElementAt(*s_pFactoryFunctionPointers, i);
117 				break;
118 			}
119 		}
120 
121 		if (s_pImplementationNames->getLength() == 0)
122 		{
123 			delete s_pImplementationNames; s_pImplementationNames = NULL;
124 			delete s_pSupportedServices; s_pSupportedServices = NULL;
125 			delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
126 			delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
127 		}
128 	}
129 
130 	//--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const Reference<XMultiServiceFactory> & _rxServiceManager)131 	Reference< XInterface > OFormsModule::getComponentFactory(
132 		const ::rtl::OUString& _rImplementationName,
133 		const Reference< XMultiServiceFactory >& _rxServiceManager)
134 	{
135 		OSL_ENSURE(_rxServiceManager.is(), "OFormsModule::getComponentFactory : invalid argument (service manager) !");
136 		OSL_ENSURE(_rImplementationName.getLength(), "OFormsModule::getComponentFactory : invalid argument (implementation name) !");
137 
138 		if (!s_pImplementationNames)
139 		{
140 			OSL_ASSERT("OFormsModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
141 			return NULL;
142 		}
143 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
144 			"OFormsModule::getComponentFactory : inconsistent state (the pointers) !");
145 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
146 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
147 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
148 			"OFormsModule::getComponentFactory : inconsistent state !");
149 
150 
151 		Reference< XInterface > xReturn;
152 
153 
154 		sal_Int32 nLen = s_pImplementationNames->getLength();
155 		const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
156 		const Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
157 		const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
158 		const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
159 
160 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
161 		{
162 			if (pImplName->equals(_rImplementationName))
163 			{
164 				const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
165 				const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
166 
167 				xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
168 				if (xReturn.is())
169 				{
170 					xReturn->acquire();
171 					return xReturn.get();
172 				}
173 			}
174 		}
175 
176 		return NULL;
177 	}
178 
179 //.........................................................................
180 }   // namespace FORMS_MODULE_NAMESPACE
181 //.........................................................................
182 
183