First commit
[anna.git] / include / anna / core / util / Microsecond.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_util_Microsecond_hpp
38 #define anna_core_util_Microsecond_hpp
39
40 #include <string>
41
42 #include <anna/config/defines.hpp>
43 #include <anna/core/RuntimeException.hpp>
44
45 namespace anna {
46
47 class Millisecond;
48 class Second;
49
50 class Microsecond {
51 public:
52   typedef Integer64 type_t;
53
54   /**
55    * Constructor
56    */
57   Microsecond() : a_value(0) {;}
58
59   /**
60    * Constructor.
61    * \param value Valor inicial de esta instancia.
62    */
63   explicit Microsecond(const type_t value) : a_value(value) {;}
64
65   /**
66    * Constructor copia.
67    * \param other Instancia de la que copiar.
68    */
69   Microsecond(const Microsecond& other) : a_value(other.a_value) {;}
70
71   /**
72    * Constructor copia.
73    * \param other Instancia de la que copiar.
74    */
75   Microsecond(const Millisecond& other);
76
77   /**
78    * Constructor copia.
79    * \param other Instancia de la que copiar.
80    */
81   Microsecond(const Second& other);
82
83   /**
84    * Conversor a numérico.
85    * \return El valor asociado a esta instancia.
86    */
87   operator type_t () const throw() { return a_value; }
88
89   /**
90    * \internal
91    */
92   type_t& refValue() throw() { return a_value; }
93
94   Microsecond& operator= (const Microsecond& other) throw() { a_value = other.a_value; return *this; }
95
96   Microsecond& operator= (const Millisecond& other) throw();
97
98   Microsecond& operator= (const Second& other) throw();
99
100   bool operator== (const Microsecond& other) const throw() { return a_value == other.a_value; }
101
102   bool operator== (const Millisecond& other) const throw();
103
104   bool operator== (const Second& other) const throw();
105
106   bool operator!= (const Microsecond& other) const throw() { return a_value != other.a_value; }
107
108   bool operator!= (const Millisecond& other) const throw();
109
110   bool operator!= (const Second& other) const throw();
111
112   bool operator> (const Microsecond& other) const throw() { return a_value > other.a_value; }
113
114   bool operator> (const Millisecond& other) const throw();
115
116   bool operator> (const Second& other) const throw();
117
118   bool operator< (const Microsecond& other) const throw() { return a_value < other.a_value; }
119
120   bool operator< (const Millisecond& other) const throw();
121
122   bool operator< (const Second& other) const throw();
123
124   bool operator>= (const Microsecond& other) const throw() { return a_value >= other.a_value; }
125
126   bool operator>= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
127
128   bool operator>= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
129
130   bool operator<= (const Microsecond& other) const throw() { return a_value <= other.a_value; }
131
132   bool operator<= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
133
134   bool operator<= (const Second& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
135
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; }
138
139   /**
140    * Devuelve el valor asociado a esta instancia.
141    * \return el valor asociado a esta instancia.
142    */
143   type_t getValue() const throw() { return a_value; }
144
145   /**
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
148    */
149   static Microsecond getTime() throw();
150
151   /**
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".
154    */
155   std::string asString() const throw();
156
157   /**
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.
161    */
162   static Microsecond fromString(const std::string& value) throw(RuntimeException);
163
164 private:
165   type_t a_value;
166
167   friend class Millisecond;
168   friend class Second;
169
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();
175 };
176
177 inline Microsecond operator - (const Microsecond& left, const Microsecond& right)
178 throw() {
179   return Microsecond(left.a_value - right.a_value);
180 }
181
182 inline Microsecond operator + (const Microsecond& left, const Microsecond& right)
183 throw() {
184   return Microsecond(left.a_value + right.a_value);
185 }
186
187 inline Microsecond operator / (const Microsecond& left, const Microsecond& right)
188 throw() {
189   return Microsecond(left.a_value / right.a_value);
190 }
191
192 inline Microsecond operator / (const Microsecond& left, const int right)
193 throw() {
194   return Microsecond(left.a_value / right);
195 }
196
197 inline Microsecond operator / (const Microsecond& left, const unsigned int right)
198 throw() {
199   return Microsecond(left.a_value / right);
200 }
201
202 }
203
204 #endif