cd92e2dd02361042418868db0f735add7b59db40
[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 // http://redmine.teslayout.com/projects/anna-suite
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 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.
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  Singleton pattern. Limits the use to a unique class instance.
49
50  Default constructor for T must be private in order to avoid
51  external instantiation. Following, an example using class A:
52
53
54  On A class header file, we will have:
55  \code
56
57  ...
58  ...
59
60  #include <anna/core/Singleton.hpp>
61
62  class A : public anna::Singleton <A> {
63  public:
64     // getters
65     int getData () const { return a_data; }
66
67     // setters
68     void setOtherData (const ASD& otherData) { a_otherData = otherData; }
69
70     // operators
71        ....
72
73     // methods
74        ...
75
76  private:
77     int a_data;
78     ASD a_otherData;
79
80     A ();
81
82     friend class functions::Singleton <A>;
83  };
84
85 \endcode
86
87  To use A class:
88
89  \code
90 #include <A.h>
91
92 ...
93 ...
94
95  void anyClass::anyFunction () throw (RuntimeException)
96  {
97     A& a (A::instantiate ());
98
99     cout << a.getData () << endl;
100  }
101
102  \endcode
103 */
104 template <class T> class Singleton {
105 public:
106   /**
107      @return Class instance provided on template creation.
108   */
109   static T& instantiate() { return *alloc(true); }
110
111   /**
112      Release the class instance provided in this template creation.
113      If no instance has been created, this will be ignored.
114      This method only have to be used during the program termination.
115   */
116   static void release() { alloc(false); }
117
118 private:
119   static T* alloc(const bool allocate) {
120     static Mutex mutex;
121     static T* result(NULL);
122
123     if(allocate == true) {
124       if(result == NULL) {
125         Guard guard(mutex, "Singleton<T>::allocate");
126
127         if(result == NULL)
128           result = new T;
129       }
130     } else {
131       if(result != NULL) {
132         Guard guard(mutex, "Singleton<T>::deallocate");
133
134         if(result != NULL) {
135           delete result;
136           result = NULL;
137         }
138       }
139     }
140
141     return result;
142   }
143 };
144
145 } //namespace anna
146
147 #endif
148