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_core_Singleton_hpp
38 #define anna_core_Singleton_hpp
42 #include <anna/core/mt/Mutex.hpp>
43 #include <anna/core/mt/Guard.hpp>
48 Patrn Singleton. Limita la utilizacin de una determinada clase
49 para que slo pueda existir una nica instancia.
51 El contructor por defecto de la clase T debe ser privado, para evitar que
52 esta clase se pueda instanciar desde el exterior.
53 Vamos a ver un ejemplo de como se haria un Singleton sobre la clase A.
55 En el archivo de cabecera de la clase A tendriamos:
61 #include <anna/core/Singleton.hpp>
63 class A : public anna::Singleton <A> {
66 int obtenerDato () const { return a_unDato; }
69 void establecerOtroDato (const ASD& otroDato) { a_otroDato = otroDato; }
83 friend class functions::Singleton <A>;
88 Para hacer uso de la clase A:
96 void CualquierClase::cualquierFuncion () throw (RuntimeException)
98 A& a (A::instantiate ());
100 cout << a.obtenerDato () << endl;
105 template <class T> class Singleton {
108 @return La instancia de la clase indicada en la creacin de la template.
110 static T& instantiate() { return *alloc(true); }
113 Libera la instancia de la clase indicada en la creacin de esta template.
114 Si no hay creada ninguna instancia simplemente se ignora. Este m�odo
115 slo deber� invocarse justo antes de la terminacin de nuestro programa.
117 static void release() { alloc(false); }
120 static T* alloc(const bool allocate) {
122 static T* result(NULL);
124 if(allocate == true) {
126 Guard guard(mutex, "Singleton<T>::allocate");
133 Guard guard(mutex, "Singleton<T>::deallocate");