2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-29 14:02:24 +01:00
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2013-10-23 19:17:00 +02:00
|
|
|
#ifndef INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
|
|
|
|
#define INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2013-11-09 13:59:36 -06:00
|
|
|
#include <comphelper/comphelperdllapi.h>
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2018-10-04 09:07:36 +02:00
|
|
|
#include <com/sun/star/uno/Reference.h>
|
2008-04-04 13:23:12 +00:00
|
|
|
#include <com/sun/star/frame/XUntitledNumbers.hpp>
|
|
|
|
|
|
|
|
#include <cppuhelper/basemutex.hxx>
|
|
|
|
#include <cppuhelper/weakref.hxx>
|
2015-07-10 17:50:12 +09:00
|
|
|
#include <cppuhelper/implbase.hxx>
|
2016-01-27 21:28:44 +01:00
|
|
|
|
2014-12-27 21:01:22 +00:00
|
|
|
#include <unordered_map>
|
2008-04-04 13:23:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-10-04 09:07:36 +02:00
|
|
|
namespace com { namespace sun { namespace star { namespace uno { class XInterface; } } } }
|
2008-04-04 13:23:12 +00:00
|
|
|
|
|
|
|
namespace comphelper{
|
|
|
|
|
2014-04-10 19:59:38 +03:00
|
|
|
/** @short defines a collection of UNO components, where every component will get its own unique number.
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2015-07-02 18:23:08 +02:00
|
|
|
@descr Such number will be unique at runtime only... but it supports fragmentation.
|
2014-04-29 19:05:05 +00:00
|
|
|
Note: This collection uses weak references only to know her components.
|
2015-07-02 18:23:08 +02:00
|
|
|
So lifetime of these components must be controlled outside.
|
2008-04-04 13:23:12 +00:00
|
|
|
|
|
|
|
@threadsafe
|
|
|
|
*/
|
2019-10-28 21:01:40 +02:00
|
|
|
class COMPHELPER_DLLPUBLIC NumberedCollection final : private ::cppu::BaseMutex
|
2015-07-10 17:50:12 +09:00
|
|
|
, public ::cppu::WeakImplHelper< css::frame::XUntitledNumbers >
|
2008-04-04 13:23:12 +00:00
|
|
|
{
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
// types, const
|
|
|
|
private:
|
|
|
|
|
|
|
|
struct TNumberedItem
|
|
|
|
{
|
|
|
|
css::uno::WeakReference< css::uno::XInterface > xItem;
|
|
|
|
::sal_Int32 nNumber;
|
|
|
|
};
|
|
|
|
|
2014-12-27 21:01:22 +00:00
|
|
|
typedef std::unordered_map<
|
2016-01-27 21:28:44 +01:00
|
|
|
long,
|
2016-03-04 10:04:07 +05:30
|
|
|
TNumberedItem > TNumberedItemHash;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
|
|
|
typedef ::std::vector< long > TDeadItemList;
|
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
// interface
|
|
|
|
public:
|
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** @short lightweight constructor.
|
|
|
|
*/
|
|
|
|
NumberedCollection();
|
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2014-02-09 10:12:07 +01:00
|
|
|
/** @short free all internally used resources.
|
2008-04-04 13:23:12 +00:00
|
|
|
*/
|
2016-09-13 13:09:01 +02:00
|
|
|
virtual ~NumberedCollection() override;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** 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);
|
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2013-04-07 12:06:47 +02:00
|
|
|
void setUntitledPrefix(const OUString& sPrefix);
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** @see css.frame.XUntitledNumbers */
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual ::sal_Int32 SAL_CALL leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent) override;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** @see css.frame.XUntitledNumbers */
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual void SAL_CALL releaseNumber(::sal_Int32 nNumber) override;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** @see css.frame.XUntitledNumbers */
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual void SAL_CALL releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent) override;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
/** @see css.frame.XUntitledNumbers */
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual OUString SAL_CALL getUntitledPrefix() override;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
// internal
|
|
|
|
private:
|
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2019-04-03 02:00:14 +00:00
|
|
|
/** @short tries to find a unique number not already used within this collection.
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2015-09-29 17:54:23 +02:00
|
|
|
@descr It reuses the smallest number which isn't used by any component
|
2008-04-04 13:23:12 +00:00
|
|
|
of this collection. (fragmentation!) If collection is full (means there
|
|
|
|
is no free number) the special value INVALID_NUMBER will be returned.
|
|
|
|
|
2017-09-24 22:54:11 +02:00
|
|
|
@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
|
2013-09-26 11:44:54 +02:00
|
|
|
doesn't used the returned number already.
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2017-09-24 22:54:11 +02:00
|
|
|
So the outside code has to make sure that retrieving and using of those numbers
|
2008-04-04 13:23:12 +00:00
|
|
|
will be an atomic operation.
|
|
|
|
|
2019-04-03 02:00:14 +00:00
|
|
|
@return a unique number or special value INVALID_NUMBER if collection is full.
|
2008-04-04 13:23:12 +00:00
|
|
|
*/
|
|
|
|
::sal_Int32 impl_searchFreeNumber ();
|
|
|
|
|
2015-03-25 15:37:53 +02:00
|
|
|
static void impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
|
|
|
|
const TDeadItemList& lDeadItems);
|
2008-04-04 13:23:12 +00:00
|
|
|
|
2014-02-25 19:06:16 +01:00
|
|
|
|
2008-04-04 13:23:12 +00:00
|
|
|
// member
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// localized string to be used for untitled components
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString m_sUntitledPrefix;
|
2008-04-04 13:23:12 +00:00
|
|
|
|
|
|
|
/// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace comphelper
|
|
|
|
|
2013-10-23 19:17:00 +02:00
|
|
|
#endif // INCLUDED_COMPHELPER_NUMBEREDCOLLECTION_HXX
|
2010-10-14 08:27:31 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|