MAK Legion API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
callbackList.h
Go to the documentation of this file.
1 /*********************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <list>
12 
13 namespace Legion
14 {
16  template<typename Cb, typename... Param>
18  {
19  public:
21  :myIsExecuting(false)
22  {
23 
24  }
25 
27  {
28 
29  }
30 
36  bool add(Cb cb)
37  {
38  if (myIsExecuting)
39  {
40  myToAddCallbacks.push_back(cb);
41  return true;
42  }
43  else
44  {
45  //protect against double add
46  std::list<Cb>::iterator iter = std::find(myCallbacks.begin(), myCallbacks.end(), cb);
47  if (iter == myCallbacks.end())
48  {
49  myCallbacks.push_back(cb);
50  }
51  else
52  {
53  return false;
54  }
55 
56  return true;
57  }
58  }
59 
64  bool remove(Cb cb)
65  {
66  if (myIsExecuting)
67  {
68  myToRemoveCallbacks.push_back(cb);
69  }
70  else
71  {
72  myCallbacks.remove(cb);
73  }
74 
75  return true;
76  }
77 
79  void execute(Param... params)
80  {
81  myIsExecuting = true;
82  std::list<Cb>::iterator iter = myCallbacks.begin();
83  for (iter; iter != myCallbacks.end(); ++iter)
84  {
85  (*iter)(params...);
86  }
87  myIsExecuting = false;
88 
89  //if callbacks were added during execution, add them
90  iter = myToAddCallbacks.begin();
91  for (iter; iter != myToAddCallbacks.end(); ++iter)
92  {
93  add(*iter);
94  }
95  myToAddCallbacks.clear();
96 
97  //if callbacks were removed during execution, remove them
98  iter = myToRemoveCallbacks.begin();
99  for (iter; iter != myToRemoveCallbacks.end(); ++iter)
100  {
101  remove(*iter);
102  }
103  myToRemoveCallbacks.clear();
104  }
105 
106  protected:
107  std::list<Cb> myCallbacks;
108  std::list<Cb> myToAddCallbacks;
109  std::list<Cb> myToRemoveCallbacks;
111  };
112 
113 }
std::list< Cb > myToAddCallbacks
Definition: callbackList.h:108
std::list< Cb > myCallbacks
Definition: callbackList.h:107
bool myIsExecuting
Definition: callbackList.h:110
void execute(Param...params)
Execute all the callbacks on the internal list of callbacks.
Definition: callbackList.h:79
~CallbackList()
Definition: callbackList.h:26
bool add(Cb cb)
add a callback to the internal list.
Definition: callbackList.h:36
CallbackList()
Definition: callbackList.h:20
The Callback list holds a list of Callback objects and manages adding/removing them from the list...
Definition: callbackList.h:17
std::list< Cb > myToRemoveCallbacks
Definition: callbackList.h:109

Document ID: Generated on Mon Mar 8 15:11:30 EST 2021 from SVN revision 225633
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)