Remove dynamic exceptions
[anna.git] / include / anna / core / util / Microsecond.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_Microsecond_hpp
10 #define anna_core_util_Microsecond_hpp
11
12 #include <string>
13
14 #include <anna/core/util/defines.hpp>
15 #include <anna/core/RuntimeException.hpp>
16
17 namespace anna {
18
19 class Millisecond;
20 class Second;
21
22 class Microsecond {
23 public:
24   typedef S64 type_t;
25
26   /**
27    * Constructor
28    */
29   Microsecond() : a_value(0) {;}
30
31   /**
32    * Constructor.
33    * \param value Valor inicial de esta instancia.
34    */
35   explicit Microsecond(const type_t value) : a_value(value) {;}
36
37   /**
38    * Constructor copia.
39    * \param other Instancia de la que copiar.
40    */
41   Microsecond(const Microsecond& other) : a_value(other.a_value) {;}
42
43   /**
44    * Constructor copia.
45    * \param other Instancia de la que copiar.
46    */
47   Microsecond(const Millisecond& other);
48
49   /**
50    * Constructor copia.
51    * \param other Instancia de la que copiar.
52    */
53   Microsecond(const Second& other);
54
55   /**
56    * Conversor a numérico.
57    * \return El valor asociado a esta instancia.
58    */
59   operator type_t () const { return a_value; }
60
61   /**
62    * \internal
63    */
64   type_t& refValue() { return a_value; }
65
66   Microsecond& operator= (const Microsecond& other) { a_value = other.a_value; return *this; }
67
68   Microsecond& operator= (const Millisecond& other) ;
69
70   Microsecond& operator= (const Second& other) ;
71
72   bool operator== (const Microsecond& other) const { return a_value == other.a_value; }
73
74   bool operator== (const Millisecond& other) const ;
75
76   bool operator== (const Second& other) const ;
77
78   bool operator!= (const Microsecond& other) const { return a_value != other.a_value; }
79
80   bool operator!= (const Millisecond& other) const ;
81
82   bool operator!= (const Second& other) const ;
83
84   bool operator> (const Microsecond& other) const { return a_value > other.a_value; }
85
86   bool operator> (const Millisecond& other) const ;
87
88   bool operator> (const Second& other) const ;
89
90   bool operator< (const Microsecond& other) const { return a_value < other.a_value; }
91
92   bool operator< (const Millisecond& other) const ;
93
94   bool operator< (const Second& other) const ;
95
96   bool operator>= (const Microsecond& other) const { return a_value >= other.a_value; }
97
98   bool operator>= (const Millisecond& other) const { return (operator==(other) == true) ? true : operator>(other); }
99
100   bool operator>= (const Second& other) const { return (operator==(other) == true) ? true : operator>(other); }
101
102   bool operator<= (const Microsecond& other) const { return a_value <= other.a_value; }
103
104   bool operator<= (const Millisecond& other) const { return (operator==(other) == true) ? true : operator<(other); }
105
106   bool operator<= (const Second& other) const { return (operator==(other) == true) ? true : operator<(other); }
107
108   Microsecond& operator+= (const Microsecond& other) { a_value += other.a_value; return *this; }
109   Microsecond& operator*= (const int& value) { a_value *= value; return *this; }
110
111   /**
112    * Devuelve el valor asociado a esta instancia.
113    * \return el valor asociado a esta instancia.
114    */
115   type_t getValue() const { return a_value; }
116
117   /**
118    * Devuelve la hora actual de sistema expresada en microsegundos transcurridos desde el 1 de Enero de 1970
119    * \return la hora actual de sistema expresada en microsegundos transcurridos desde el 1 de Enero de 1970
120    */
121   static Microsecond getTime() ;
122
123   /**
124    * Devuelve una cadena con el valor de esta instancia y las unidades "us".
125    * \return una cadena con el valor de esta instancia y las unidades "us".
126    */
127   std::string asString() const ;
128
129   /**
130    * Obtiene los microsegundos del valor contenido en la cadena recibida como parámetro.
131    * \param value Cadena que contiene los microsegundos habrá sido obtenida con #asString.
132    * \return los microsegundos del valor contenido en la cadena recibida como parámetro.
133    */
134   static Microsecond fromString(const std::string& value) noexcept(false);
135
136 private:
137   type_t a_value;
138
139   friend class Millisecond;
140   friend class Second;
141
142   friend class Microsecond operator - (const Microsecond& left, const Microsecond& right) ;
143   friend class Microsecond operator + (const Microsecond& left, const Microsecond& right) ;
144   friend class Microsecond operator / (const Microsecond& left, const Microsecond& right) ;
145   friend class Microsecond operator / (const Microsecond& left, const int right) ;
146   friend class Microsecond operator / (const Microsecond& left, const unsigned int right) ;
147 };
148
149 inline Microsecond operator - (const Microsecond& left, const Microsecond& right)
150 {
151   return Microsecond(left.a_value - right.a_value);
152 }
153
154 inline Microsecond operator + (const Microsecond& left, const Microsecond& right)
155 {
156   return Microsecond(left.a_value + right.a_value);
157 }
158
159 inline Microsecond operator / (const Microsecond& left, const Microsecond& right)
160 {
161   return Microsecond(left.a_value / right.a_value);
162 }
163
164 inline Microsecond operator / (const Microsecond& left, const int right)
165 {
166   return Microsecond(left.a_value / right);
167 }
168
169 inline Microsecond operator / (const Microsecond& left, const unsigned int right)
170 {
171   return Microsecond(left.a_value / right);
172 }
173
174 }
175
176 #endif