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_dbos_AutoObject_hpp
38 #define anna_dbos_AutoObject_hpp
40 #include <anna/dbos/Object.hpp>
48 Facilita el uso de los punteros a objectos obtenidos a partir de los datos guardados en un medio fisico.
50 La idea de esta clase es que el constructor y el destructor de esta clase cooperan para reservar y/o
51 liberar correctamente la instancia de T asociada a esta instancia.
53 \param T Clase que vamos a gestionar.
55 En el siguiente ejemplo podemos ver la forma habitual de trabajar con un objeto persistente tiene
56 el incoveniente de que tenemos que tener en cuenta cada una de las situaciones en las que la
57 referencia obtenida mediante el metodo \em instantiate debe ser liberada.
60 void Application::getServerSocketsData (vector <SocketData>& serverSocketsData) const
61 throw (RuntimeException)
63 LOGMETHOD (TraceMethod ("anna::Application", "getServerSocketsData", ANNA_FILE_LOCATION));
65 Facility* facility (NULL);
66 FacilityLoader facilityLoader;
69 facility = Facility::instantiate (facilityLoader.setKey (a_thisFacility)); // (1)
70 LOGDEBUG (Logger::write (Logger::Debug, facility->asString (), ANNA_FILE_LOCATION));
72 getSocketsData (getThisHostName (), facility->getName (), a_thisCell, a_thisInstance, serverSocketsData);
74 Facility::release (facility);
76 catch (dbos::DatabaseException& edbos) {
77 Facility::release (facility);
78 throw RuntimeException (edbos.getText (), ANNA_FILE_LOCATION);
80 catch (RuntimeException&) { // Tenemos que capturar esta excepcion para liberar el recurso.
81 Facility::release (facility);
87 Como podemos ver a continuacion el siguiente metodo es mucho mas sencillo y aporta la gran ventaja de que
88 el sistema trabaja por nosotros para liberar correctamente los recursos.
91 void Application::getServerSocketsData (vector <SocketData>& serverSocketsData) const
92 throw (RuntimeException)
94 LOGMETHOD (TraceMethod ("anna::Application", "getServerSocketsData", ANNA_FILE_LOCATION));
96 AutoObject <Facility> facility;
97 FacilityLoader facilityLoader;
100 facility = Facility::instantiate (facilityLoader.setKey (a_thisFacility)); // (1)
102 LOGDEBUG (Logger::write (Logger::Debug, facility->asString (), ANNA_FILE_LOCATION));
104 getSocketsData (getThisHostName (), facility->getName (), a_thisCell, a_thisInstance, serverSocketsData);
106 catch (dbos::DatabaseException& edbos) {
107 throw RuntimeException (edbos.getText (), ANNA_FILE_LOCATION);
112 template <typename T> class AutoObject {
116 \param t Instancia del objeto asociado a esta instancia.
117 \warning La instancia deberia haber sido obtenida mediate la invocacion a \em T::instantiate de la
120 explicit AutoObject(T* t) : a_t(t) {;}
125 AutoObject() : a_t(NULL) {;}
128 Destructor. Invoca al metodo \em T::release
130 ~AutoObject() { if(a_t != NULL) T::release(a_t); }
134 Permite invocar a metodos de la clase T.
135 \return La instancia de la clase T asociada a esta instancia.
137 T* operator -> () const throw() { return a_t; }
141 \param t Referencia al objeto que vamos a tratar.
142 \return La instancia de la clase T asociada a esta instancia.
144 T* operator = (T* t) throw() {
155 \param other Referencia al objeto que vamos a tratar.
156 \return La instancia de la clase T asociada a esta instancia.
158 T* operator = (const AutoObject <T>& other) throw(RuntimeException) {
159 return (this != &other) ? (*this = T::duplicate(other.a_t)) : a_t;
163 Operador de conversion.
164 \return La instancia de la clase T asociada a esta instancia.
166 operator T*() const throw() { return a_t; }