Branch data 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 _COMPHELPER_NUMBEREDCOLLECTION_HXX_
21 : : #define _COMPHELPER_NUMBEREDCOLLECTION_HXX_
22 : :
23 : : #include "comphelper/comphelperdllapi.h"
24 : :
25 : : #include <com/sun/star/uno/Reference.hxx>
26 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 : : #include <com/sun/star/uno/XInterface.hpp>
28 : : #include <com/sun/star/frame/XUntitledNumbers.hpp>
29 : :
30 : : #include <cppuhelper/basemutex.hxx>
31 : : #include <cppuhelper/weakref.hxx>
32 : : #include <cppuhelper/implbase1.hxx>
33 : :
34 : : #include <vector>
35 : : #include <boost/unordered_map.hpp>
36 : :
37 : :
38 : : namespace comphelper{
39 : :
40 : : #ifdef css
41 : : #error "Ambigious namespace definition of css."
42 : : #else
43 : : #define css ::com::sun::star
44 : : #endif
45 : :
46 : :
47 : : /** @short defines a collection of UNO components, where every component will get it's own unique number.
48 : :
49 : : @descr Such number will be unique at runtime only ... but it supports fragmentation.
50 : : Note: This collection uses weak refrences only to know her components.
51 : : So lifetime of thise components must be controlled outside.
52 : :
53 : : @threadsafe
54 : : */
55 : : class COMPHELPER_DLLPUBLIC NumberedCollection : private ::cppu::BaseMutex
56 : : , public ::cppu::WeakImplHelper1< css::frame::XUntitledNumbers >
57 : : {
58 : : //-------------------------------------------
59 : : // types, const
60 : : private:
61 : :
62 : 14062 : struct TNumberedItem
63 : : {
64 : : css::uno::WeakReference< css::uno::XInterface > xItem;
65 : : ::sal_Int32 nNumber;
66 : : };
67 : :
68 : : typedef ::boost::unordered_map<
69 : : long ,
70 : : TNumberedItem ,
71 : : ::boost::hash< long > ,
72 : : ::std::equal_to< long > > TNumberedItemHash;
73 : :
74 : : typedef ::std::vector< long > TDeadItemList;
75 : :
76 : : //-------------------------------------------
77 : : // interface
78 : : public:
79 : :
80 : : //---------------------------------------
81 : : /** @short lightweight constructor.
82 : : */
83 : : NumberedCollection();
84 : :
85 : : //---------------------------------------
86 : : /** @short free all internaly used resources.
87 : : */
88 : : virtual ~NumberedCollection();
89 : :
90 : : //---------------------------------------
91 : : /** set an outside component which uses this container and must be set
92 : : as source of all broadcasted messages, exceptions.
93 : :
94 : : It's holded weak only so we do not need any complex dispose sessions.
95 : :
96 : : Note: Passing NULL as parameter will be allowed. It will reset the internal
97 : : member reference only.
98 : :
99 : : @param xOwner
100 : : the new owner of this collection.
101 : : */
102 : : void setOwner (const css::uno::Reference< css::uno::XInterface >& xOwner);
103 : :
104 : : //---------------------------------------
105 : : /** set the localized prefix to be used for untitled components.
106 : :
107 : : Localization has to be done outside. This container will return
108 : : those value then. There are no further checks. Its up to you to define
109 : : a suitable string here :-)
110 : :
111 : : @param sPrefix
112 : : the new prefix for untitled components.
113 : : */
114 : : void setUntitledPrefix(const ::rtl::OUString& sPrefix);
115 : :
116 : : //---------------------------------------
117 : : /** @see css.frame.XUntitledNumbers */
118 : : virtual ::sal_Int32 SAL_CALL leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
119 : : throw (css::lang::IllegalArgumentException,
120 : : css::uno::RuntimeException );
121 : :
122 : : //---------------------------------------
123 : : /** @see css.frame.XUntitledNumbers */
124 : : virtual void SAL_CALL releaseNumber(::sal_Int32 nNumber)
125 : : throw (css::lang::IllegalArgumentException,
126 : : css::uno::RuntimeException );
127 : :
128 : : //---------------------------------------
129 : : /** @see css.frame.XUntitledNumbers */
130 : : virtual void SAL_CALL releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
131 : : throw (css::lang::IllegalArgumentException,
132 : : css::uno::RuntimeException );
133 : :
134 : : //---------------------------------------
135 : : /** @see css.frame.XUntitledNumbers */
136 : : virtual ::rtl::OUString SAL_CALL getUntitledPrefix()
137 : : throw (css::uno::RuntimeException);
138 : :
139 : : //-------------------------------------------
140 : : // internal
141 : : private:
142 : :
143 : : //---------------------------------------
144 : : /** @short trys to find an unique number not already used within this collection.
145 : :
146 : : @descr It reuses the smalles number which isnt used by any component
147 : : of this collection. (fragmentation!) If collection is full (means there
148 : : is no free number) the special value INVALID_NUMBER will be returned.
149 : :
150 : : @note Those method cant be called within a multithreaded environment ..
151 : : Because such number wont be "reserved" for the calli of these method
152 : : it can happen that two calls returns the same number (reasoned by the fact that first calli
153 : : doesnt used the returned number already.
154 : :
155 : : So the outside code has to make sure that retrieving and using of those number
156 : : will be an atomic operation.
157 : :
158 : : @return an unique number or special value INVALID_NUMBER if collection is full.
159 : : */
160 : : ::sal_Int32 impl_searchFreeNumber ();
161 : :
162 : : void impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
163 : : const TDeadItemList& lDeadItems);
164 : :
165 : : //-------------------------------------------
166 : : // member
167 : : private:
168 : :
169 : : /// localized string to be used for untitled components
170 : : ::rtl::OUString m_sUntitledPrefix;
171 : :
172 : : /// cache of all "leased numbers" and its bound components
173 : : TNumberedItemHash m_lComponents;
174 : :
175 : : /// used as source of broadcasted messages or exceptions (can be null !)
176 : : css::uno::WeakReference< css::uno::XInterface > m_xOwner;
177 : : };
178 : :
179 : : #undef css
180 : :
181 : : } // namespace comphelper
182 : :
183 : : #endif // _COMPHELPER_NUMBEREDCOLLECTION_HXX_
184 : :
185 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|