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_Microsecond_hpp
38 #define anna_core_util_Microsecond_hpp
42 #include <anna/core/util/defines.hpp>
43 #include <anna/core/RuntimeException.hpp>
57 Microsecond() : a_value(0) {;}
61 * \param value Valor inicial de esta instancia.
63 explicit Microsecond(const type_t value) : a_value(value) {;}
67 * \param other Instancia de la que copiar.
69 Microsecond(const Microsecond& other) : a_value(other.a_value) {;}
73 * \param other Instancia de la que copiar.
75 Microsecond(const Millisecond& other);
79 * \param other Instancia de la que copiar.
81 Microsecond(const Second& other);
84 * Conversor a numérico.
85 * \return El valor asociado a esta instancia.
87 operator type_t () const throw() { return a_value; }
92 type_t& refValue() throw() { return a_value; }
94 Microsecond& operator= (const Microsecond& other) throw() { a_value = other.a_value; return *this; }
96 Microsecond& operator= (const Millisecond& other) throw();
98 Microsecond& operator= (const Second& other) throw();
100 bool operator== (const Microsecond& other) const throw() { return a_value == other.a_value; }
102 bool operator== (const Millisecond& other) const throw();
104 bool operator== (const Second& other) const throw();
106 bool operator!= (const Microsecond& other) const throw() { return a_value != other.a_value; }
108 bool operator!= (const Millisecond& other) const throw();
110 bool operator!= (const Second& other) const throw();
112 bool operator> (const Microsecond& other) const throw() { return a_value > other.a_value; }
114 bool operator> (const Millisecond& other) const throw();
116 bool operator> (const Second& other) const throw();
118 bool operator< (const Microsecond& other) const throw() { return a_value < other.a_value; }
120 bool operator< (const Millisecond& other) const throw();
122 bool operator< (const Second& other) const throw();
124 bool operator>= (const Microsecond& other) const throw() { return a_value >= other.a_value; }
126 bool operator>= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
128 bool operator>= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
130 bool operator<= (const Microsecond& other) const throw() { return a_value <= other.a_value; }
132 bool operator<= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
134 bool operator<= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
136 Microsecond& operator+= (const Microsecond& other) throw() { a_value += other.a_value; return *this; }
137 Microsecond& operator*= (const int& value) throw() { a_value *= value; return *this; }
140 * Devuelve el valor asociado a esta instancia.
141 * \return el valor asociado a esta instancia.
143 type_t getValue() const throw() { return a_value; }
146 * Devuelve la hora actual de sistema expresada en microsegundos transcurridos desde el 1 de Enero de 1970
147 * \return la hora actual de sistema expresada en microsegundos transcurridos desde el 1 de Enero de 1970
149 static Microsecond getTime() throw();
152 * Devuelve una cadena con el valor de esta instancia y las unidades "us".
153 * \return una cadena con el valor de esta instancia y las unidades "us".
155 std::string asString() const throw();
158 * Obtiene los microsegundos del valor contenido en la cadena recibida como parámetro.
159 * \param value Cadena que contiene los microsegundos habrá sido obtenida con #asString.
160 * \return los microsegundos del valor contenido en la cadena recibida como parámetro.
162 static Microsecond fromString(const std::string& value) throw(RuntimeException);
167 friend class Millisecond;
170 friend class Microsecond operator - (const Microsecond& left, const Microsecond& right) throw();
171 friend class Microsecond operator + (const Microsecond& left, const Microsecond& right) throw();
172 friend class Microsecond operator / (const Microsecond& left, const Microsecond& right) throw();
173 friend class Microsecond operator / (const Microsecond& left, const int right) throw();
174 friend class Microsecond operator / (const Microsecond& left, const unsigned int right) throw();
177 inline Microsecond operator - (const Microsecond& left, const Microsecond& right)
179 return Microsecond(left.a_value - right.a_value);
182 inline Microsecond operator + (const Microsecond& left, const Microsecond& right)
184 return Microsecond(left.a_value + right.a_value);
187 inline Microsecond operator / (const Microsecond& left, const Microsecond& right)
189 return Microsecond(left.a_value / right.a_value);
192 inline Microsecond operator / (const Microsecond& left, const int right)
194 return Microsecond(left.a_value / right);
197 inline Microsecond operator / (const Microsecond& left, const unsigned int right)
199 return Microsecond(left.a_value / right);