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