blob: 3dbb1f534e9d0d6f107157ab86e6906b6487bf43 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
#define INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
#include <comphelper/comphelperdllapi.h>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/frame/XUntitledNumbers.hpp>
#include <cppuhelper/weakref.hxx>
#include <cppuhelper/implbase.hxx>
#include <unordered_map>
#include <mutex>
#include <vector>
namespace com::sun::star::uno { class XInterface; }
namespace comphelper{
/** @short defines a collection of UNO components, where every component will get its own unique number.
@descr Such number will be unique at runtime only... but it supports fragmentation.
Note: This collection uses weak references only to know her components.
So lifetime of these components must be controlled outside.
@threadsafe
*/
class COMPHELPER_DLLPUBLIC NumberedCollection final :
public ::cppu::WeakImplHelper< css::frame::XUntitledNumbers >
{
// types, const
private:
struct TNumberedItem
{
css::uno::WeakReference< css::uno::XInterface > xItem;
::sal_Int32 nNumber;
};
typedef std::unordered_map<
sal_IntPtr,
TNumberedItem > TNumberedItemHash;
typedef ::std::vector< sal_IntPtr > TDeadItemList;
// interface
public:
/** @short lightweight constructor.
*/
NumberedCollection();
/** @short free all internally used resources.
*/
virtual ~NumberedCollection() override;
/** set an outside component which uses this container and must be set
as source of all broadcasted messages, exceptions.
It's holded weak only so we do not need any complex dispose sessions.
Note: Passing NULL as parameter will be allowed. It will reset the internal
member reference only.
@param xOwner
the new owner of this collection.
*/
void setOwner (const css::uno::Reference< css::uno::XInterface >& xOwner);
/** set the localized prefix to be used for untitled components.
Localization has to be done outside. This container will return
those value then. There are no further checks. Its up to you to define
a suitable string here :-)
@param sPrefix
the new prefix for untitled components.
*/
void setUntitledPrefix(const OUString& sPrefix);
/** @see css.frame.XUntitledNumbers */
virtual ::sal_Int32 SAL_CALL leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent) override;
/** @see css.frame.XUntitledNumbers */
virtual void SAL_CALL releaseNumber(::sal_Int32 nNumber) override;
/** @see css.frame.XUntitledNumbers */
virtual void SAL_CALL releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent) override;
/** @see css.frame.XUntitledNumbers */
virtual OUString SAL_CALL getUntitledPrefix() override;
// internal
private:
/** @short tries to find a unique number not already used within this collection.
@descr It reuses the smallest number which isn't used by any component
of this collection. (fragmentation!) If collection is full (means there
is no free number) the special value INVALID_NUMBER will be returned.
@note Those method can't be called within a multithreaded environment.
Because such number won't be "reserved" for the call of these method
it can happen that two calls returns the same number (reasoned by the fact that first call
doesn't used the returned number already.
So the outside code has to make sure that retrieving and using of those numbers
will be an atomic operation.
@return a unique number or special value INVALID_NUMBER if collection is full.
*/
::sal_Int32 impl_searchFreeNumber ();
static void impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
const TDeadItemList& lDeadItems);
// member
private:
/// localized string to be used for untitled components
OUString m_sUntitledPrefix;
/// cache of all "leased numbers" and its bound components
TNumberedItemHash m_lComponents;
/// used as source of broadcasted messages or exceptions (can be null !)
css::uno::WeakReference< css::uno::XInterface > m_xOwner;
std::mutex m_aMutex;
};
} // namespace comphelper
#endif // INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|