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_util_CommaSeparatedList_hpp
38 #define anna_core_util_CommaSeparatedList_hpp
43 Obtiene una lista de objetos a partir de la lista expresada con el operador ','.
44 Lo mas normal es que sea usada para pasar un numero indeterminado de parametros del mismo tipo a un metodo
47 Por ejemplo el modo de uso seria el siguiente:
50 CommaSeparatedList <Objeto> params;
55 for (CommaSeparatedList<Objeto>::const_iterator ii = params.begin (), maxii = params.end (); ii != maxii; ii ++) {
56 Objecto* obj = CommaSeparatedList<Objeto>::data (ii);
58 .... usa el objeto de la lista ...
63 Un ejemplo mas realista. Suponed que tenemos una funcion que tiene que recibir un numero indeterminado
64 de instancia de tipo Objeto. Se definira de la forma:
67 void cualquierFuncion (const anna::CommaSeparatedList <Objeto>& csl) {
68 for (CommaSeparatedList<Objeto>::const_iterator ii = params.begin (), maxii = params.end (); ii != maxii; ii ++) {
69 Objecto* obj = CommaSeparatedList<Objeto>::data (ii);
71 .... usa el objeto de la lista ...
76 La invocacion a la funcion sera:
81 cualquierFunction (aaa, bbb, ccc);
82 cualquierFunction (bbb, ccc);
86 En el primer caso la lista de objetos terminara conteniendo los elementos aaa, bbb y ccc..
87 En el segundo caso la lista de objetos terminara conteniendo los elementos bbb y ccc.
89 template <typename T> class CommaSeparatedList {
91 typedef typename std::vector <T*>::iterator iterator; /**< Definicion para recorrer los elementos. */
92 typedef typename std::vector <T*>::const_iterator const_iterator; /**< Definicion para recorrer los elementos */
97 CommaSeparatedList() {;}
102 ~CommaSeparatedList() { a_parameters.clear(); }
105 Operador coma re-escrito para recorrer los objetos de la expresion basada en ','.
106 Cada uno de los elementos de la expresion es convertido a un elemento de la lista.
107 \param t Parametro con el que ampliar la lista.
108 \return La instancia de la lista destino.
110 CommaSeparatedList<T>& operator, (T& t) throw() { a_parameters.push_back(&t); return *this; }
113 Operador de asignacion.
114 Amplia la lista con el elemento recibido.
115 \param t Parametro con el que ampliar la lista.
116 \return La instancia de la lista destino.
118 CommaSeparatedList<T>& operator= (T& t) throw() { a_parameters.push_back(&t); return *this; }
122 \param rsh Lista de parametros a copiar.
123 \return La instancia de la lista destino.
125 CommaSeparatedList<T>& operator= (CommaSeparatedList<T>& rsh) throw() {
127 a_parameters = rsh.parameters;
133 Elimina el contenido actual de la lista de parametros.
135 void clear() throw() { a_parameters.clear(); }
138 Devuelve un iterator al primer parametro de la lista.
139 \return Un iterator al primer parametro de la lista.
141 iterator begin() throw() { return a_parameters.begin(); }
144 Devuelve un iterator al primer parametro de la lista.
145 \return Un iterator al primer parametro de la lista.
147 const_iterator begin() const throw() { return a_parameters.begin(); }
150 Devuelve un iterator al ultimo parametro de la lista.
151 \return Un iterator al ultimo parametro de la lista.
153 iterator end() throw() { return a_parameters.end(); }
156 Devuelve un iterator al ultimo parametro de la lista.
157 \return Un iterator al ultimo parametro de la lista.
159 const_iterator end() const throw() { return a_parameters.end(); }
162 Devuelve el puntero sobre el que esta posicionado el iterador recibido como parametro.
163 \return El puntero sobre el que esta posicionado el iterador recibido como parametro.
165 static T* data(iterator& ii) throw() { return *ii; }
168 Devuelve el puntero sobre el que esta posicionado el iterador recibido como parametro.
169 \return El puntero sobre el que esta posicionado el iterador recibido como parametro.
171 static const T* data(const_iterator& ii) throw() { return *ii; }
174 std::vector <T*> a_parameters;