First commit
[anna.git] / include / anna / core / AutoPointer.hpp
1 // ANNA - Anna is Not 'N' 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_core_AutoPointer_hpp
38 #define anna_core_AutoPointer_hpp
39
40 #include <anna/core/util/String.hpp>
41
42 namespace anna {
43
44 /**
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
47  * la pila.
48  *
49  * La clase que implemente este interface debe declara como \em friend a esta clase.
50  */
51 class AutoPointer {
52 public:
53   /**
54    * Óperación a realizar cuando se termine de tratar con esta instancia y se invoque a anna::AutoPointer::release.
55    * \see AutoPointer.
56    */
57   struct WhenFinished  {
58     enum _v {
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. */
61     };
62   };
63
64   /**
65    * Destructor.
66    */
67   virtual ~AutoPointer() {;}
68
69   /**
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.
72    */
73   WhenFinished::_v getWhenFinished() const throw() { return a_whenFinished; }
74
75   /**
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.
78    */
79   void setWhenFinished(const  WhenFinished::_v whenFinished) throw() { a_whenFinished = whenFinished; }
80
81   /**
82      Devuelve una cadena con la informacion relevante de este objeto.
83      \return Una cadena con la informacion relevante de este objeto.
84   */
85   virtual String asString() const throw();
86
87   /**
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.
91    */
92   template <typename T> static T* release(T& instance)
93   throw() {
94     return release(&instance);
95   }
96
97   /**
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.
101    */
102   template <typename T> static T* release(T* instance)
103   throw() {
104     if(instance == NULL)
105       return NULL;
106
107     if(instance->getWhenFinished() == WhenFinished::Delete) {
108       delete instance;
109       instance = NULL;
110     }
111
112     return instance;
113   }
114
115   /**
116    * Devuelve el nombre lógico de esta clase.
117    * \return el nombre lógico de esta clase.
118    */
119   static const char* className() throw() { return "AutoPointer"; }
120
121 protected:
122   /**
123    * Constructor
124    * \param whenFinished Indica que operación a realizar cuando se invoque a anna::AutoPointer::release.
125    */
126   AutoPointer(const WhenFinished::_v whenFinished) : a_whenFinished(whenFinished) {;}
127
128 private:
129   WhenFinished::_v a_whenFinished;
130 };
131
132 }
133
134 #endif
135
136