mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-03 08:57:57 +00:00
Initialize all members.
Remove the leading whitespace.
This commit is contained in:
parent
45a19e60c4
commit
365c06e709
@ -17,83 +17,83 @@
|
||||
#include <algorithm>
|
||||
|
||||
namespace events{
|
||||
observer::~observer() {
|
||||
}
|
||||
generic_event::generic_event(std::string name){
|
||||
name_ = name;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -28,42 +28,44 @@ specific events but rather defines a generic framework.
|
||||
*/
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user