1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 Google Inc. 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_timex_Meter_hpp
38 #define anna_timex_Meter_hpp
40 #include <anna/core/functions.hpp>
47 Facilita la medicion de los tiempos empleados las distintas partes de nuestra aplicacion.
49 Permite calcular tiempos acumulados como tiempos individuales. Por ejemplo:
53 #include <anna.timex.Meter.h>
59 Millisecond gooTime = meter.getDelay ();
62 Millisecond goohooTime = meter.setControlPoint ();
65 Millisecond jooTime = meter.getDelay ();
73 Inicializa la cuenta de temporizacion.
75 Meter() { a_timestamp = functions::millisecond(); a_topReference = 0; }
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 Meter(const Meter& other) : a_timestamp(other.a_timestamp) { a_topReference = 0; ;}
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 Millisecond setControlPoint() throw() {
91 Millisecond now = functions::millisecond();
92 Millisecond result = (now > a_timestamp) ? (now - a_timestamp) : (Millisecond)0;
99 * Se da la posiblidad de establecer un punto de referencia temporal de forma
100 * que cuando se invoque a Meter::getDelay, el calculo de la diferencia de tiempo
101 * no se hará entre la marca de tiempo y el tiempo actual, sino la marca de
102 * tiempo y ésta marca de referencia.
104 * Esta funcionalidad ha sido requerida para medir el tiempo de ejecución "real"
105 * de tareas que se ejecutan dentro de un thread. Ya que puede pasar un tiempo
106 * indeterminado desde que se termina la tarea MT (momento en el que se establecerá
107 * la marca de tiempo) y el núcleo y demás partes pueden tener conocimiento de que
108 * esa tarea ha sido finalidad.
111 void setTopReference(const Millisecond & topReference) throw() { a_topReference = topReference; }
114 * Elimina el punto de referencia temporal.
116 void clearTopReference() throw() { a_topReference = 0; }
119 * Devuelve el número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporización.
120 * Si se ha establecido un punto de referencia mediante #setTopReference, devolverá la diferencia entre el
121 * el punto de control y la referencia, en otro caso, devolverá la diferencia entre el punto de control y el
123 * \return El número de milisegundos transcurridos desde la última vez que inicializamos la cuenta de temporizacion.
124 * \warning Si detecta algun fallo devolvera 0.
126 Millisecond getDelay() const throw() {
127 Millisecond now = (a_topReference == 0) ? functions::millisecond() : a_topReference;
128 return (now > a_timestamp) ? (now - a_timestamp) : (Millisecond)0;
133 * \param timestamp Milisegundo en el que inicilizar la cuenta de esta instancia.
134 * \warning Elimina el punto de referencia temporal que puediera haberse establecido con #setTopReference.
135 * \see anna::functions::millisecond.
137 Meter& operator= (const Millisecond & timestamp) throw() { a_timestamp = timestamp; a_topReference = 0; return *this; }
141 * \param other Instancia de la que copiar.
143 Meter& operator= (const Meter& other) throw() { a_timestamp = other.a_timestamp; a_topReference = other.a_topReference; return *this; }
146 Millisecond a_topReference;
147 Millisecond a_timestamp;