Remove dynamic exceptions
[anna.git] / include / anna / core / util / Millisecond.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_core_util_Millisecond_hpp
10 #define anna_core_util_Millisecond_hpp
11
12 #include <time.h>
13
14 #include <string>
15
16 #include <anna/core/util/defines.hpp>
17 #include <anna/core/RuntimeException.hpp>
18
19 namespace anna {
20
21 class Second;
22 class Microsecond;
23
24 class Millisecond {
25 public:
26   typedef S64 type_t;
27
28   /**
29    * Constructor
30    */
31   Millisecond() : a_value(0) {;}
32
33   /**
34    * Constructor.
35    * \param value Valor inicial de esta instancia.
36    */
37   explicit Millisecond(const type_t value) : a_value(value) {;}
38
39   /**
40    * Constructor copia.
41    * \param other Instancia de la que copiar.
42    */
43   Millisecond(const Millisecond& other) : a_value(other.a_value) {;}
44
45   /**
46    * Constructor copia.
47    * \param other Instancia de la que copiar.
48    */
49   Millisecond(const Second& other);
50
51   /**
52    * Constructor copia.
53    * \param other Instancia de la que copiar.
54    */
55   Millisecond(const Microsecond& other);
56
57   /**
58    * Conversor a numérico.
59    * \return El valor asociado a esta instancia.
60    */
61   operator type_t () const { return a_value; }
62
63   /**
64    * \internal
65    */
66   type_t& refValue() { return a_value; }
67
68   Millisecond& operator= (const type_t other) { a_value = other; return *this; }
69
70   Millisecond& operator= (const Millisecond& other) { a_value = other.a_value; return *this; }
71
72   Millisecond& operator= (const Second& other) ;
73
74   Millisecond& operator= (const Microsecond& other) ;
75
76   bool operator== (const Millisecond& other) const { return a_value == other.a_value; }
77
78   bool operator== (const Second& other) const ;
79
80   bool operator== (const Microsecond& other) const ;
81
82   bool operator!= (const Millisecond& other) const { return a_value != other.a_value; }
83
84   bool operator!= (const Second& other) const ;
85
86   bool operator!= (const Microsecond& other) const ;
87
88   bool operator> (const Millisecond& other) const { return a_value > other.a_value; }
89
90   bool operator> (const Second& other) const ;
91
92   bool operator> (const Microsecond& other) const ;
93
94   bool operator< (const Millisecond& other) const { return a_value < other.a_value; }
95
96   bool operator< (const Second& other) const ;
97
98   bool operator< (const Microsecond& other) const ;
99
100   bool operator>= (const Millisecond& other) const { return a_value >= other.a_value; }
101
102   bool operator>= (const Second& other) const { return (operator==(other) == true) ? true : operator>(other); }
103
104   bool operator>= (const Microsecond& other) const { return (operator==(other) == true) ? true : operator>(other); }
105
106   bool operator<= (const Millisecond& other) const { return a_value <= other.a_value; }
107
108   bool operator<= (const Second& other) const { return (operator==(other) == true) ? true : operator<(other); }
109
110   bool operator<= (const Microsecond& other) const { return (operator==(other) == true) ? true : operator<(other); }
111
112   Millisecond& operator+= (const Millisecond& other) { a_value += other.a_value; return *this; }
113
114   Millisecond& operator-= (const Millisecond& other) {(a_value > other.a_value) ? (a_value -= other.a_value) : (a_value = 0); return *this; }
115
116   /**
117    * Devuelve el valor asociado a esta instancia.
118    * \return el valor asociado a esta instancia.
119    */
120   type_t getValue() const { return a_value; }
121
122   /**
123    * Si el valor de esta instancia es positivo devuelve el valor asociado a esta instancia en una estructura de
124    * time \em timeval usada habitualmente para temporizar operaciones a nivel de SO, en otro caso retorna NULL.
125    * \param tv Instancia sobre la que guardar el valor en caso de que se éste tenga un valor positivo.
126    * \return Si el valor de esta instancia es positivo devuelve el valor asociado a esta instancia en una estructura de
127    * time \em timeval usada habitualmente para temporizar operaciones a nivel de SO, en otro caso retorna NULL.
128    */
129   timeval* getTimeVal(timeval& tv) const ;
130
131   /**
132    * Devuelve la hora actual de sistema expresada en milisegundos transcurridos desde el 1 de Enero de 1970
133    * \return la hora actual de sistema expresada en milisegundos transcurridos desde el 1 de Enero de 1970
134    */
135   static Millisecond getTime() ;
136
137   /**
138    * Devuelve una cadena con el valor de esta instancia y las unidades "ms".
139    * \return una cadena con el valor de esta instancia y las unidades "ms".
140    */
141   std::string asString() const ;
142
143   /**
144    * Obtiene los microsegundos del valor contenido en la cadena recibida como parámetro.
145    * \param value Cadena que contiene los microsegundos habrá sido obtenida con #asString.
146    * \return los microsegundos del valor contenido en la cadena recibida como parámetro.
147    */
148   static Millisecond fromString(const std::string& value) noexcept(false);
149
150 private:
151   type_t a_value;
152
153   friend class Second;
154   friend class Microsecond;
155
156   friend class Millisecond operator / (const Millisecond& left, const Millisecond& right) ;
157   friend class Millisecond operator + (const Millisecond& left, const Millisecond& right) ;
158   friend class Millisecond operator - (const Millisecond& left, const Millisecond& right) ;
159   friend class Millisecond operator / (const Millisecond& left, const int right) ;
160   friend class Millisecond operator / (const Millisecond& left, const unsigned int right) ;
161   friend class Millisecond operator *(const Millisecond& left, const int right) ;
162 };
163
164 inline Millisecond operator / (const Millisecond& left, const Millisecond& right)
165 {
166   return Millisecond(left.a_value / right.a_value);
167 }
168
169 inline Millisecond operator + (const Millisecond& left, const Millisecond& right)
170 {
171   return Millisecond(left.a_value + right.a_value);
172 }
173
174 inline Millisecond operator - (const Millisecond& left, const Millisecond& right)
175 {
176   return Millisecond(left.a_value - right.a_value);
177 }
178
179 inline Millisecond operator / (const Millisecond& left, const int right)
180 {
181   return Millisecond(left.a_value / right);
182 }
183
184 inline Millisecond operator / (const Millisecond& left, const unsigned int right)
185 {
186   return Millisecond(left.a_value / right);
187 }
188
189 inline Millisecond operator *(const Millisecond& left, const int right)
190 {
191   return Millisecond(left.a_value * right);
192 }
193
194 }
195
196 #endif