Updated license
[anna.git] / include / anna / core / oam / Counter.hpp
1 // ANNA - Anna is Not Nothingness 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_oam_Counter_hpp
38 #define anna_core_oam_Counter_hpp
39
40 #include <anna/core/util/defines.hpp>
41
42 namespace anna {
43
44 namespace oam {
45
46 class CounterScope;
47
48 /**
49    Contador.
50
51    Permite un sistema rapido para registrar situaciones ocurridas en las aplicaciones.
52 */
53 class Counter {
54 public:
55   typedef unsigned int type_t;
56
57   /**
58      Devuelve el nombre logico del contador, que coincidira con el indicado a la hora
59      de crear el contador mediante el metodo CounterScope::create.
60      \return El nombre logico del contador.
61   */
62   const std::string& getName() const throw() { return a_name; }
63
64   /**
65      Devuelve la referencia logica de este contador. Sera el numero resultante de combinar
66      el numero de ambito en el que esta registrado y el numero de contador dentro de este
67      ambito.
68      \return La referencia logica de este contador.
69   */
70   int getReference() const throw();
71
72   /**
73    * Devuelve el valor actual de este contador. Este valor se podrĂ¡ a cero cada vez que los contadores
74    * se graben.
75    * \return El valor actual de este contador.
76    */
77   type_t getValue() const throw() { return a_value; }
78
79   /**
80    * Devuelve el valor acumulado de este contador.
81    * \return El valor acumulado de este contador.
82    */
83   U64 getAccumulatedValue() const throw() { return a_accValue; }
84
85   /**
86      Operador de conversion.
87      \return El valor asociado a este contador.
88   */
89   operator type_t () const throw() { return a_value; }
90
91   /**
92      Inicializa el valor de este contador.
93   */
94   void reset() throw() { a_value = 0; }
95
96   /**
97      Inicializa el valor acumulado de este contador.
98      \return Devuelve 'true' si fue reseteado, 'false' si ya lo estaba
99   */
100   bool resetAcc() throw() { bool result = (a_accValue != 0); a_accValue = 0; return result; }
101
102
103   /**
104      Devuelve una cadena con la informacion relevante de este objeto.
105      \return Una cadena con la informacion relevante de este objeto.
106   */
107   std::string asString() const throw();
108
109 private:
110   CounterScope& a_scope;
111   const int a_id;
112   std::string a_name;
113   type_t a_value;
114   U64 a_accValue;
115
116   Counter(CounterScope& scope, const int id, const char* name);
117   Counter(const Counter&);
118
119   void debug() const throw();
120
121   friend class CounterScope;
122 };
123
124 }
125 }
126
127 #endif
128