iCub-main
EventDispatcher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <cassert>
20 
22 
23 namespace iCub {
24 namespace learningmachine {
25 
26 
27 // the copy constructor and the assignment operator are best left unimplemented
28 // for this singleton. Anyhow, here is how they could be implemented.
29 
30 //EventDispatcher::EventDispatcher(const EventDispatcher& other) {
31 // for(int i = 0; i < other.countListeners(); i++) {
32 // addListener(other.getAt(i).clone());
33 // }
34 //}
35 
36 //EventDispatcher& EventDispatcher::operator=(const EventDispatcher& other) {
37 // if(this == &other) return *this; // handle self initialization
38 
39 // this->clear();
40 // for(int i = 0; i < other.countListeners(); i++) {
41 // this->addListener(other.getAt(i).clone());
42 // }
43 // return *this;
44 //}
45 
46 
47 EventDispatcher::~EventDispatcher() {
48  this->clear();
49 }
50 
52  assert(idx >= 0 && idx < int(this->listeners.size()));
53  std::list<IEventListener*>::iterator it = this->listeners.begin();
54  std::advance(it, idx);
55  delete *it;
56  this->listeners.erase(it);
57 }
58 
60  this->listeners.remove(listener);
61  //delete listener;
62 }
63 
65  std::list<IEventListener*>::iterator it = this->listeners.begin();
66  std::advance(it, idx);
67  return **it;
68 }
69 
71  std::list<IEventListener*>::const_iterator it = this->listeners.begin();
72  std::advance(it, idx);
73  return **it;
74 }
75 
77  // remove from front while the list is not empty
78  while(this->hasListeners()) {
79  this->removeListener(0);
80  }
81 }
82 
84  std::list<IEventListener*>::iterator it;
85  for(it = listeners.begin(); it != listeners.end(); it++) {
86  if((*it)->isEnabled()) {
87  event.visit(**it);
88  }
89  }
90 }
91 
92 
93 } // learningmachine
94 } // iCub
95 
virtual bool hasListeners()
Tells whether there are listeners for events.
virtual void clear()
Clears all the IEventListeners from the EventDispatcher.
virtual void raise(IEvent &event)
Raises an IEvent, causing it to be dispatched to each registered IEventListener.
virtual IEventListener & getAt(int idx)
Returns the IEventListener at a specified index.
virtual void removeListener(int idx)
Removes an IEventListener from the list.
static uint32_t idx[BOARD_NUM]
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.