bump product version to 7.6.3.2-android
[LibreOffice.git] / include / comphelper / weakbag.hxx
blobc52defab23eb9486016dbbc8b1b9f567ebcbfa9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_WEAKBAG_HXX
21 #define INCLUDED_COMPHELPER_WEAKBAG_HXX
23 #include <sal/config.h>
25 #include <vector>
26 #include <com/sun/star/uno/Reference.h>
27 #include <cppuhelper/weakref.hxx>
28 #include <osl/diagnose.h>
30 namespace comphelper {
32 /**
33 A bag of UNO weak references.
35 template< typename T > class WeakBag {
36 public:
37 /**
38 Add a new weak reference.
40 The implementation keeps the amount of memory consumed linear in the
41 number of living references added, not linear in the number of total
42 references added.
44 @param e
45 a non-null reference.
47 void add(css::uno::Reference< T > const & e) {
48 OSL_ASSERT(e.is());
49 for (auto i = m_list.begin(); i != m_list.end();) {
50 if (css::uno::Reference< T >(*i).is()) {
51 ++i;
52 } else {
53 i = m_list.erase(i);
56 m_list.push_back(css::uno::WeakReference< T >(e));
59 /**
60 Remove a living reference.
62 @return
63 a living reference, or null if there are none.
65 css::uno::Reference< T > remove() {
66 while (!m_list.empty()) {
67 css::uno::Reference< T > r(m_list.back());
68 m_list.pop_back();
69 if (r.is()) {
70 return r;
73 return css::uno::Reference< T >();
76 private:
77 std::vector< css::uno::WeakReference< T > > m_list;
82 #endif
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */