1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_DelayMeter_hpp
38 #define anna_core_util_DelayMeter_hpp
45 Facilita la medicion de los tiempos empleados las distintas partes de nuestra aplicacion.
47 Permite calcular tiempos acumulados como tiempos individuales. Por ejemplo:
51 #include <anna/core/DelayMeter.hpp>
54 DelayMeter <_TimeUnit> meter;
57 _TimeUnit gooTime = meter.getValue ();
60 _TimeUnit goohooTime = meter.setControlPoint ();
63 _TimeUnit jooTime = meter.getValue ();
67 Dónde _TimeUnit podria ser anna::Second, anna::Millisecond, anna::Microsecond
69 template <class _TimeUnit> class DelayMeter {
73 Inicializa la cuenta de temporizacion.
75 DelayMeter() { a_timestamp = _TimeUnit::getTime(); }
79 * Copia la cuenta de utilizacion de la instancia recibida como parametro.
80 * \param other Instancia de la copiar los parametros para calcular el tiempo transcurrido.
82 DelayMeter(const DelayMeter& other) : a_timestamp(other.a_timestamp), a_topReference(other.a_topReference) { ;}
85 * Inicializa la cuenta de temporizacion. Este metodo es invocado automaticamente desde el contructor la clase
86 * por lo que si vamos usar esta instancia para tomar un unica medida no es necesario invocarlo.
87 * \warning Elimina el punto de referencia temporal que puediera haberse establecido con #setTopReference.
88 * \return El número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporizacion.
90 void setControlPoint() throw() {
91 a_timestamp = _TimeUnit::getTime();
96 * Inicializa la cuenta de temporizacion. Este metodo es invocado automaticamente desde el contructor la clase
97 * por lo que si vamos usar esta instancia para tomar un unica medida no es necesario invocarlo.
98 * \warning Elimina el punto de referencia temporal que puediera haberse establecido con #setTopReference.
99 * \return El número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporizacion.
101 * \param timestamp Valor de referencia a establecer.
103 void setControlPoint(const _TimeUnit& timestamp) throw() {
104 a_timestamp = timestamp;
109 * Se da la posiblidad de establecer un punto de referencia temporal de forma
110 * que cuando se invoque a DelayMeter::getValue, el calculo de la diferencia de tiempo
111 * no se hará entre la marca de tiempo y el tiempo actual, sino la marca de
112 * tiempo y ésta marca de referencia.
114 * Esta funcionalidad ha sido requerida para medir el tiempo de ejecución "real"
115 * de tareas que se ejecutan dentro de un thread. Ya que puede pasar un tiempo
116 * indeterminado desde que se termina la tarea MT (momento en el que se establecerá
117 * la marca de tiempo) y el núcleo y demás partes pueden tener conocimiento de que
118 * esa tarea ha sido finalidad.
120 void setTopReference(const _TimeUnit& topReference) throw() { a_topReference = topReference; }
123 * Elimina el punto de referencia temporal.
125 void clearTopReference() throw() { a_topReference = _TimeUnit(0); }
128 * Inicializa el valor del punto de referencia.
130 void clear() throw() { a_timestamp = 0; }
133 * Devuelve el número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporización.
134 * Si se ha establecido un punto de referencia mediante #setTopReference, devolverá la diferencia entre el
135 * el punto de control y la referencia, en otro caso, devolverá la diferencia entre el punto de control y el
137 * \return El número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporizacion.
138 * \warning Si detecta algun fallo devolvera 0.
140 _TimeUnit getValue() const throw() {
141 a_now = (a_topReference == _TimeUnit(0)) ? _TimeUnit::getTime() : a_topReference;
142 return (a_now > a_timestamp) ? (a_now - a_timestamp) : _TimeUnit(0);
146 * Devuelve el número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporización.
147 * Si se ha establecido un punto de referencia mediante #setTopReference, devolverá la diferencia entre el
148 * el punto de control y la referencia, en otro caso, devolverá la diferencia entre el punto de control y el
150 * \param now Valor temporal tomado como referencia.
151 * \return El número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporizacion.
152 * \warning Si detecta algun fallo devolvera 0.
154 _TimeUnit getValue(const _TimeUnit& now) const throw() {
155 return ((a_now = now) > a_timestamp) ? (a_now - a_timestamp) : _TimeUnit(0);
159 * Devuelve el tiempo que se usó como referencia al calcular el retardo en #getValue
160 * \return El tiempo que se usó como referencia al calcular el retardo en #getValue
162 const _TimeUnit& getNow() const throw() { return a_now; }
166 * \param other Instancia de la que copiar.
168 DelayMeter& operator= (const DelayMeter& other) throw() { a_timestamp = other.a_timestamp; a_topReference = other.a_topReference; return *this; }
171 * Compara el retardo acumulado por esta instancia con el valor recibido.
172 * \param left Valor numérico con el comparar.
173 * \return \em true si el retardo acumulado es mayor que el parámetro recibido o \em false en otro caso.
175 bool operator> (const _TimeUnit& left) const throw() { return getValue() > left; }
178 * Compara el retardo acumulado por esta instancia con el valor recibido.
179 * \param left Valor numérico con el comparar.
180 * \return \em true si el retardo acumulado es mayor que el parámetro recibido o \em false en otro caso.
182 bool operator< (const _TimeUnit& left) const throw() { return getValue() < left; }
185 * Devuelve la cadena que muestra el tiempo medido por esta instancia.
186 * \return la cadena que muestra el tiempo medido por esta instancia.
188 std::string asString() const throw() { return getValue().asString(); }
191 * Devuelve la cadena de depuración de esta instancia.
192 * \param whatis Texto con el nombre lógico de esta instancia.
193 * \return la cadena de depuración de esta instancia.
195 std::string asDebugString(const char* whatis) const throw() {
196 std::string result(whatis);
197 result += " { TopReference: ";
198 result += a_topReference.asString();
199 result += " | TimeStamp: ";
200 result += a_timestamp.asString();
201 result += " | Now: ";
202 result += a_now.asString();
203 return result += " }";
207 _TimeUnit a_topReference;
208 _TimeUnit a_timestamp;
209 mutable _TimeUnit a_now;