Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Function.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Cinder Project (http://libcinder.org)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 
27 #include <algorithm>
28 #include <vector>
29 #include <utility>
30 
31 #include <boost/signals2.hpp>
32 namespace cinder { namespace signals {
33  using namespace boost::signals2;
34 } } // cinder::signals
35 
36 #include <functional>
37 
38 namespace cinder {
39 
41 typedef uint32_t CallbackId;
42 
44 template<typename SIG>
45 class CallbackMgr {
46  public:
47  typedef typename std::vector<std::pair<CallbackId,std::function<SIG> > > collection;
48  typedef typename collection::iterator iterator;
49 
50  CallbackId registerCb( std::function<SIG> cb )
51  {
52  CallbackId cbId = 0;
53  if( ! mCallbacks.empty() )
54  cbId = mCallbacks.rbegin()->first + 1;
55  mCallbacks.push_back( std::make_pair( cbId, cb ) );
56  return cbId;
57  }
58 
59  CallbackId registerCb( iterator position, std::function<SIG> cb )
60  {
61  CallbackId cbId = 0;
62  if( ! mCallbacks.empty() )
63  cbId = mCallbacks.rbegin()->first + 1;
64  mCallbacks.insert( position, std::make_pair( cbId, cb ) );
65  return cbId;
66  }
67 
68  void call() { for( iterator it = begin(); it != end(); ++it ) it->second(); }
69  template<typename A1>
70  void call( A1 a1 ) { for( iterator it = begin(); it != end(); ++it ) it->second( a1 ); }
71  template<typename A1, typename A2>
72  void call( A1 a1, A2 a2 ) { for( iterator it = begin(); it != end(); ++it ) it->second( a1, a2 ); }
73  template<typename A1, typename A2, typename A3>
74  void call( A1 a1, A2 a2, A3 a3 ) { for( iterator it = begin(); it != end(); ++it ) it->second( a1, a2, a3 ); }
75  template<typename A1, typename A2, typename A3, typename A4>
76  void call( A1 a1, A2 a2, A3 a3, A4 a4 ) { for( iterator it = begin(); it != end(); ++it ) it->second( a1, a2, a3, a4 ); }
77  template<typename A1, typename A2, typename A3, typename A4, typename A5>
78  void call( A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 ) { for( iterator it = begin(); it != end(); ++it ) it->second( a1, a2, a3, a4, a5 ); }
79 
80  void unregisterCb( CallbackId cbId ) { mCallbacks.erase( find( cbId ) ); }
81 
82  bool empty() const { return mCallbacks.empty(); }
83 
84  iterator find( CallbackId cbId ) { for( iterator it = begin(); it != end(); ++it ) if( it->first == cbId ) return it; return mCallbacks.end(); }
85  iterator begin() { return mCallbacks.begin(); }
86  iterator end() { return mCallbacks.end(); }
87 
88  collection& getCallbacks() { return mCallbacks; }
89 
90  private:
91  collection mCallbacks;
92 };
93 
94 } // namespace cinder
collection & getCallbacks()
Definition: Function.h:88
Implements a utility class for maintaining a list of callbacks.
Definition: Function.h:45
void call(A1 a1)
Definition: Function.h:70
CallbackId registerCb(iterator position, std::function< SIG > cb)
Definition: Function.h:59
uint32_t CallbackId
Represents a unique identifier for a callback.
Definition: Function.h:41
iterator begin()
Definition: Function.h:85
CallbackId registerCb(std::function< SIG > cb)
Definition: Function.h:50
std::vector< std::pair< CallbackId, std::function< SIG > > > collection
Definition: Function.h:47
void call(A1 a1, A2 a2, A3 a3)
Definition: Function.h:74
bool empty() const
Definition: Function.h:82
collection::iterator iterator
Definition: Function.h:48
iterator end()
Definition: Function.h:86
void unregisterCb(CallbackId cbId)
Definition: Function.h:80
void call(A1 a1, A2 a2)
Definition: Function.h:72
void call(A1 a1, A2 a2, A3 a3, A4 a4)
Definition: Function.h:76
void call(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
Definition: Function.h:78
void call()
Definition: Function.h:68
iterator find(CallbackId cbId)
Definition: Function.h:84