Updated license
[anna.git] / include / anna / core / Singleton.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_Singleton_hpp
38 #define anna_core_Singleton_hpp
39
40 #include <typeinfo>
41
42 #include <anna/core/mt/Mutex.hpp>
43 #include <anna/core/mt/Guard.hpp>
44
45 namespace anna {
46
47 /**
48  Patrn Singleton. Limita la utilizacin de una determinada clase
49  para que slo pueda existir una nica instancia.
50
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.
54
55  En el archivo de cabecera de la clase A tendriamos:
56  \code
57
58  ...
59  ...
60
61  #include <anna/core/Singleton.hpp>
62
63  class A : public anna::Singleton <A> {
64  public:
65     // Accesores
66     int obtenerDato () const { return a_unDato; }
67
68     // Modificadores
69     void establecerOtroDato (const ASD& otroDato) { a_otroDato = otroDato; }
70
71     // Operadores
72        ....
73
74     // Metodos
75        ...
76
77  private:
78     int a_unDato;
79     ASD a_otroDato;
80
81     A ();
82
83     friend class functions::Singleton <A>;
84  };
85
86 \endcode
87
88  Para hacer uso de la clase A:
89
90  \code
91 #include <A.h>
92
93 ...
94 ...
95
96  void CualquierClase::cualquierFuncion () throw (RuntimeException)
97  {
98     A& a (A::instantiate ());
99
100     cout << a.obtenerDato () << endl;
101  }
102
103  \endcode
104 */
105 template <class T> class Singleton {
106 public:
107   /**
108      @return La instancia de la clase indicada en la creacin de la template.
109   */
110   static T& instantiate() { return *alloc(true); }
111
112   /**
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.
116   */
117   static void release() { alloc(false); }
118
119 private:
120   static T* alloc(const bool allocate) {
121     static Mutex mutex;
122     static T* result(NULL);
123
124     if(allocate == true) {
125       if(result == NULL) {
126         Guard guard(mutex, "Singleton<T>::allocate");
127
128         if(result == NULL)
129           result = new T;
130       }
131     } else {
132       if(result != NULL) {
133         Guard guard(mutex, "Singleton<T>::deallocate");
134
135         if(result != NULL) {
136           delete result;
137           result = NULL;
138         }
139       }
140     }
141
142     return result;
143   }
144 };
145
146 } //namespace anna
147
148 #endif
149