xref: /trunk/main/unotools/inc/unotools/idhelper.hxx (revision 86e1cf34)
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 
23 
24 #ifndef _UNOTOOLS_ID_HELPER_HXX_
25 #define _UNOTOOLS_ID_HELPER_HXX_
26 
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <com/sun/star/lang/XTypeProvider.hpp>
29 #include <osl/mutex.hxx>
30 #include <comphelper/stl_types.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <tools/debug.hxx>
33 
34 //.........................................................................
35 namespace utl
36 {
37 //.........................................................................
38 
39 //=========================================================================
40 // to shorten some lines ...
41 typedef ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type >	TypeSequence;
42 
43 // compare to Sequences of Types
44 struct TypeSequenceLess : public ::std::binary_function<TypeSequence, TypeSequence, bool>
45 {
46 public:
operator ()utl::TypeSequenceLess47 	inline bool operator() (const TypeSequence& lhs, const TypeSequence& rhs) const
48 	{
49 		sal_Int32 nLengthLeft = lhs.getLength();
50 		sal_Int32 nLengthRight = rhs.getLength();
51 
52 		// first check the two lengths
53 		if (nLengthLeft < nLengthRight)
54 			return sal_True;
55 		if (nLengthLeft > nLengthRight)
56 			return sal_False;
57 
58 		// both sequences have the same length -> check the type names
59 		const ::com::sun::star::uno::Type* pTypesLeft = lhs.getConstArray();
60 		const ::com::sun::star::uno::Type* pTypesRight = rhs.getConstArray();
61 		for (sal_Int32 i=0; i<nLengthLeft; ++i, ++pTypesLeft, ++pTypesRight)
62 		{
63 			sal_Int32 nTypeNameCompare = pTypesLeft->getTypeName().compareTo(pTypesRight->getTypeName());
64 			if (nTypeNameCompare < 0)
65 				return sal_True;
66 			if (nTypeNameCompare > 0)
67 				return sal_False;
68 		}
69 
70 		// both sequences are equal ...
71 		return sal_False;
72 	}
73 };
74 
75 // declare the map
76 DECLARE_STL_MAP	(	TypeSequence,
77 					::cppu::OImplementationId,
78 					TypeSequenceLess,
79 					MapType2Id
80 				);
81 
82 //.........................................................................
83 }	// namespace utl
84 //.........................................................................
85 
86 //=========================================================================
87 /** defines a helper class for implementing the XTypeProvider::getImplementationId.
88 	it maps sequences of ::com::sun::star::uno::Type to implementation ids
89 	(which means sequences of bytes).<BR>
90 	As there is no possibility to determine the time where the id's are no longer
91 	needed (e.g. because the last instance of the class using this mechanism died)
92 	the helper is "refcounted", i.e. there are acquire and release methods.
93 	To simplify this there is a class classname##Ref which you may want to
94 	use as an member of your classes.
95 	<BR><BR>
96 	As we don't want a global helper class which handles implementation id's
97 	of components from all over the office (supposing somebody want's to use this :)
98 	this is only a define. Wherever you have a "closed" area (which is small enough
99 	and large enough :), see below) where diffenrent components want to use an id helper,
100 	define your own one with this macro.<BR>
101 	The more classes use this helper, the later redundant map entries will be
102 	cleared. The less classes use it, the earlier map entries which may have
103 	been reused will be cleared.
104 */
105 #define DECLARE_IMPLEMENTATIONID_HELPER(_namespace, classname)		\
106 namespace _namespace {												\
107 class classname														\
108 {																	\
109 	friend class classname##Ref;								\
110 																	\
111 	static sal_Int32	s_nReferenced;								\
112 	static void*		s_pMap;										\
113 																	\
114 	static ::osl::Mutex	s_aMutex;									\
115 																	\
116 public:																\
117 	static void acquire();											\
118 	static void release();											\
119 																	\
120 	static ::com::sun::star::uno::Sequence< sal_Int8 > getImplementationId(	\
121 		const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type >& _rTypes);	\
122 																	\
123 private:															\
124 	static void implCreateMap();									\
125 																	\
126 	classname() { }													\
127 };																	\
128 																	\
129 /*=======================================================================*/	\
130 class classname##Ref												\
131 {																	\
132 public:																\
133 	classname##Ref() { classname::acquire(); }	\
134 	~classname##Ref() { classname::release(); }	\
135 };																	\
136 																	\
137 }	/* _namespace */												\
138 																	\
139 
140 /*************************************************************************
141 **************************************************************************
142 *************************************************************************/
143 
144 /** implement an id helper
145 */
146 #define IMPLEMENT_IMPLEMENTATIONID_HELPER(_namespace, classname)		\
147 namespace _namespace {	\
148 	\
149 /*=======================================================================*/	\
150 	\
151 sal_Int32		classname::s_nReferenced(0);	\
152 void*			classname::s_pMap = NULL;	\
153 ::osl::Mutex	classname::s_aMutex;	\
154 	\
155 /*-----------------------------------------------------------------------*/	\
156 void classname::acquire()	\
157 {	\
158 	::osl::MutexGuard aGuard(s_aMutex);	\
159 	++s_nReferenced;	\
160 }	\
161 	\
162 /*-----------------------------------------------------------------------*/	\
163 void classname::release()	\
164 {	\
165 	::osl::MutexGuard aGuard(s_aMutex);	\
166 	if (!--s_nReferenced)	\
167 	{	\
168 		delete static_cast< ::utl::MapType2Id *>( s_pMap );	\
169 		s_pMap = NULL;	\
170 	}	\
171 }	\
172 	\
173 /*-----------------------------------------------------------------------*/	\
174 ::com::sun::star::uno::Sequence< sal_Int8 > classname::getImplementationId(	\
175 		const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type >& _rTypes)	\
176 {	\
177 	::osl::MutexGuard aGuard(s_aMutex);	\
178 	DBG_ASSERT(s_nReferenced,	\
179 		"classname::getImplementationId : you need to hold a reference to this class in order to use it !");	\
180 		/* give the calling class a member of type classname##Ref and all is fine .... */	\
181 	\
182 	implCreateMap();	\
183 	\
184 	::utl::MapType2Id* pMap = static_cast< ::utl::MapType2Id *>(s_pMap);	\
185 	\
186 	::cppu::OImplementationId& rId = (*pMap)[_rTypes];	\
187 	/* this will create an entry for the given type sequence, if necessary */	\
188 	\
189 	return rId.getImplementationId();	\
190 }	\
191 	\
192 /*-----------------------------------------------------------------------*/	\
193 void classname::implCreateMap()	\
194 {	\
195 	if (s_pMap)	\
196 		return;	\
197 	s_pMap = new ::utl::MapType2Id();	\
198 }	\
199 	\
200 	\
201 }	/* _namespace */	\
202 	\
203 
204 
205 #endif // _UNOTOOLS_ID_HELPER_HXX_
206 
207