Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 10, 2010, 11:17:02 PM (14 years ago)
Author:
dafrick
Message:

Some cleanup and documenting.
After some more extensive testing it seems to work, the code looks ok as well…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/notifications/src/modules/notifications/NotificationManager.cc

    r7398 r7399  
    3939#include "core/LuaState.h"
    4040#include "util/ScopedSingletonManager.h"
     41
    4142#include "interfaces/NotificationListener.h"
     43
    4244#include "Notification.h"
    4345#include "NotificationQueue.h"
     
    5658    ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false);
    5759
    58     //TODO: Make work.
    5960    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
    6061
     
    7071
    7172        ModifyConsoleCommand("enterEditMode").setObject(this);
    72        
     73
    7374        COUT(3) << "NotificatioManager created." << std::endl;
    7475    }
     
    8283        ModifyConsoleCommand("enterEditMode").setObject(NULL);
    8384
     85        //  Destroys all Notifications that have been registered with the NotificationManager.
    8486        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
    8587            it->second->destroy();
    86        
     88
    8789        COUT(3) << "NotificationManager destroyed." << std::endl;
    8890    }
    8991
     92    /**
     93    @brief
     94        Is called before the object is destroyed.
     95    */
    9096    void NotificationManager::preDestroy(void)
    9197    {
     98        // Destroys all NotificationQueues that have been registered with the NotificationManager.
    9299        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); )
    93100        {
     
    109116    bool NotificationManager::registerNotification(Notification* notification)
    110117    {
    111         if(notification == NULL) // A NULL-Notification cannot be registered.
    112             return false;
     118        assert(notification);
    113119
    114120        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
    115121
     122        // Add the Notification to the list that holds all Notifications.
    116123        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
    117124
     
    123130            all = true;
    124131
    125         // Insert the notification in all listeners that have its sender as target.
    126         for(std::map<NotificationListener*,int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all listeners.
     132        // Insert the Notification in all NotificationListeners that have its sender as target.
     133        for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
    127134        {
    128135            std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet();
    129136            bool bAll = set.find(NotificationManager::ALL) != set.end();
    130             if(all || bAll || set.find(notification->getSender()) != set.end()) //TODO: Make sure this works.
     137            // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
     138            if(all || bAll || set.find(notification->getSender()) != set.end())
    131139            {
    132140                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                 }
    136                 it->first->update(notification, time); // Update the listener.
    137                 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(notification);
    138                 if(counterIt == this->listenerCounter_.end())
    139                     this->listenerCounter_[notification] = 1;
    140                 else
    141                     this->listenerCounter_[notification] = counterIt->second + 1;
     141                    this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
     142                it->first->update(notification, time); // Update the NotificationListener.
    142143            }
    143144        }
     
    150151    /**
    151152    @brief
    152         Unregisters a Notification within the NotificationManager.
     153        Unregisters a Notification within the NotificationManager for a given NotificationListener.
    153154    @param notification
    154155        A pointer to the Notification to be unregistered.
     
    161162        assert(listener);
    162163
    163         // If the Notification was removed from the list of Notifications of the input NotificationListener, the counter for the Notification of the number of NotificationListeners it is present in is decremented.
    164         if(this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second)))
    165             this->listenerCounter_[notification] = this->listenerCounter_[notification] - 1;
     164        // Remove the Notification from the list of Notifications of the input NotificationListener.
     165        this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
    166166
    167167        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
     
    170170    /**
    171171    @brief
    172         Helper method that removes an input notification form an input map.
     172        Helper method that removes an input Notification form an input map.
    173173    @param notification
    174         A pointer to the notification to be removed.
     174        A pointer to the Notification to be removed.
    175175    @param map
    176         The map the notification should be removed from.
     176        The map the Notification should be removed from.
    177177    @return
    178178        Returns true if successful.
     
    199199        The NotificationListener to be registered.
    200200    @return
    201         Returns true if successful.
     201        Returns true if successful.  Fales if the NotificationListener is already registered.
    202202    */
    203203    bool NotificationManager::registerListener(NotificationListener* listener)
    204204    {
     205        assert(listener);
     206
     207        // If the NotificationListener is already registered.
     208        if(this->listenerList_.find(listener) != this->listenerList_.end())
     209            return false;
     210
    205211        this->highestIndex_ += 1;
    206         int index = this->highestIndex_;
    207 
    208         this->listenerList_[listener] = index; // Add the NotificationListener to the list of listeners.
     212        unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
     213
     214        this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
    209215
    210216        std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet();
    211217
    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.
     218        // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications.
    213219        bool bAll = set.find(NotificationManager::ALL) != set.end();
    214220        std::multimap<std::time_t, Notification*>* map;
    215221        if(bAll)
    216222            this->notificationLists_[index] = &this->allNotificationsList_;
     223        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
    217224        else
    218225        {
     
    224231        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
    225232        {
    226             if(bAll || set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current notification as target.
    227             {
    228                 if(!bAll)
    229                     map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
    230                 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(it->second);
    231                 if(counterIt == this->listenerCounter_.end())
    232                     this->listenerCounter_[it->second] = 1;
    233                 else
    234                     this->listenerCounter_[it->second] = counterIt->second + 1;
    235             }
     233            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
     234                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
    236235        }
    237236
     
    245244    /**
    246245    @brief
    247         Unregisters a NotificationListener withing the NotificationManager.
     246        Unregisters a NotificationListener within the NotificationManager.
     247    @param listener
     248        The NotificationListener to be unregistered.
    248249    */
    249250    void NotificationManager::unregisterListener(NotificationListener* listener)
     
    251252        assert(listener);
    252253
    253         int identifier = this->listenerList_.find(listener)->second;
     254        //TODO: Make unsigned int.
     255        unsigned int identifier = this->listenerList_.find(listener)->second;
    254256        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
    255257
    256         // If the map is not the map of all notifications, make sure all Notifications are removed and delete it.
     258        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
    257259        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
    258260        if(map != &this->allNotificationsList_)
     
    266268        }
    267269
     270        // Remove the NotificationListener from the list of NotificationListeners.
    268271        this->listenerList_.erase(listener);
     272        // Remove the Notifications list that was associated with the input NotificationListener.
    269273        this->notificationLists_.erase(identifier);
    270274
     
    274278    /**
    275279    @brief
    276         Fetches the Notifications for a specific NotificationListener in a specified timeframe.
     280        Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
    277281    @param listener
    278282        The NotificationListener the Notifications are fetched for.
    279283    @param map
    280         A multimap, in which the notifications are stored.
     284        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
    281285    @param timeFrameStart
    282286        The start time of the timeframe.
     
    286290        Returns true if successful.
    287291    */
    288     bool NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
    289     {
    290         if(listener == NULL || map == NULL)
    291             return false;
    292 
    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;
     292    void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
     293    {
     294        assert(listener);
     295        assert(map);
     296
     297        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
    297298
    298299        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
     300        // Iterators pointing to the bounds specified by the specified start and end times of the time frame.
    299301        itLowest = notifications->lower_bound(timeFrameStart);
    300302        itHighest = notifications->upper_bound(timeFrameEnd);
    301303
    302         for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time Frame to the end of it.
    303             map->insert(std::pair<std::time_t, Notification*>(it->first,it->second)); // Add the found Notifications to the map.
    304 
    305         return true;
    306     }
    307 
    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());
    335     }
    336 
     304        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it.
     305            map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
     306    }
     307
     308    /**
     309    @brief
     310        Enters the edit mode of the NotificationLayer.
     311    */
    337312    void NotificationManager::enterEditMode(void)
    338313    {
     
    342317    }
    343318
     319    /**
     320    @brief
     321        Registers a NotificationQueue.
     322        This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
     323    @param queue
     324        A pointer to the NotificationQueue to be registered.
     325    @return
     326        Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false.
     327    */
     328    bool NotificationManager::registerQueue(NotificationQueue* queue)
     329    {
     330        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
     331    }
     332
     333    /**
     334    @brief
     335        Unregisters a NotificationQueue.
     336    @param queue
     337        A pointer to the NotificationQueue to be unregistered.
     338    */
     339    void NotificationManager::unregisterQueue(NotificationQueue* queue)
     340    {
     341        this->queues_.erase(queue->getName());
     342    }
     343
     344    /**
     345    @brief
     346        Loads all the NotificationQueues that should exist.
     347    */
     348    void NotificationManager::loadQueues(void)
     349    {
     350        new NotificationQueue("all");
     351    }
     352
     353    /**
     354    @brief
     355        Creates a new NotificationQueue.
     356        This is used in lua.
     357    @param name
     358        The name of the new NotificationQueue.
     359    */
     360    void NotificationManager::createQueue(const std::string& name)
     361    {
     362        new NotificationQueue(name);
     363    }
     364
     365    /**
     366    @brief
     367        Get the NotificationQueue with the input name.
     368    @param name
     369        The name of the NotificationQueue.
     370    @return
     371        Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists.
     372    */
     373    NotificationQueue* NotificationManager::getQueue(const std::string & name)
     374    {
     375        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name);
     376        // Returns NULL if no such NotificationQueue exists.
     377        if(it == this->queues_.end())
     378            return NULL;
     379
     380        return (*it).second;
     381    }
     382
    344383}
Note: See TracChangeset for help on using the changeset viewer.