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 INCLUDED_COMPHELPER_WEAKBAG_HXX
21 : : #define INCLUDED_COMPHELPER_WEAKBAG_HXX
22 : :
23 : : #include "sal/config.h"
24 : :
25 : : #include <list>
26 : : #include "com/sun/star/uno/Reference.hxx"
27 : : #include "cppuhelper/weakref.hxx"
28 : : #include "osl/diagnose.h"
29 : :
30 : : namespace comphelper {
31 : :
32 : : /**
33 : : A bag of UNO weak references.
34 : : */
35 : 316 : template< typename T > class WeakBag {
36 : : public:
37 : : /**
38 : : Add a new weak reference.
39 : :
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.
43 : :
44 : : @param e
45 : : a non-null reference.
46 : : */
47 : 96 : void add(com::sun::star::uno::Reference< T > const & e) {
48 : : OSL_ASSERT(e.is());
49 [ - + ]: 96 : for (typename WeakReferenceList::iterator i(m_list.begin()); i != m_list.end();) {
50 [ # # ][ # # ]: 0 : if (com::sun::star::uno::Reference< T >(*i).is()) {
51 : 0 : ++i;
52 : : } else {
53 [ # # ]: 0 : i = m_list.erase(i);
54 : : }
55 : : }
56 [ + - ]: 96 : m_list.push_back(com::sun::star::uno::WeakReference< T >(e));
57 : 96 : }
58 : :
59 : : /**
60 : : Remove a living reference.
61 : :
62 : : @return
63 : : a living reference, or null if there are none.
64 : : */
65 : 254 : com::sun::star::uno::Reference< T > remove() {
66 [ + + ]: 254 : while (!m_list.empty()) {
67 [ + - ][ + - ]: 96 : com::sun::star::uno::Reference< T > r(m_list.front());
68 [ + - ]: 96 : m_list.pop_front();
69 [ + - ]: 96 : if (r.is()) {
70 [ - + ]: 96 : return r;
71 : : }
72 : : }
73 : 254 : return com::sun::star::uno::Reference< T >();
74 : : }
75 : :
76 : : private:
77 : : typedef std::list< com::sun::star::uno::WeakReference< T > > WeakReferenceList;
78 : :
79 : : WeakReferenceList m_list;
80 : : };
81 : :
82 : : }
83 : :
84 : : #endif
85 : :
86 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|