FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
percentdonelistener.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <iostream>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 
32 #include "percentdonelistener.h"
33 
34 namespace FIFE
35 {
36  const uint32_t minPercent = 0;
37  const uint32_t maxPercent = 100;
38 
40 
41  }
42 
44  : m_totalElements(0), m_percent(1), m_numberOfEvents(0), m_count(0) {
45 
46  }
47 
49 
50  }
51 
52  void PercentDoneCallback::setTotalNumberOfElements(unsigned int totalElements)
53  {
54  m_totalElements = totalElements;
55  }
56 
58  {
59  m_percent = percent;
60  }
61 
63 
64  if (m_count == minPercent) {
65  // go ahead and fire event just to tell clients we are starting
66  fireEvent(minPercent);
67  }
68 
69  // increment count
70  ++m_count;
71 
72  // only go through the effort of figuring out percent done if we have listeners
73  // and we have a total number of elements greater than 0
74  if (!m_listeners.empty() && m_totalElements > 0) {
75  if (m_count >= m_totalElements) {
76  fireEvent(maxPercent);
77  }
78  else {
79  // calculate percent done
80  uint32_t percentDone = static_cast<uint32_t>((static_cast<float>(m_count)/m_totalElements) * maxPercent);
81 
82  if ((percentDone % m_percent) == 0 && (percentDone != m_percent * m_numberOfEvents)) {
83  // keep track of how many times event has occurred
85 
86  // alert listeners of event
87  fireEvent(m_percent * m_numberOfEvents);
88  }
89  }
90  }
91  }
92 
94  m_totalElements = 0;
95  m_count = 0;
96  m_numberOfEvents = 0;
97 
98  // send event to alert of the reset
99  fireEvent(minPercent);
100  }
101 
103  if (listener) {
104  m_listeners.push_back(listener);
105  }
106  }
107 
109  ListenerContainer::iterator iter = m_listeners.begin();
110  for ( ; iter != m_listeners.end(); ++iter) {
111  if (*iter == listener) {
112  m_listeners.erase(iter);
113  break;
114  }
115  }
116  }
117 
119  ListenerContainer::iterator iter = m_listeners.begin();
120  for ( ; iter != m_listeners.end(); ++iter) {
121  (*iter)->OnEvent(percent);
122  }
123  }
124 }
void fireEvent(uint32_t percent)
void removeListener(PercentDoneListener *listener)
void setPercentDoneInterval(unsigned int percent)
const uint32_t minPercent
const uint32_t maxPercent
void setTotalNumberOfElements(unsigned int totalElements)
void addListener(PercentDoneListener *listener)
unsigned int uint32_t
Definition: core.h:40