blob: ef5f0041268aa98bf73c639aa996a449a64b957d [file] [log] [blame]
Alexander Afanasyev9d158f02015-02-17 21:30:19 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyev9d158f02015-02-17 21:30:19 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_DETAIL_CONTAINER_WITH_ON_EMPTY_SIGNAL_HPP
23#define NDN_DETAIL_CONTAINER_WITH_ON_EMPTY_SIGNAL_HPP
24
25#include "../common.hpp"
26#include "../util/signal.hpp"
27
28namespace ndn {
29
30/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000031 * @brief A container that emits onEmpty signal when it becomes empty
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080032 */
33template<class T>
34class ContainerWithOnEmptySignal
35{
36public:
37 typedef std::list<T> Base;
38 typedef typename Base::value_type value_type;
39 typedef typename Base::iterator iterator;
40
41 iterator
42 begin()
43 {
44 return m_container.begin();
45 }
46
47 iterator
48 end()
49 {
50 return m_container.end();
51 }
52
53 size_t
54 size()
55 {
56 return m_container.size();
57 }
58
59 bool
60 empty()
61 {
62 return m_container.empty();
63 }
64
65 iterator
66 erase(iterator item)
67 {
68 iterator next = m_container.erase(item);
69 if (empty()) {
70 this->onEmpty();
71 }
72 return next;
73 }
74
75 void
76 clear()
77 {
78 m_container.clear();
79 this->onEmpty();
80 }
81
82 std::pair<iterator, bool>
83 insert(const value_type& value)
84 {
85 return {m_container.insert(end(), value), true};
86 }
87
88 template<class Predicate>
89 void remove_if(Predicate p)
90 {
91 m_container.remove_if(p);
92 if (empty()) {
93 this->onEmpty();
94 }
95 }
96
97public:
98 Base m_container;
99
100 /**
101 * @brief Signal to be fired when container becomes empty
102 */
103 util::Signal<ContainerWithOnEmptySignal<T>> onEmpty;
104};
105
106} // namespace ndn
107
108#endif // NDN_DETAIL_CONTAINER_WITH_ON_EMPTY_SIGNAL_HPP