libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
SynchronizedCollection.h
1 /*
2  * This file is part of Adblock Plus <https://adblockplus.org/>,
3  * Copyright (C) 2006-present eyeo GmbH
4  *
5  * Adblock Plus is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 3 as
7  * published by the Free Software Foundation.
8  *
9  * Adblock Plus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #pragma once
18 #include <mutex>
19 #include <condition_variable>
20 
21 namespace AdblockPlus
22 {
28  template<typename TContainer>
30  {
31  protected:
32  typedef TContainer Container;
33  public:
37  typedef typename Container::value_type value_type;
38 
43  void push_back(const value_type& value)
44  {
45  {
46  std::lock_guard<std::mutex> lock(mutex);
47  collection.push_back(value);
48  }
49  conditionVar.notify_one();
50  }
51  void push_back(value_type&& value)
52  {
53  {
54  std::lock_guard<std::mutex> lock(mutex);
55  collection.push_back(std::move(value));
56  }
57  conditionVar.notify_one();
58  }
59 
65  value_type pop_front()
66  {
67  std::unique_lock<std::mutex> lock(mutex);
68  conditionVar.wait(lock, [this]()->bool
69  {
70  return !collection.empty();
71  });
72  value_type retValue = collection.front();
73  collection.pop_front();
74  return retValue;
75  }
76  protected:
77  Container collection;
78  std::mutex mutex;
79  std::condition_variable conditionVar;
80  };
81 }
value_type pop_front()
Extracts the first stored element and returns it.
Definition: SynchronizedCollection.h:65
Wrapper around Container providing few generic methods which ensure that the underlying container is ...
Definition: SynchronizedCollection.h:29
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
Container::value_type value_type
The value_type represents the type of stored values.
Definition: SynchronizedCollection.h:37
void push_back(const value_type &value)
Adds value normally to the end.
Definition: SynchronizedCollection.h:43