Updated license
[anna.git] / include / anna / core / Cloneable.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_core_Cloneable_hpp
38 #define anna_core_Cloneable_hpp
39
40 #include <typeinfo>
41
42 #include <anna/core/functions.hpp>
43 #include <anna/core/RuntimeException.hpp>
44 #include <anna/core/AutoPointer.hpp>
45
46 namespace anna {
47
48 /**
49  * Clase de la que deben heredar todas las clases con capacidades de clonado.
50  *
51  * La clase que implemente este interface debe invocar a alguno de las macros que facilitan la implementación de los métodos virtuales requeridos.
52  *
53  * \see \ref clone_final \ref clone_default \ref clone_abstract
54  */
55 class Cloneable : public AutoPointer {
56 public:
57   /**
58    * Destructor.
59    */
60   virtual ~Cloneable() {;}
61
62   /**
63    * Genera de esta instancia. Por cada clon generado habrá que invocar a #release.
64    * \return Un clon de la instancia recibida
65    */
66   virtual Cloneable* clone() const throw() = 0;
67
68   /**
69    * Devuelve el nombre lógico de esta clase.
70    * \return el nombre lógico de esta clase.
71    */
72   static const char* className() throw() { return "Cloneable"; }
73
74 protected:
75   /**
76    * Constructor
77    * \param whenFinished Indica que operación a realizar cuando se invoque a anna::Cloneable::release.
78    */
79   Cloneable(const WhenFinished::_v whenFinished = WhenFinished::Ignore) : AutoPointer(whenFinished) {;}
80
81   /**
82    * Constructor copia.
83    * \param other Instancia de la que copiar.
84    * \warning la instancia generada con este constructor establecerá que al invocar a anna::Cloneable::release se
85    * invocará al operador \em delete de esta instancia.
86    */
87   Cloneable(const Cloneable& other) : AutoPointer(WhenFinished::Delete) {;}
88 };
89
90 /**
91  * \page clone_final
92  * Define el método \em clone que invoca al constructor copia de la forma:
93  *
94  * \code
95 #define anna_clone_final(Class) \
96    Class* clone () const throw () { return new Class (*this); } \
97    friend class AutoPointer;
98  * \endcode
99  */
100 #define anna_clone_final(Class) \
101    Class* clone () const throw () { return new Class (*this); } \
102    friend class AutoPointer;
103
104 /**
105  * \page clone_default
106  * Define el método \em clone, pero permite que sea re-escrito por herencias posteriores con la forma:
107  *
108  * \code
109 #define anna_clone_default(Class) \
110    virtual Class* clone () const throw () { return new Class (*this); } \
111    friend class AutoPointer;
112  * \endcode
113  */
114 #define anna_clone_default(Class) \
115    virtual Class* clone () const throw () { return new Class (*this); } \
116    friend class AutoPointer;
117
118 /**
119  * \page clone_abstract
120  * Define el método \em clone que tiene que ser re-escrito por herencias posteriores, pero facilita la conversión de
121  * la instancia creada al tipo adecuado de 'Cloneable', tendrá la forma:
122  * \code
123 #define anna_clone_abstract(Class) \
124    virtual Class* clone () const throw () = 0; \
125    friend class AutoPointer;
126  * \endcode
127  */
128 #define anna_clone_abstract(Class) \
129    virtual Class* clone () const throw () = 0; \
130    friend class AutoPointer;
131
132 }
133
134 #endif
135
136