First commit
[anna.git] / include / anna / core / mt / SafeSortedVector.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_mt_SafeSortedVector_hpp
38 #define anna_core_mt_SafeSortedVector_hpp
39
40 #include <vector>
41
42 #include <anna/core/mt/Mutex.hpp>
43 #include <anna/core/mt/Guard.hpp>
44 #include <anna/core/util/SortedVector.hpp>
45
46 namespace anna {
47
48 /**
49    Patron para ordenar instancias de objetos en base de una clave. El acceso a la lista de
50    datos se realiza desde secciones criticas.
51
52    \param T Clase del patron.
53    \param SortBy Clase que ofrece el valor por el que ordenar. Debe implementar un metodo constante con la
54     signatura: TKey value (const T*)
55    \param TKey Tipo de clave usado para calcular la ordenacion. Debe implementar los operadores '=', '<' y '==' y el
56    contructor copia.
57 */
58 template < typename T, typename SortBy, typename TKey = int >
59 class SafeSortedVector : public SortedVector <T, SortBy, TKey>, public Mutex {
60 public:
61   /**
62      Constructor.
63   */
64   SafeSortedVector() : SortedVector <T, SortBy, TKey> () {;}
65
66   /**
67      Constructor copia.
68      \param other Instancia de la que copiar.
69   */
70   explicit SafeSortedVector(const SafeSortedVector& other) : SortedVector <T, SortBy, TKey> (other) {}
71
72   /**
73      Devolvera \em true si la instancia recibida como parametro esta contenido en el
74      vector o \em en otro caso. Si la instancia recibida es NULL siempre devolvera \em false.
75      \param t Instancia a comprobar.
76      \return \em true si la instancia recibida como parametro esta contenido en el
77      vector o \em en otro caso.
78   */
79   bool contains(const T* t) const
80   throw() {
81     if(t == NULL)
82       return false;
83
84     Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::contains");
85     return  SortedVector <T, SortBy, TKey>::contains(t);
86   }
87
88   /**
89      Incorpora la instancia recibida como parametro en la lista ordenada de objetos.
90      \param t Instancia a guardar en el vector. Si es NULL la operacion no tendra ningun efecto.
91      \return \em true si ha registrado la nueva instancia o \em false en otro caso.
92   */
93   bool add(T* t)
94   throw(RuntimeException) {
95     if(t == NULL)
96       return false;
97
98     Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::add");
99     return SortedVector <T, SortBy, TKey>::add(t);
100   }
101
102   /**
103      Elimina la instancia recibida como parametro de la lista ordenada de objetos.
104      \param t Instancia a guardar en el vector. Si es NULL la operacion no tendra ningun efecto.
105      \return \em true si ha eliminado la instancia o \em false en otro caso.
106   */
107   bool erase(T* t)
108   throw(RuntimeException) {
109     if(t == NULL)
110       return false;
111
112     Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::erase");
113     return SortedVector <T, SortBy, TKey>::erase(t);
114   }
115
116   /**
117      Devuelve la instancia asociada a la clave recibida como parametro o NULL si no existe.
118      \param key Clave a buscar en el vector.
119      \return la instancia asociada a la clave recibida como parametro o NULL si no existe.
120   */
121   T* find(const TKey key)
122   throw() {
123     Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::find");
124     return SortedVector <T, SortBy, TKey>::find(key);
125   }
126
127   /**
128      Devuelve la instancia asociada a la clave recibida como parametro o NULL si no existe.
129      \param key Clave a buscar en el vector.
130      \return la instancia asociada a la clave recibida como parametro o NULL si no existe.
131   */
132   const T* find(const TKey key) const throw() {
133     return const_cast <SafeSortedVector <T, SortBy, TKey>*>(this)->find(key);
134   }
135 };
136
137 }
138
139 #endif