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_Millisecond_hpp
38 #define anna_core_util_Millisecond_hpp
44 #include <anna/core/util/defines.hpp>
45 #include <anna/core/RuntimeException.hpp>
59 Millisecond() : a_value(0) {;}
63 * \param value Valor inicial de esta instancia.
65 explicit Millisecond(const type_t value) : a_value(value) {;}
69 * \param other Instancia de la que copiar.
71 Millisecond(const Millisecond& other) : a_value(other.a_value) {;}
75 * \param other Instancia de la que copiar.
77 Millisecond(const Second& other);
81 * \param other Instancia de la que copiar.
83 Millisecond(const Microsecond& other);
86 * Conversor a numérico.
87 * \return El valor asociado a esta instancia.
89 operator type_t () const throw() { return a_value; }
94 type_t& refValue() throw() { return a_value; }
96 Millisecond& operator= (const type_t other) throw() { a_value = other; return *this; }
98 Millisecond& operator= (const Millisecond& other) throw() { a_value = other.a_value; return *this; }
100 Millisecond& operator= (const Second& other) throw();
102 Millisecond& operator= (const Microsecond& other) throw();
104 bool operator== (const Millisecond& other) const throw() { return a_value == other.a_value; }
106 bool operator== (const Second& other) const throw();
108 bool operator== (const Microsecond& other) const throw();
110 bool operator!= (const Millisecond& other) const throw() { return a_value != other.a_value; }
112 bool operator!= (const Second& other) const throw();
114 bool operator!= (const Microsecond& other) const throw();
116 bool operator> (const Millisecond& other) const throw() { return a_value > other.a_value; }
118 bool operator> (const Second& other) const throw();
120 bool operator> (const Microsecond& other) const throw();
122 bool operator< (const Millisecond& other) const throw() { return a_value < other.a_value; }
124 bool operator< (const Second& other) const throw();
126 bool operator< (const Microsecond& other) const throw();
128 bool operator>= (const Millisecond& other) const throw() { return a_value >= other.a_value; }
130 bool operator>= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
132 bool operator>= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
134 bool operator<= (const Millisecond& other) const throw() { return a_value <= other.a_value; }
136 bool operator<= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
138 bool operator<= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
140 Millisecond& operator+= (const Millisecond& other) throw() { a_value += other.a_value; return *this; }
142 Millisecond& operator-= (const Millisecond& other) throw() {(a_value > other.a_value) ? (a_value -= other.a_value) : (a_value = 0); return *this; }
145 * Devuelve el valor asociado a esta instancia.
146 * \return el valor asociado a esta instancia.
148 type_t getValue() const throw() { return a_value; }
151 * Si el valor de esta instancia es positivo devuelve el valor asociado a esta instancia en una estructura de
152 * time \em timeval usada habitualmente para temporizar operaciones a nivel de SO, en otro caso retorna NULL.
153 * \param tv Instancia sobre la que guardar el valor en caso de que se éste tenga un valor positivo.
154 * \return Si el valor de esta instancia es positivo devuelve el valor asociado a esta instancia en una estructura de
155 * time \em timeval usada habitualmente para temporizar operaciones a nivel de SO, en otro caso retorna NULL.
157 timeval* getTimeVal(timeval& tv) const throw();
160 * Devuelve la hora actual de sistema expresada en milisegundos transcurridos desde el 1 de Enero de 1970
161 * \return la hora actual de sistema expresada en milisegundos transcurridos desde el 1 de Enero de 1970
163 static Millisecond getTime() throw();
166 * Devuelve una cadena con el valor de esta instancia y las unidades "ms".
167 * \return una cadena con el valor de esta instancia y las unidades "ms".
169 std::string asString() const throw();
172 * Obtiene los microsegundos del valor contenido en la cadena recibida como parámetro.
173 * \param value Cadena que contiene los microsegundos habrá sido obtenida con #asString.
174 * \return los microsegundos del valor contenido en la cadena recibida como parámetro.
176 static Millisecond fromString(const std::string& value) throw(RuntimeException);
182 friend class Microsecond;
184 friend class Millisecond operator / (const Millisecond& left, const Millisecond& right) throw();
185 friend class Millisecond operator + (const Millisecond& left, const Millisecond& right) throw();
186 friend class Millisecond operator - (const Millisecond& left, const Millisecond& right) throw();
187 friend class Millisecond operator / (const Millisecond& left, const int right) throw();
188 friend class Millisecond operator / (const Millisecond& left, const unsigned int right) throw();
189 friend class Millisecond operator *(const Millisecond& left, const int right) throw();
192 inline Millisecond operator / (const Millisecond& left, const Millisecond& right)
194 return Millisecond(left.a_value / right.a_value);
197 inline Millisecond operator + (const Millisecond& left, const Millisecond& right)
199 return Millisecond(left.a_value + right.a_value);
202 inline Millisecond operator - (const Millisecond& left, const Millisecond& right)
204 return Millisecond(left.a_value - right.a_value);
207 inline Millisecond operator / (const Millisecond& left, const int right)
209 return Millisecond(left.a_value / right);
212 inline Millisecond operator / (const Millisecond& left, const unsigned int right)
214 return Millisecond(left.a_value / right);
217 inline Millisecond operator *(const Millisecond& left, const int right)
219 return Millisecond(left.a_value * right);