OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CallbackManager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <limits>
20 #include <map>
21 #include <type_traits>
22 #include <vector>
23 
62 template <typename IDType, typename CallbackType, typename ContextType>
64  static_assert(std::is_integral_v<IDType>, "IDType must be and integral type");
65  static_assert(
66  std::is_invocable_v<CallbackType, const ContextType&>,
67  "CallbackType must be callable with \'const ContextType&\' as the argument");
68 
69  public:
70  IDType registerCallback(CallbackType callback) {
71  CHECK_LT(next_id_, std::numeric_limits<IDType>::max());
72  callbacks_.emplace(next_id_++, callback);
73  return next_id_ - 1;
74  }
75 
76  void unregisterCallback(IDType id) {
77  CHECK(callbacks_.count(id)) << "Callback id not found";
78  if (is_notifying_) {
79  // Defer unregistration until notification loop is complete
80  deferred_unregister_ids_.push_back(id);
81  } else {
82  callbacks_.erase(id);
83  }
84  }
85 
86  void notify(const ContextType& context) {
87  is_notifying_ = true;
88  for (auto callback_pair : callbacks_) {
89  callback_pair.second(context);
90  }
91  is_notifying_ = false;
92 
93  // Unregister any callbacks that tried to unregister during notification
94  if (!deferred_unregister_ids_.empty()) {
95  for (auto id : deferred_unregister_ids_) {
97  }
98  deferred_unregister_ids_.clear();
99  }
100  }
101 
102  bool isEmpty() const { return callbacks_.empty(); }
103 
104  private:
105  // Use std::map to ensure callback iteration is in registration order
106  std::map<IDType, CallbackType> callbacks_;
107  IDType next_id_;
109  std::vector<IDType> deferred_unregister_ids_;
110 };
std::map< IDType, CallbackType > callbacks_
void notify(const ContextType &context)
#define CHECK_LT(x, y)
Definition: Logger.h:303
std::vector< IDType > deferred_unregister_ids_
bool isEmpty() const
#define CHECK(condition)
Definition: Logger.h:291
void unregisterCallback(IDType id)
IDType registerCallback(CallbackType callback)