1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
21 #define INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
23 #include <comphelper/comphelperdllapi.h>
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>
30 #include <cppuhelper/basemutex.hxx>
31 #include <cppuhelper/weakref.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <boost/functional/hash.hpp>
34 #include <unordered_map>
40 /** @short defines a collection of UNO components, where every component will get its own unique number.
42 @descr Such number will be unique at runtime only ... but it supports fragmentation.
43 Note: This collection uses weak references only to know her components.
44 So lifetime of thise components must be controlled outside.
48 class COMPHELPER_DLLPUBLIC NumberedCollection
: private ::cppu::BaseMutex
49 , public ::cppu::WeakImplHelper1
< css::frame::XUntitledNumbers
>
57 css::uno::WeakReference
< css::uno::XInterface
> xItem
;
61 typedef std::unordered_map
<
64 ::boost::hash
< long > ,
65 ::std::equal_to
< long > > TNumberedItemHash
;
67 typedef ::std::vector
< long > TDeadItemList
;
74 /** @short lightweight constructor.
79 /** @short free all internally used resources.
81 virtual ~NumberedCollection();
84 /** set an outside component which uses this container and must be set
85 as source of all broadcasted messages, exceptions.
87 It's holded weak only so we do not need any complex dispose sessions.
89 Note: Passing NULL as parameter will be allowed. It will reset the internal
90 member reference only.
93 the new owner of this collection.
95 void setOwner (const css::uno::Reference
< css::uno::XInterface
>& xOwner
);
98 /** set the localized prefix to be used for untitled components.
100 Localization has to be done outside. This container will return
101 those value then. There are no further checks. Its up to you to define
102 a suitable string here :-)
105 the new prefix for untitled components.
107 void setUntitledPrefix(const OUString
& sPrefix
);
110 /** @see css.frame.XUntitledNumbers */
111 virtual ::sal_Int32 SAL_CALL
leaseNumber(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
112 throw (css::lang::IllegalArgumentException
,
113 css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
116 /** @see css.frame.XUntitledNumbers */
117 virtual void SAL_CALL
releaseNumber(::sal_Int32 nNumber
)
118 throw (css::lang::IllegalArgumentException
,
119 css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
122 /** @see css.frame.XUntitledNumbers */
123 virtual void SAL_CALL
releaseNumberForComponent(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
124 throw (css::lang::IllegalArgumentException
,
125 css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
128 /** @see css.frame.XUntitledNumbers */
129 virtual OUString SAL_CALL
getUntitledPrefix()
130 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
137 /** @short trys to find an unique number not already used within this collection.
139 @descr It reuses the smalles number which isn't used by any component
140 of this collection. (fragmentation!) If collection is full (means there
141 is no free number) the special value INVALID_NUMBER will be returned.
143 @note Those method can't be called within a multithreaded environment ..
144 Because such number wont be "reserved" for the calli of these method
145 it can happen that two calls returns the same number (reasoned by the fact that first calli
146 doesn't used the returned number already.
148 So the outside code has to make sure that retrieving and using of those number
149 will be an atomic operation.
151 @return an unique number or special value INVALID_NUMBER if collection is full.
153 ::sal_Int32
impl_searchFreeNumber ();
155 static void impl_cleanUpDeadItems ( TNumberedItemHash
& lItems
,
156 const TDeadItemList
& lDeadItems
);
162 /// localized string to be used for untitled components
163 OUString m_sUntitledPrefix
;
165 /// cache of all "leased numbers" and its bound components
166 TNumberedItemHash m_lComponents
;
168 /// used as source of broadcasted messages or exceptions (can be null !)
169 css::uno::WeakReference
< css::uno::XInterface
> m_xOwner
;
172 } // namespace comphelper
174 #endif // INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */