1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_mt_SafeSortedVector_hpp
38 #define anna_core_mt_SafeSortedVector_hpp
42 #include <anna/core/mt/Mutex.hpp>
43 #include <anna/core/mt/Guard.hpp>
44 #include <anna/core/util/SortedVector.hpp>
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.
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
58 template < typename T, typename SortBy, typename TKey = int >
59 class SafeSortedVector : public SortedVector <T, SortBy, TKey>, public Mutex {
64 SafeSortedVector() : SortedVector <T, SortBy, TKey> () {;}
68 \param other Instancia de la que copiar.
70 explicit SafeSortedVector(const SafeSortedVector& other) : SortedVector <T, SortBy, TKey> (other) {}
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.
79 bool contains(const T* t) const
84 Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::contains");
85 return SortedVector <T, SortBy, TKey>::contains(t);
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.
94 throw(RuntimeException) {
98 Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::add");
99 return SortedVector <T, SortBy, TKey>::add(t);
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.
108 throw(RuntimeException) {
112 Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::erase");
113 return SortedVector <T, SortBy, TKey>::erase(t);
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.
121 T* find(const TKey key)
123 Guard guard(this, "SafeSortedVector <T, SortBy, TKey>::find");
124 return SortedVector <T, SortBy, TKey>::find(key);
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.
132 const T* find(const TKey key) const throw() {
133 return const_cast <SafeSortedVector <T, SortBy, TKey>*>(this)->find(key);