xref: /trunk/main/filter/source/config/cache/basecontainer.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 __FILTER_CONFIG_BASECONTAINER_HXX_
23 #define __FILTER_CONFIG_BASECONTAINER_HXX_
24 
25 //_______________________________________________
26 // includes
27 
28 #include "filtercache.hxx"
29 #include <com/sun/star/uno/Exception.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/util/XRefreshable.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/container/XNameContainer.hpp>
34 #include <com/sun/star/container/XContainerQuery.hpp>
35 #include <com/sun/star/util/XFlushable.hpp>
36 #include <cppuhelper/interfacecontainer.h>
37 #include <salhelper/singletonref.hxx>
38 #include <cppuhelper/implbase4.hxx>
39 #include <cppuhelper/weakref.hxx>
40 #include <rtl/ustring.hxx>
41 
42 //_______________________________________________
43 // namespace
44 
45 namespace filter{
46     namespace config{
47 
48 //_______________________________________________
49 // definitions
50 
51 //_______________________________________________
52 
53 /** @short      implements the interface css::container::XNameContainer
54                 on top of a FilterCache reference.
55 
56     @descr      This class can be used as base for own service implementations,
57                 which must provide read/write access to the filter configuration.
58                 Parameters regulate read/write access, which sub set of information
59                 should be available etc.
60 
61     @attention  The base class BaseLock must be the first of declared ones.
62                 Otherwise we can't be sure, that our own mutex member (which is
63                 present by this base class!) was full initialized inside our own
64                 ctor as first!
65  */
66 class BaseContainer : public BaseLock
67                     , public ::cppu::WeakImplHelper4< css::lang::XServiceInfo         ,
68                                                       css::container::XNameContainer  , // => XNameReplace => XNameAccess => XElementAccess
69                                                       css::container::XContainerQuery ,
70                                                       css::util::XFlushable           >
71 {
72     //-------------------------------------------
73     // member
74 
75     protected:
76 
77         /** @short  reference to an uno service manager, which can be used
78                     to create own needed services. */
79         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
80 
81         // TODO
82         css::uno::WeakReference< css::util::XRefreshable > m_xRefreshBroadcaster;
83 
84         /** @short  the implementation name of our derived class, which we provide
85                     at the interface XServiceInfo of our class ... */
86         ::rtl::OUString m_sImplementationName;
87 
88         /** @short  the list of supported uno service names of our derived class, which we provide
89                     at the interface XServiceInfo of our class ... */
90         css::uno::Sequence< ::rtl::OUString > m_lServiceNames;
91 
92         /** @short  reference(!) to a singleton filter cache implementation,
93                     which is used to work with the underlying configuration. */
94         ::salhelper::SingletonRef< FilterCache > m_rCache;
95 
96         /** @short  local filter cache, which is used to collect changes on the
97                     filter configuration first and flush it later.
98 
99             @descr  Normally this member isn't used nor initialized. That's true,
100                     if this container is used for reading only. The first write access
101                     (e.g. by calling insertByName()) creates a copy of the current
102                     global cache m_rCache to initialize the m_pFlushCache member.
103 
104                     Afterwards only the flush cache copy is used. Inside flush() this
105                     copy will be removed and m_rCache can be used again.
106 
107                     m_pFlushCache and m_rCache must not be synchronized manually here.
108                     m_rCache listen on the global configuration, where m_pFlushCache
109                     write its data. m_rCache update itself automatically.
110          */
111         FilterCache* m_pFlushCache;
112 
113         /** @short  specify, which sub container of the used filter cache
114                     must be wrapped by this container interface. */
115         FilterCache::EItemType m_eType;
116 
117         /** @short  holds all listener, which are registered at this instance. */
118         ::cppu::OMultiTypeInterfaceContainerHelper m_lListener;
119 
120         /** @short  hold at least on filter cache instance alive and
121                     prevent he office from unloading this cache if no filter
122                     is currently used.*/
123         static ::salhelper::SingletonRef< FilterCache >* m_pPerformanceOptimizer;
124 
125     //-------------------------------------------
126     // native interface
127 
128     public:
129 
130         //---------------------------------------
131         // ctor/dtor
132 
133         /** @short  standard ctor.
134 
135             @descr  Because mostly this class is used as base class for own service
136                     implementations in combination with a ImplInheritanceHelper2 template ...
137                     there is no way to provide some initializing data through the ctor :-(
138                     This base class will be created inside its default ctor and must be
139                     initialized with its needed parameters explicitly by calling: "init()".
140 
141             @see    init()
142          */
143         BaseContainer();
144 
145         //---------------------------------------
146 
147         /** @short  standard dtor.
148          */
149         virtual ~BaseContainer();
150 
151         //---------------------------------------
152 
153         /** @short  initialize this generic instance with some specialized values
154                     from our derived object.
155 
156             @descr  Because an outside class must use ImplInheritanceHelper2 template to
157                     use us a base class ... and there is no way to pass such initializing
158                     parameters through a required default ctor ... we must be initialized
159                     by this special method. Of course this method must be called first before
160                     any other interface method is used.
161 
162             @param  xSMGR
163                     reference to the uno service manager, which created this service instance.
164 
165             @param  sImplementationName
166                     the implementation name of our derived class, which we provide
167                     at the interface XServiceInfo of our class ...
168 
169             @param  lServiceNames
170                     the list of supported uno service names of our derived class, which we provide
171                     at the interface XServiceInfo of our class ...
172 
173             @param  eType
174                     specify, which sub container of the used filter cache
175                     must be wrapped by this container interface.
176          */
177         virtual void init(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR              ,
178                           const ::rtl::OUString&                                        sImplementationName,
179                           const css::uno::Sequence< ::rtl::OUString >&                  lServiceNames      ,
180                                 FilterCache::EItemType                                  eType              );
181 
182     //-------------------------------------------
183     // helper
184 
185     protected:
186 
187         //---------------------------------------
188 
189         /** @short  check if the underlying configuration data was already loaded
190                     and do it if necessary automatically.
191          */
192         void impl_loadOnDemand();
193 
194         //---------------------------------------
195 
196         /** @short  it creates the global instance m_pFilterCache, which is a copy
197                     of the global instance m_rCache, and will be used to change the
198                     configuration.
199 
200             @descr  If no exception occurs, its guaranteed, that the member m_rFlushCache
201                     was initialized right and can be used further.
202          */
203         void impl_initFlushMode();
204 
205         //---------------------------------------
206 
207         /** @short  returns a pointer to the current used cache member.
208 
209             @descr  Its a point to the FilterCache instance behind m_pFlushCache
210                     or m_rCache.
211 
212             @note   The lifetime of this pointer is restricted to the time, where
213                     the mutex of this BaseContainer instance is locked.
214                     Otherwise maybe the interface method flush() will destroy
215                     m_pFlushCache and the here returned pointer will be invalid!
216 
217                     Use:
218 
219                         Guard aLock(m_aLock);
220                             FilterCache* p = impl_getWorkingCache();
221                             p->doSomething();
222                         aLock.clear();
223                         // after this point p can't b e guaranteed any longer!
224          */
225         FilterCache* impl_getWorkingCache() const;
226 
227     //-------------------------------------------
228     // uno interface
229 
230     public:
231 
232         //---------------------------------------
233         // XServiceInfo
234 
235         virtual ::rtl::OUString SAL_CALL getImplementationName();
236 
237         virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& sServiceName);
238 
239         virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames();
240 
241         //---------------------------------------
242         // XNameContainer
243 
244         virtual void SAL_CALL insertByName(const ::rtl::OUString& sItem ,
245                                            const css::uno::Any&   aValue);
246 
247         virtual void SAL_CALL removeByName(const ::rtl::OUString& sItem);
248 
249         //---------------------------------------
250         // XNameReplace
251 
252         virtual void SAL_CALL replaceByName(const ::rtl::OUString& sItem ,
253                                             const css::uno::Any&   aValue);
254 
255         //---------------------------------------
256         // XElementAccess
257 
258         virtual css::uno::Any SAL_CALL getByName(const ::rtl::OUString& sItem);
259 
260         virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames();
261 
262         virtual sal_Bool SAL_CALL hasByName(const ::rtl::OUString& sItem);
263 
264         virtual css::uno::Type SAL_CALL getElementType();
265 
266         virtual sal_Bool SAL_CALL hasElements();
267 
268         //---------------------------------------
269         // XContainerQuery
270 
271         // must be implemented really by derived class ...
272         // We implement return of an empty result here only!
273         // But we show an assertion :-)
274         virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByQuery(const ::rtl::OUString& sQuery);
275 
276         virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties);
277 
278         //---------------------------------------
279         // XFlushable
280 
281         virtual void SAL_CALL flush();
282 
283         virtual void SAL_CALL addFlushListener(const css::uno::Reference< css::util::XFlushListener >& xListener);
284 
285         virtual void SAL_CALL removeFlushListener(const css::uno::Reference< css::util::XFlushListener >& xListener);
286 };
287 
288     } // namespace config
289 } // namespace filter
290 
291 #endif // __FILTER_CONFIG_BASECONTAINER_HXX_
292 
293 /* vim: set noet sw=4 ts=4: */
294