CARLA
CallbackList.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/AtomicList.h"
10 #include "carla/NonCopyable.h"
11 
12 #include <atomic>
13 #include <functional>
14 
15 namespace carla {
16 namespace client {
17 namespace detail {
18 
19  template <typename... InputsT>
20  class CallbackList : private NonCopyable {
21  public:
22 
23  using CallbackType = std::function<void(InputsT...)>;
24 
25  void Call(InputsT... args) const {
26  auto list = _list.Load();
27  for (auto &item : *list) {
28  item.callback(args...);
29  }
30  }
31 
32  size_t Push(CallbackType &&callback) {
33  auto id = ++_counter;
34  DEBUG_ASSERT(id != 0u);
35  _list.Push(Item{id, std::move(callback)});
36  return id;
37  }
38 
39  void Remove(size_t id) {
40  _list.DeleteByValue(id);
41  }
42 
43  void Clear() {
44  _list.Clear();
45  }
46 
47  private:
48 
49  struct Item {
50  size_t id;
52 
53  friend bool operator==(const Item &lhs, const Item &rhs) {
54  return lhs.id == rhs.id;
55  }
56 
57  friend bool operator==(const Item &lhs, size_t rhs) {
58  return lhs.id == rhs;
59  }
60 
61  friend bool operator==(size_t lhs, const Item &rhs) {
62  return lhs == rhs.id;
63  }
64  };
65 
66  std::atomic_size_t _counter{0u};
67 
69  };
70 
71 } // namespace detail
72 } // namespace client
73 } // namespace carla
size_t Push(CallbackType &&callback)
Definition: CallbackList.h:32
void Call(InputsT... args) const
Definition: CallbackList.h:25
Holds an atomic pointer to a list.
Definition: AtomicList.h:25
friend bool operator==(size_t lhs, const Item &rhs)
Definition: CallbackList.h:61
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
friend bool operator==(const Item &lhs, size_t rhs)
Definition: CallbackList.h:57
friend bool operator==(const Item &lhs, const Item &rhs)
Definition: CallbackList.h:53
Inherit (privately) to suppress copy/move construction and assignment.