1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 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.
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_AutoPointer_hpp
38 #define anna_core_AutoPointer_hpp
40 #include <anna/core/util/String.hpp>
45 * Clase de la que deben heredar todas las clases con capacidades de liberación automática.
46 * Respector a \em auto_ptr tiene la ventaja de que puede actuar sobre objetos creados en
49 * La clase que implemente este interface debe declara como \em friend a esta clase.
54 * Óperación a realizar cuando se termine de tratar con esta instancia y se invoque a anna::AutoPointer::release.
59 Ignore /** << La invocación al método anna::AutoPointer::release no tendrá ningún efecto. */,
60 Delete /** << La invocación al método anna::AutoPointer::release originará la llamada al operador \em delete de la instancia. */
67 virtual ~AutoPointer() {;}
70 * Devuelve la operación que se realizará cuando se termine con esta instancia y se invoque a anna::AutoPointer::release.
71 * \return la operación que se realizará cuando se termine con esta instancia y se invoque a anna::AutoPointer::release.
73 WhenFinished::_v getWhenFinished() const throw() { return a_whenFinished; }
76 * Establece la operación a realizar cuando se invoca al método anna::AutoPointer::release.
77 * \param whenFinished Indica que operación a realizar cuando se invoque a anna::AutoPointer::release.
79 void setWhenFinished(const WhenFinished::_v whenFinished) throw() { a_whenFinished = whenFinished; }
82 Devuelve una cadena con la informacion relevante de este objeto.
83 \return Una cadena con la informacion relevante de este objeto.
85 virtual String asString() const throw();
88 * Si fuera necesario libera de la instancia recibida.
89 * \param instance Instancia a liberar si fuera necesario. Puede ser NULL.
90 * \return Devolverá \em NULL si la instancia ha sido liberada o la misma instancia recibida en caso de que no haya sido liberada.
92 template <typename T> static T* release(T& instance)
94 return release(&instance);
98 * Si fuera necesario libera de la instancia recibida.
99 * \param instance Instancia a liberar si fuera necesario. Puede ser NULL.
100 * \return Devolverá \em NULL si la instancia ha sido liberada o la misma instancia recibida en caso de que no haya sido liberada.
102 template <typename T> static T* release(T* instance)
107 if(instance->getWhenFinished() == WhenFinished::Delete) {
116 * Devuelve el nombre lógico de esta clase.
117 * \return el nombre lógico de esta clase.
119 static const char* className() throw() { return "AutoPointer"; }
124 * \param whenFinished Indica que operación a realizar cuando se invoque a anna::AutoPointer::release.
126 AutoPointer(const WhenFinished::_v whenFinished) : a_whenFinished(whenFinished) {;}
129 WhenFinished::_v a_whenFinished;