percentdonelistener.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <iostream>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 
00032 #include "percentdonelistener.h"
00033 
00034 namespace FIFE
00035 {
00036     const uint32_t minPercent = 0;
00037     const uint32_t maxPercent = 100;
00038 
00039     PercentDoneListener::~PercentDoneListener() {
00040 
00041     }
00042 
00043     PercentDoneCallback::PercentDoneCallback()
00044     : m_totalElements(0), m_percent(1), m_numberOfEvents(0), m_count(0) {
00045 
00046     }
00047 
00048     PercentDoneCallback::~PercentDoneCallback() {
00049 
00050     }
00051 
00052     void PercentDoneCallback::setTotalNumberOfElements(unsigned int totalElements)
00053     {
00054         m_totalElements = totalElements;
00055     }
00056 
00057     void PercentDoneCallback::setPercentDoneInterval(unsigned int percent)
00058     {
00059         m_percent = percent;
00060     }
00061 
00062     void PercentDoneCallback::incrementCount() {
00063 
00064         if (m_count == minPercent) {
00065             // go ahead and fire event just to tell clients we are starting
00066             fireEvent(minPercent);
00067         }
00068 
00069         // increment count
00070         ++m_count;
00071 
00072         // only go through the effort of figuring out percent done if we have listeners
00073         // and we have a total number of elements greater than 0
00074         if (!m_listeners.empty() && m_totalElements > 0) {
00075             if (m_count >= m_totalElements) {
00076                 fireEvent(maxPercent);
00077             }
00078             else {
00079                 // calculate percent done
00080                 uint32_t percentDone = static_cast<uint32_t>((static_cast<float>(m_count)/m_totalElements) * maxPercent);
00081 
00082                 if ((percentDone % m_percent) == 0 && (percentDone != m_percent * m_numberOfEvents)) {
00083                     // keep track of how many times event has occurred
00084                     ++m_numberOfEvents;
00085 
00086                     // alert listeners of event
00087                     fireEvent(m_percent * m_numberOfEvents);
00088                 }
00089             }
00090         }
00091     }
00092 
00093     void PercentDoneCallback::reset() {
00094         m_totalElements = 0;
00095         m_count = 0;
00096         m_numberOfEvents = 0;
00097 
00098         // send event to alert of the reset
00099         fireEvent(minPercent);
00100     }
00101 
00102     void PercentDoneCallback::addListener(PercentDoneListener* listener) {
00103         if (listener) {
00104             m_listeners.push_back(listener);
00105         }
00106     }
00107 
00108     void PercentDoneCallback::removeListener(PercentDoneListener* listener) {
00109         ListenerContainer::iterator iter = m_listeners.begin();
00110         for ( ; iter != m_listeners.end(); ++iter) {
00111             if (*iter == listener) {
00112                 m_listeners.erase(iter);
00113                 break;
00114             }
00115         }
00116     }
00117 
00118     void PercentDoneCallback::fireEvent(uint32_t percent) {
00119         ListenerContainer::iterator iter = m_listeners.begin();
00120         for ( ; iter != m_listeners.end(); ++iter) {
00121             (*iter)->OnEvent(percent);
00122         }
00123     }
00124 }