LibreOffice Module comphelper (master)  1
weakbag.hxx
Go to the documentation of this file.
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 <vector>
26 #include <com/sun/star/uno/Reference.h>
27 #include <cppuhelper/weakref.hxx>
28 #include <osl/diagnose.h>
29 
30 namespace comphelper {
31 
35 template< typename T > class WeakBag {
36 public:
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);
54  }
55  }
56  m_list.push_back(css::uno::WeakReference< T >(e));
57  }
58 
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;
71  }
72  }
73  return css::uno::Reference< T >();
74  }
75 
76 private:
77  std::vector< css::uno::WeakReference< T > > m_list;
78 };
79 
80 }
81 
82 #endif
83 
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A bag of UNO weak references.
Definition: weakbag.hxx:35
int i
std::vector< css::uno::WeakReference< T > > m_list
Definition: weakbag.hxx:77
void add(css::uno::Reference< T > const &e)
Add a new weak reference.
Definition: weakbag.hxx:47