Changeset 7395 for code/branches/notifications/src
- Timestamp:
- Sep 9, 2010, 10:59:01 PM (14 years ago)
- Location:
- code/branches/notifications/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/notifications/src/modules/notifications/CMakeLists.txt
r7354 r7395 13 13 TOLUA_FILES 14 14 NotificationManager.h 15 NotificationQueue.h 15 16 PCH_FILE 16 17 NotificationsPrecompiledHeaders.h -
code/branches/notifications/src/modules/notifications/NotificationManager.cc
r7362 r7395 28 28 29 29 /** 30 @file 30 @file NotificationManager.cc 31 31 @brief Implementation of the NotificationManager class. 32 32 */ … … 43 43 #include "NotificationQueue.h" 44 44 45 #include "ToluaBindNotifications.h" 46 45 47 namespace orxonox 46 48 { … … 49 51 const std::string NotificationManager::NONE("none"); 50 52 53 // Register tolua_open function when loading the library. 54 DeclareToluaInterface(Notifications); 55 51 56 ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false); 52 57 … … 65 70 66 71 ModifyConsoleCommand("enterEditMode").setObject(this); 67 68 if(GameMode::showsGraphics()) 69 { 70 GUIManager::getInstance().loadGUI("NotificationLayer"); 71 72 // Create first queue: 73 this->queues_.push_back(new NotificationQueue("all")); 74 } 72 73 COUT(3) << "NotificatioManager created." << std::endl; 75 74 } 76 75 … … 82 81 { 83 82 ModifyConsoleCommand("enterEditMode").setObject(NULL); 83 84 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 85 it->second->destroy(); 86 87 COUT(3) << "NotificationManager destroyed." << std::endl; 84 88 } 85 89 86 90 void NotificationManager::preDestroy(void) 87 91 { 88 for(std::vector<NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) 89 (*it)->destroy(); 92 for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); ) 93 { 94 NotificationQueue* queue = (*it).second; 95 it++; 96 queue->destroy(); 97 } 90 98 this->queues_.clear(); 91 99 } … … 101 109 bool NotificationManager::registerNotification(Notification* notification) 102 110 { 103 104 if(notification == NULL) //!< A NULL-Notification cannot be registered. 111 if(notification == NULL) // A NULL-Notification cannot be registered. 105 112 return false; 106 113 107 114 std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time. 108 115 109 this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time,notification));110 111 if(notification->getSender() == N ONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.116 this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); 117 118 if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications. 112 119 return true; 113 120 114 121 bool all = false; 115 if(notification->getSender() == ALL) // If all are the sender, then the Notifications is added to every NotificationListener.122 if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener. 116 123 all = true; 117 124 … … 119 126 for(std::map<NotificationListener*,int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all listeners. 120 127 { 121 std::set<std::string> set = it->first->getTargetsSet(); 122 if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TODO: Make sure this works. 128 std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet(); 129 bool bAll = set.find(NotificationManager::ALL) != set.end(); 130 if(all || bAll || set.find(notification->getSender()) != set.end()) //TODO: Make sure this works. 123 131 { 124 this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); // Insert the Notification in the Notifications list of the current NotificationListener. 132 if(!bAll) 133 { 134 this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the Notifications list of the current NotificationListener. 135 } 125 136 it->first->update(notification, time); // Update the listener. 126 137 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(notification); … … 132 143 } 133 144 134 COUT(4) << "Notification registered with the NotificationManager." << std::endl;145 COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl; 135 146 136 147 return true; … … 154 165 this->listenerCounter_[notification] = this->listenerCounter_[notification] - 1; 155 166 156 // If the Notification is no longer present in any of the NotificationListeners it can be removed from the map of all Notifications and be destroyed. 157 if(this->listenerCounter_[notification] == (unsigned int) 0) 158 { 159 this->removeNotification(notification, this->allNotificationsList_); 160 this->listenerCounter_.erase(notification); 161 notification->destroy(); 162 } 163 164 COUT(4) << "Notification unregistered with the NotificationManager." << std::endl; 167 COUT(4) << "Notification (&" << notification << ")unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl; 165 168 } 166 169 … … 175 178 Returns true if successful. 176 179 */ 177 //TODO: Needed?178 180 bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map) 179 181 { … … 206 208 this->listenerList_[listener] = index; // Add the NotificationListener to the list of listeners. 207 209 208 std::set<std::string > set = listener->getTargetsSet(); //TODO: Does this work?210 std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet(); 209 211 210 212 // If all senders are the target of the listener, then the list of notification for that specific listener is te same as the list of all Notifications. 211 if(set.find(ALL) != set.end()) 212 { 213 bool bAll = set.find(NotificationManager::ALL) != set.end(); 214 std::multimap<std::time_t, Notification*> map; 215 if(bAll) 213 216 this->notificationLists_[index] = &this->allNotificationsList_; 214 COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl; 215 return true; 216 } 217 218 this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>; 219 std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index]; 217 else 218 { 219 this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>; 220 map = *this->notificationLists_[index]; 221 } 220 222 221 223 // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener. 222 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)223 { 224 if( set.find(it->second->getSender()) != set.end()) // Checks whether the overlayhas the sender of the current notification as target.224 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 225 { 226 if(bAll || set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current notification as target. 225 227 { 226 map.insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 228 if(!bAll) 229 map.insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 227 230 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(it->second); 228 231 if(counterIt == this->listenerCounter_.end()) … … 288 291 return false; 289 292 290 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // The Notifications for the input NotificationListener.291 292 if(notifications == NULL) // Returns NULL, if there are no Notifications.293 return true;293 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // The Notifications for the input NotificationListener. 294 295 if(notifications == NULL) // Returns false, if there are no Notifications. 296 return false; 294 297 295 298 std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; 296 299 itLowest = notifications->lower_bound(timeFrameStart); 297 itHighest = notifications->upper_bound(timeFrame Start);300 itHighest = notifications->upper_bound(timeFrameEnd); 298 301 299 302 for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time Frame to the end of it. 300 { 301 map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); // Add the found Notifications to the map. 302 } 303 map->insert(std::pair<std::time_t, Notification*>(it->first,it->second)); // Add the found Notifications to the map. 303 304 304 305 return true; 305 306 } 306 307 307 void NotificationManager::createQueue(const std::string& name, const std::string& targets, unsigned int size, unsigned int displayTime) 308 { 309 this->queues_.push_back(new NotificationQueue(name, targets, size, displayTime)); 308 void NotificationManager::loadQueues(void) 309 { 310 new NotificationQueue("all"); 311 } 312 313 void NotificationManager::createQueue(const std::string& name) 314 { 315 new NotificationQueue(name); 316 } 317 318 NotificationQueue* NotificationManager::getQueue(const std::string & name) 319 { 320 std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); 321 if(it == this->queues_.end()) 322 return NULL; 323 324 return (*it).second; 325 } 326 327 bool NotificationManager::registerQueue(NotificationQueue* queue) 328 { 329 return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second; 330 } 331 332 void NotificationManager::unregisterQueue(NotificationQueue* queue) 333 { 334 this->queues_.erase(queue->getName()); 310 335 } 311 336 -
code/branches/notifications/src/modules/notifications/NotificationManager.h
r7354 r7395 28 28 29 29 /** 30 @file 30 @file NotificationManager.h 31 31 @brief Definition of the NotificationManager class. 32 32 */ … … 40 40 #include <map> 41 41 #include <string> 42 #include <vector>43 42 44 43 #include "util/Singleton.h" … … 55 54 Damian 'Mozork' Frick 56 55 */ 57 class _NotificationsExport NotificationManager 56 class _NotificationsExport NotificationManager // tolua_export 58 57 : public Singleton<NotificationManager>, public OrxonoxClass 59 58 { // tolua_export … … 75 74 void unregisterListener(NotificationListener* listener); //!< Unregisters a NotificationListener withing the NotificationManager. 76 75 77 bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd); //!< Returns the Notifications for a specific NotificationListener in a specified timeframe.76 bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd); //!< Returns the Notifications for a specific NotificationListener in a specified timeframe. 78 77 79 78 /** … … 84 83 @return Returns true if successful. 85 84 */ 86 bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, int timeDelay)85 bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, int timeDelay) 87 86 { return this->getNotifications(listener, map, std::time(0)-timeDelay, std::time(0)); } 88 87 89 88 void enterEditMode(void); 90 89 91 void createQueue(const std::string& name, const std::string& targets, unsigned int size, unsigned int displayTime); // tolua_export 90 // tolua_begin 91 void loadQueues(void); 92 93 void createQueue(const std::string& name); 94 orxonox::NotificationQueue* getQueue(const std::string & name); 95 // tolua_end 96 97 bool registerQueue(NotificationQueue* queue); 98 void unregisterQueue(NotificationQueue* queue); 92 99 93 100 private: 94 101 static NotificationManager* singletonPtr_s; 95 102 96 std:: vector<NotificationQueue*> queues_; //!< The list of NotificationQueues created by the NotificationManager.103 std::map<const std::string, NotificationQueue*> queues_; //!< The list of NotificationQueues created by the NotificationManager. 97 104 98 105 int highestIndex_; //!< This variable holds the highest index (resp. key) in notificationLists_s, to secure that no key appears twice. 99 106 100 std::multimap<std::time_t, Notification*> allNotificationsList_; //!< Container where all notifications are stored.101 std::map<NotificationListener*, int> listenerList_; //!< Container where all NotificationListeners are stored with a number as identifier.102 std::map<int,std::multimap<std::time_t, Notification*>*> notificationLists_; //!< Container where all Notifications, for each identifier (associated with a NotificationListener), are stored.107 std::multimap<std::time_t, Notification*> allNotificationsList_; //!< Container where all notifications are stored. 108 std::map<NotificationListener*, int> listenerList_; //!< Container where all NotificationListeners are stored with a number as identifier. 109 std::map<int,std::multimap<std::time_t, Notification*>*> notificationLists_; //!< Container where all Notifications, for each identifier (associated with a NotificationListener), are stored. 103 110 std::map<Notification*, unsigned int> listenerCounter_; //!< A container to store the number of NotificationListeners a Notification is registered with. 104 111 -
code/branches/notifications/src/modules/notifications/NotificationQueue.cc
r7354 r7395 35 35 36 36 #include <map> 37 #include <sstream> 37 38 38 39 #include "core/CoreIncludes.h" … … 62 63 this->setDisplayTime(displayTime); 63 64 65 bool queueRegistered = NotificationManager::getInstance().registerQueue(this); 66 this->registered_ = true; 67 if(!queueRegistered) 68 { 69 this->registered_ = false; 70 COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl; 71 return; 72 } 73 64 74 this->create(); 65 66 NotificationManager::getInstance().registerListener(this); 67 this->registered_ = true; 75 76 bool listenerRegistered = NotificationManager::getInstance().registerListener(this); 77 if(!listenerRegistered) 78 { 79 this->registered_ = false; 80 NotificationManager::getInstance().unregisterQueue(this); 81 COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl; 82 return; 83 } 68 84 69 85 COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; … … 77 93 { 78 94 this->targets_.clear(); 79 this->clear();80 95 81 96 if(this->registered_) 97 { 98 this->clear(); 99 82 100 NotificationManager::getInstance().unregisterListener(this); 101 NotificationManager::getInstance().unregisterQueue(this); 102 } 83 103 } 84 104 … … 100 120 void NotificationQueue::create(void) 101 121 { 102 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");122 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")"); 103 123 } 104 124 … … 144 164 } 145 165 146 if(notifications->empty()) 147 return; 148 149 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) // Add all Notifications. 150 this->push(it->second, it->first); 166 if(!notifications->empty()) 167 { 168 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) // Add all Notifications. 169 { 170 this->push(it->second, it->first); 171 } 172 } 151 173 152 174 delete notifications; 153 175 154 COUT( 4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl;176 COUT(3) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; //TODO: Level 4. 155 177 } 156 178 … … 167 189 this->push(notification, time); 168 190 169 COUT( 4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl;191 COUT(3) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; //TODO: Level 4. 170 192 } 171 193 … … 253 275 bool NotificationQueue::setName(const std::string& name) 254 276 { 255 //TODO: Test uniqueness of name.256 277 this->name_ = name; 257 278 return true; … … 268 289 void NotificationQueue::setMaxSize(unsigned int size) 269 290 { 291 if(this->maxSize_ == size) 292 return; 293 270 294 this->maxSize_ = size; 271 295 this->sizeChanged(); … … 292 316 void NotificationQueue::setDisplayTime(unsigned int time) 293 317 { 318 if(this->displayTime_ == time) 319 return; 320 294 321 this->displayTime_ = time; 295 this->update(); 322 323 if(this->registered_) 324 this->update(); 296 325 } 297 326 … … 299 328 @brief 300 329 Produces all targets concatinated as string, with kommas (',') as seperators. 301 @param string 302 Pointer to a string which will be used by the method to fill with the concatination of the targets. 303 @return 304 Returns true if successful. 305 */ 306 bool NotificationQueue::getTargets(std::string* string) const 307 { 308 if(string == NULL) 309 { 310 COUT(4) << "Input string must have memory allocated." << std::endl; 311 return false; 312 } 313 string->clear(); 330 @return 331 Returns the targets as a string. 332 */ 333 const std::string& NotificationQueue::getTargets(void) const 334 { 335 std::stringstream stream; 314 336 bool first = true; 315 for(std::set<std::string >::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) // Iterate through the set of targets.337 for(std::set<std::string, NotificationListenerStringCompare>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) // Iterate through the set of targets. 316 338 { 317 339 if(!first) 318 *string +=',';340 stream << ','; 319 341 else 320 342 first = false; 321 *string +=*it;322 } 323 324 return true;343 stream << *it; 344 } 345 346 return *(new std::string(stream.str())); 325 347 } 326 348 … … 352 374 } 353 375 376 if(this->registered_) 377 { 378 NotificationManager::getInstance().unregisterListener(this); 379 NotificationManager::getInstance().registerListener(this); 380 } 381 354 382 return true; 355 383 } -
code/branches/notifications/src/modules/notifications/NotificationQueue.h
r7354 r7395 46 46 #include "NotificationManager.h" 47 47 48 namespace orxonox 49 { 48 namespace orxonox // tolua_export 49 { // tolua_export 50 50 51 51 //! Container to allow easy handling. … … 68 68 Damian 'Mozork' Frick 69 69 */ 70 class _NotificationsExport NotificationQueue : public Tickable, public NotificationListener 71 { 70 class _NotificationsExport NotificationQueue // tolua_export 71 : public Tickable, public NotificationListener 72 { // tolua_export 72 73 73 74 public: … … 80 81 void update(Notification* notification, const std::time_t & time); //!< Adds a Notification to the queue. 81 82 83 // tolua_begin 82 84 /** 83 85 @brief Get the name of the NotificationQueue. … … 87 89 { return this->name_; } 88 90 91 void setMaxSize(unsigned int size); //!< Sets the maximum number of displayed Notifications. 89 92 /** 90 93 @brief Returns the maximum number of Notifications displayed. … … 93 96 inline unsigned int getMaxSize() const 94 97 { return this->maxSize_; } 98 99 void setDisplayTime(unsigned int time); //!< Sets the maximum number of seconds a Notification is displayed. 100 /** 101 @brief Returns the time interval the Notification is displayed. 102 @return Returns the display time. 103 */ 104 inline float getDisplayTime() const 105 { return this->displayTime_; } 106 // tolua_end 107 95 108 /** 96 109 @brief Returns the current number of Notifications displayed. … … 99 112 inline unsigned int getSize() const 100 113 { return this->size_; } 101 /**102 @brief Returns the time interval the Notification is displayed.103 @return Returns the display time.104 */105 inline float getDisplayTime() const106 { return this->displayTime_; }107 114 108 115 /** … … 110 117 @return Retuns a set of string holding the different targets. 111 118 */ 112 inline const std::set<std::string > & getTargetsSet()119 inline const std::set<std::string, NotificationListenerStringCompare> & getTargetsSet() 113 120 { return this->targets_; } 114 bool getTargets(std::string* string) const; //!< Returns a string consisting of the concatination of the targets. 121 122 // tolua_begin 123 bool setTargets(const std::string & targets); //!< Set the targets of this NotificationQueue. 124 const std::string& getTargets(void) const; //!< Returns a string consisting of the concatination of the targets. 125 // tolua_end 115 126 116 127 private: … … 124 135 unsigned int displayTime_; //!< The time a Notification is displayed. 125 136 126 std::set<std::string > targets_; //!< The targets the Queue displays Notifications of.137 std::set<std::string, NotificationListenerStringCompare> targets_; //!< The targets the Queue displays Notifications of. 127 138 128 139 std::multiset<NotificationContainer*, NotificationContainerCompare> ordering_; //!< The NotificationContainers ordered by the time they were registered. //TODO: Would set work as well? … … 139 150 bool setName(const std::string& name); //!< Sets the name of the NotificationQueue. 140 151 141 void setMaxSize(unsigned int size); //!< Sets the maximum number of displayed Notifications.142 void setDisplayTime(unsigned int time); //!< Sets the maximum number of seconds a Notification is displayed.143 144 bool setTargets(const std::string & targets); //!< Set the targets of this NotificationQueue.145 146 152 void sizeChanged(void); //!< Adjusts the NotificationQueue, when the maximum size has changed. 147 153 … … 152 158 void clear(void); //!< Clears the queue by removing all Notifications. 153 159 154 }; 160 }; // tolua_export 155 161 156 } 162 } // tolua_export 157 163 158 164 #endif /* _NotificationOverlay_H__ */ -
code/branches/notifications/src/orxonox/interfaces/NotificationListener.h
r7163 r7395 49 49 /** 50 50 @brief 51 Struct that overloads the compare operation between two PickupIdentifier pointers. 52 */ 53 //TODO: 54 struct NotificationListenerStringCompare 55 { 56 bool operator() (const std::string& lhs, const std::string& rhs) const 57 { return lhs.compare(rhs) < 0; } 58 }; 59 60 /** 61 @brief 51 62 NotificationListener interface. 52 63 @author … … 59 70 virtual ~NotificationListener() {} 60 71 61 virtual const std::set<std::string > & getTargetsSet() = 0;72 virtual const std::set<std::string, NotificationListenerStringCompare> & getTargetsSet() = 0; 62 73 virtual void update(void) = 0; 63 74 virtual void update(Notification* notification, const std::time_t & time) = 0;
Note: See TracChangeset
for help on using the changeset viewer.