SMIL  1.1
DSlot.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _DSLOT_H
30 #define _DSLOT_H
31 
32 #include <vector>
33 #include <iostream>
34 
35 using namespace std;
36 
37 #include "DCommon.h"
38 
39 namespace smil
40 {
41  class Event;
42  class Signal;
43 
44  class BaseSlot
45  {
46  friend class Signal;
47  public:
48  BaseSlot() {}
49  virtual ~BaseSlot()
50  {
51  unregisterAll();
52  }
53  virtual void run(Event * /*e*/ = NULL)
54  {
55  }
56  protected:
57 #ifndef SWIG
58  virtual void _run(Event *e=NULL) { run(e); }
59  virtual void registerSignal(Signal *signal);
60  virtual void unregisterSignal(Signal *signal, bool _disconnect=true);
61  virtual void unregisterAll();
62  vector<Signal*> _signals;
63 #endif // SWIG
64  };
65 
66  template <class eventT>
67  class Slot : public BaseSlot
68  {
69  public:
70  Slot() {}
71  virtual ~Slot() {}
72  virtual void run(eventT * /*e*/)
73  {
74  }
75  void operator() (eventT * /*e*/)
76  {
77  }
78 #ifndef SWIG
79  protected:
80  virtual void _run(Event *e)
81  {
82  run(static_cast<eventT*>(e));
83  }
84 #endif // SWIG
85  private:
86  using BaseSlot::run;
87  };
88 
89  template <class T, class eventT=Event>
90  class MemberFunctionSlot : public Slot<eventT>
91  {
92  public:
93  typedef void(T::*memberFunc)(eventT*);
94  typedef void(T::*voidMemberFunc)();
96  {
97  _instance = NULL;
98  _function = NULL;
99  }
100  MemberFunctionSlot(T *inst, memberFunc func)
101  {
102  init(inst, func);
103  }
104  MemberFunctionSlot(T *inst, voidMemberFunc func)
105  {
106  init(inst, func);
107  }
108  void init(T *inst, memberFunc func)
109  {
110  _instance = inst;
111  _function = func;
112  }
113  void init(T *inst, voidMemberFunc func)
114  {
115  _instance = inst;
116  _void_function = func;
117  }
118  protected:
119  T *_instance;
120  memberFunc _function;
121  voidMemberFunc _void_function;
122  virtual void run(eventT *e=NULL)
123  {
124  if (!_instance)
125  return;
126 
127  if (_function)
128  (_instance->*_function)(e);
129  if (_void_function)
130  (_instance->*_void_function)();
131  }
132  };
133 
134  template <class eventT=Event>
135  class FunctionSlot : public Slot<eventT>
136  {
137  public:
138  typedef void(*funcPtr)(eventT*);
139  typedef void(*voidFuncPtr)();
140  FunctionSlot(funcPtr func)
141  {
142  _function = func;
143  }
144  protected:
145  funcPtr _function;
146  virtual void run(eventT *e)
147  {
148  (*_function)(e);
149  }
150  };
151 
152 } // namespace smil
153 
154 
155 #endif // _DSLOT_H
Definition: DSlot.h:45
Definition: DSignal.h:46
Definition: DSlot.h:136
Definition: DSlot.h:91
Definition: DSignal.h:56
Definition: DSlot.h:68