Updated license
[anna.git] / include / anna / dbos / SetFacade.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_dbos_SetFacade_hpp
38 #define anna_dbos_SetFacade_hpp
39
40 #include <vector>
41
42 #include <anna/dbos/Set.hpp>
43 #include <anna/dbos/ObjectFacade.hpp>
44
45 namespace anna {
46
47 namespace dbos {
48
49 /**
50    Clase que facilita el acceso y uso de las clases encargadas de la instanciacion de multiples objetos a partir de los
51    datos contenidos en un medio fisico, que normalmente seria la tabla de una base de datos.
52
53    Esta nos facilita el manejo de instancias multiples, es decir, para una condicion de carga dada hay varios registros
54    o elementos del medio fisico que la cumplen.
55
56    La clase \em T debe tener definidos los siquientes metodos:
57       \li anna::dbos::Object::instantiate: Interpreta la informacin la del medio fisico para adecuarla a
58       la clase C++.
59       \li void destroy () throw (): Libera los recursos reservados por este objeto
60
61    \see dbos_declare_set
62    \see dbos_prepare_set
63 */
64 template <typename T> class SetFacade : public ObjectFacade < Set <T> > {
65 public:
66   typedef typename Set<T>::iterator iterator;
67   typedef typename Set<T>::const_iterator const_iterator;
68
69   /**
70      Devuelve el numero de elementos contenidos en el conjunto recibido como parametro.
71       Se puede usar sin tener que preocuparse por
72      el valor de la instancia del conjunto, ya que si este es NULL devolveria 0.
73      \param t Instancia del conjunto.
74      \return El numero de elementos que contiene el conjunto.
75   */
76   static int size(Set<T>* t) throw() { return (t == NULL) ? 0 : t->size(); }
77
78   /**
79      Iterator al primer elemento del conjunto. Se puede usar sin tener que preocuparse por
80      el valor de la instancia del conjunto, ya que si este es NULL devolveria 0.
81      \return Un iterador del primer elemento del conjunto.
82   */
83   static iterator begin(Set<T>* t) throw() {
84     return (t == NULL) ? iterator(0) : t->begin();
85   }
86
87   /**
88      Iterator al ultimo elemento del conjunto. Se puede usar sin tener que preocuparse por
89      el valor de la instancia del conjunto, ya que si este es NULL devolveria 0.
90      \return Un iterador del primer elemento del conjunto.
91   */
92   static iterator end(Set<T>* t) throw() {
93     return (t == NULL) ? iterator(0) : t->end();
94   }
95
96   /**
97      Iterator al primer elemento del conjunto. Se puede usar sin tener que preocuparse por
98      el valor de la instancia del conjunto, ya que si este es NULL devolveria 0.
99      \return Un iterador del primer elemento del conjunto.
100   */
101   static const_iterator begin(const Set<T>* t) throw() {
102     return (t == NULL) ? const_iterator(0) : t->begin();
103   }
104
105   /**
106      Iterator al ultimo elemento del conjunto. Se puede usar sin tener que preocuparse por
107      el valor de la instancia del conjunto, ya que si este es NULL devolveria 0.
108      \return Un iterador del primer elemento del conjunto.
109   */
110   static const_iterator end(const Set<T>* t) throw() {
111     return (t == NULL) ? const_iterator(0) : t->end();
112   }
113   /**
114      Devuelve el objeto referenciado por el iterator recibido como parametro.
115      \return El objeto referenciado por el iterator recibido como parametro.
116   */
117   static T* data(iterator ii) throw() { return Set<T>::data(ii); }
118
119   /**
120      Devuelve el objeto referenciado por el iterator recibido como parametro.
121      \return El objeto referenciado por el iterator recibido como parametro.
122   */
123   static const T* data(const_iterator ii) throw() { return Set<T>::data(ii); }
124
125 protected:
126   /**
127      Contructor.
128   */
129   SetFacade() {}
130 };
131
132 /**
133    Definicion a la que hay que invocar en la implementacion de la clase que hereda
134    de anna::dbos::SetFacade.
135
136    \param T Nombre de la clase que vamos a tratar en el ambito de C++.
137 */
138 #define dbos_prepare_set(T) \
139    template <> anna::dbos::StorageArea* anna::dbos::ObjectFacade < anna::dbos::Set <T> >::st_storageArea = NULL
140
141 /**
142    Definicion a la que hay que invocar dentro de la definicion de la clase que hereda
143    de anna::dbos::SetFacade.
144
145    \param T Nombre de la clase que vamos a tratar en el ambito de C++.
146 */
147 #define dbos_declare_set(T) \
148    friend class anna::Allocator<T>; \
149    friend class anna::dbos::Set <T>
150
151 }
152 }
153
154 #endif