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