Author: mordante
Date: Fri Jul 18 17:49:42 2008
New Revision: 28068
URL: http://svn.gna.org/viewcvs/wesnoth?rev=28068&view=rev
Log:
Initialize all members.
Remove the leading whitespace.
Modified:
trunk/src/generic_event.cpp
trunk/src/generic_event.hpp
Modified: trunk/src/generic_event.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/generic_event.cpp?rev=28068&r1=28067&r2=28068&view=diff
==============================================================================
--- trunk/src/generic_event.cpp (original)
+++ trunk/src/generic_event.cpp Fri Jul 18 17:49:42 2008
@@ -17,83 +17,83 @@
#include <algorithm>
namespace events{
- observer::~observer() {
+
+generic_event::generic_event(std::string name) :
+ name_(name),
+ observers_(),
+ change_handler_(false),
+ notify_active_(false)
+{
+}
+
+bool generic_event::attach_handler(observer* obs){
+ bool handler_attached = false;
+
+ //make sure observers are not notified right now
+ if (!notify_active_){
+ change_handler_ = true;
+ try{
+ std::vector<observer*>::const_iterator it =
std::find(observers_.begin(), observers_.end(), obs);
+ if (it != observers_.end()){
+ handler_attached = false;
+ }
+ else{
+ observers_.push_back(obs);
+ handler_attached = true;
+ }
+ }
+ catch (std::exception&){
+ change_handler_ = false;
+ throw;
+ }
+ change_handler_ = false;
}
- generic_event::generic_event(std::string name){
- name_ = name;
+
+ return handler_attached;
+}
+
+bool generic_event::detach_handler(observer* obs){
+ bool handler_detached = false;
+
+ //make sure observers are not notified right now
+ if (!notify_active_){
+ change_handler_ = true;
+ try{
+ std::vector<observer*>::iterator it =
std::find(observers_.begin(), observers_.end(), obs);
+ if (it == observers_.end()){
+ handler_detached = false;
+ }
+ else{
+ observers_.erase(it);
+ handler_detached = true;
+ }
+ }
+ catch (std::exception&){
+ change_handler_ = false;
+ throw;
+ }
change_handler_ = false;
+ }
+
+ return handler_detached;
+}
+
+void generic_event::notify_observers(){
+ if (!change_handler_){
+ notify_active_ = true;
+ try{
+ for (std::vector<observer*>::const_iterator it =
observers_.begin();
+ it != observers_.end(); it++){
+ (*it)->handle_generic_event(name_);
+ }
+ }
+ catch (std::exception&){
+ //reset the flag if event handlers throw exceptions and
don't catch them
+ notify_active_ = false;
+ throw;
+ }
notify_active_ = false;
}
- generic_event::~generic_event() {
- }
- bool generic_event::attach_handler(observer* obs){
- bool handler_attached = false;
-
- //make sure observers are not notified right now
- if (!notify_active_){
- change_handler_ = true;
- try{
- std::vector<observer*>::const_iterator it =
std::find(observers_.begin(), observers_.end(), obs);
- if (it != observers_.end()){
- handler_attached = false;
- }
- else{
- observers_.push_back(obs);
- handler_attached = true;
- }
- }
- catch (std::exception&){
- change_handler_ = false;
- throw;
- }
- change_handler_ = false;
- }
-
- return handler_attached;
- }
-
- bool generic_event::detach_handler(observer* obs){
- bool handler_detached = false;
-
- //make sure observers are not notified right now
- if (!notify_active_){
- change_handler_ = true;
- try{
- std::vector<observer*>::iterator it =
std::find(observers_.begin(), observers_.end(), obs);
- if (it == observers_.end()){
- handler_detached = false;
- }
- else{
- observers_.erase(it);
- handler_detached = true;
- }
- }
- catch (std::exception&){
- change_handler_ = false;
- throw;
- }
- change_handler_ = false;
- }
-
- return handler_detached;
- }
-
- void generic_event::notify_observers(){
- if (!change_handler_){
- notify_active_ = true;
- try{
- for (std::vector<observer*>::const_iterator it
= observers_.begin();
- it != observers_.end(); it++){
- (*it)->handle_generic_event(name_);
- }
- }
- catch (std::exception&){
- //reset the flag if event handlers throw
exceptions and don't catch them
- notify_active_ = false;
- throw;
- }
- notify_active_ = false;
- }
- }
+}
} //namespace events
Modified: trunk/src/generic_event.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/generic_event.hpp?rev=28068&r1=28067&r2=28068&view=diff
==============================================================================
--- trunk/src/generic_event.hpp (original)
+++ trunk/src/generic_event.hpp Fri Jul 18 17:49:42 2008
@@ -28,42 +28,44 @@
*/
namespace events{
- /*
- This is the observer that gets notified, if a generic event takes place
- Use this as base class for every class that is supposed to react on a
- generic event.
- */
- class observer{
- public:
- virtual void handle_generic_event(const std::string&
event_name) = 0;
- virtual ~observer();
- };
+/*
+This is the observer that gets notified, if a generic event takes place
+Use this as base class for every class that is supposed to react on a
+generic event.
+*/
+class observer{
+public:
+ virtual void handle_generic_event(const std::string& event_name) = 0;
+ virtual ~observer() {}
+};
- /*
- This is the class that notifies the observers and maintains a list of
them.
- */
- class generic_event{
- public:
- generic_event(std::string name);
- virtual ~generic_event();
- virtual bool attach_handler(observer* obs);
- virtual bool detach_handler(observer* obs);
- virtual void notify_observers();
- private:
- //Name of the event to help event handlers distinguish between
several events
- std::string name_;
- //List of all subscribers waiting to react on this event
- std::vector<observer*> observers_;
+/*
+This is the class that notifies the observers and maintains a list of them.
+*/
+class generic_event{
+public:
+ generic_event(std::string name);
+ virtual ~generic_event() {}
- //This flag makes sure, that an event is not raised while the
vector of
- //observers is changed through attach_handler or detach_handler
- bool change_handler_;
+ virtual bool attach_handler(observer* obs);
+ virtual bool detach_handler(observer* obs);
+ virtual void notify_observers();
+private:
+ //Name of the event to help event handlers distinguish between several
events
+ std::string name_;
- //This flag makes sure, that attaching/detaching event handlers
does not
- //take place during notify of observers to prevent iterator
corruption.
- bool notify_active_;
- };
+ //List of all subscribers waiting to react on this event
+ std::vector<observer*> observers_;
+
+ //This flag makes sure, that an event is not raised while the vector of
+ //observers is changed through attach_handler or detach_handler
+ bool change_handler_;
+
+ //This flag makes sure, that attaching/detaching event handlers does not
+ //take place during notify of observers to prevent iterator corruption.
+ bool notify_active_;
+};
}
#endif
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits