Open Inventor Release 2023.2.3
 
Loading...
Searching...
No Matches
SbEventHandler.h
Go to the documentation of this file.
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2014 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : Thibaut Andrieu (Mar 2015)
22**=======================================================================*/
23
24#ifndef _SB_EVENTHANDLER_H_
25#define _SB_EVENTHANDLER_H_
26
27#include <Inventor/SbDelegate.h>
28
99template <typename ArgType>
101{
102public:
103
109 void operator()(ArgType arg)
110 {
111 //Copy the delegate vector because the called function may change the vector
112 DelegateVector delegateCopy = m_delegates;
113 typename DelegateVector::const_iterator it = delegateCopy.begin();
114 typename DelegateVector::const_iterator end = delegateCopy.end();
115 while ( it != end )
116 {
117 (*it)(arg);
118 ++it;
119 }
120 }
121
123 void add(void (*f)(ArgType))
124 {
125 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(f);
126 m_delegates.push_back(delegate);
127 }
128
130 template <typename ClassType>
131 void add(ClassType& a, void (ClassType::*f)(ArgType))
132 {
133 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(a, f);
134 m_delegates.push_back(delegate);
135 }
136
142 bool remove(void (*f)(ArgType))
143 {
144 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
145 {
146 if ( itDelegate->equal(f) )
147 {
148 m_delegates.erase(itDelegate);
149 return true;
150 }
151 }
152 return false;
153 }
154
160 template <typename ClassType>
161 bool remove(ClassType& a, void (ClassType::*f)(ArgType))
162 {
163 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
164 {
165 if ( itDelegate->equal(a, f) )
166 {
167 m_delegates.erase(itDelegate);
168 return true;
169 }
170 }
171 return false;
172 }
173
174private:
175
176 typedef std::vector< inventor::helper::SbDelegate<void, ArgType> > DelegateVector;
178 DelegateVector m_delegates;
179};
180
181#endif // _SB_EVENTHANDLER_H_
Class representing an event.
bool remove(void(*f)(ArgType))
Remove a static function.
void add(ClassType &a, void(ClassType::*f)(ArgType))
Add a member function callback for this event.
void operator()(ArgType arg)
Call all registered callbacks.
void add(void(*f)(ArgType))
Add a static function callback for this event.
bool remove(ClassType &a, void(ClassType::*f)(ArgType))
Remove a member function.
iterator end()