SMIL  0.9.1
DSignal.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 _DSIGNAL_H
30 #define _DSIGNAL_H
31 
32 #include <iostream>
33 #include <vector>
34 #include <algorithm>
35 
36 using namespace std;
37 
38 #include "DCommon.h"
39 #include "Core/include/DSlot.h"
40 
41 namespace smil
42 {
43  class BaseObject;
44 
45  class Event
46  {
47  public:
48  Event(BaseObject *_sender=NULL)
49  : sender(_sender)
50  {
51  }
52  const BaseObject *sender;
53  };
54 
55  class Signal
56  {
57  friend class BaseSlot;
58  public:
59  Signal(BaseObject *_sender=NULL)
60  : sender(_sender), enabled(true)
61  {}
62  virtual ~Signal()
63  {
64  disconnectAll();
65  }
66 
67  virtual void connect(BaseSlot *slot, bool _register=true)
68  {
69  vector<BaseSlot*>::iterator it = std::find(_slots.begin(), _slots.end(), slot);
70 
71  if (it!=_slots.end())
72  return;
73 
74  _slots.push_back(slot);
75  if (_register)
76  slot->registerSignal(this);
77  }
78 
79  virtual void disconnect(BaseSlot *slot, bool _unregister=true)
80  {
81  vector<BaseSlot*>::iterator it = std::find(_slots.begin(), _slots.end(), slot);
82 
83  if (it==_slots.end())
84  return;
85 
86  _slots.erase(it);
87 
88  if (_unregister)
89  slot->unregisterSignal(this, false);
90  }
91 
92  virtual void disconnectAll()
93  {
94  vector<BaseSlot*>::iterator it = _slots.begin();
95 
96  while(it!=_slots.end())
97  {
98  (*it)->unregisterSignal(this, false);
99  it++;
100  }
101  }
102 
103  virtual void trigger(Event *e=NULL)
104  {
105  if (!enabled)
106  return;
107 
108  if (e && sender)
109  e->sender = sender;
110 
111  vector<BaseSlot*>::iterator it = _slots.begin();
112 
113  while(it!=_slots.end())
114  {
115  (*it)->_run(e);
116  it++;
117  }
118  }
119 
120  const BaseObject *sender;
121  bool enabled;
122  protected:
123  vector<BaseSlot*> _slots;
124  };
125 
126 } // namespace smil
127 
128 
129 #endif // _DSIGNAL_H
130 
Definition: DColorConvert.h:38
Definition: DSignal.h:55
Definition: DSlot.h:44
Base Smil Object.
Definition: DBaseObject.h:53
Definition: DSignal.h:45