SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CSingleton.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CSingleton.hpp
23  *
24  * Created on: Jul 8, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CSINGLETON_HPP_
30 #define CSINGLETON_HPP_
31 
32 #ifdef DEBUG
33 //For printing debug messages
34 #include <iostream>
35 #endif
36 
37 namespace sutil
38 {
39 
46  template<typename SDataStruct>
47  class CSingleton
48  {
49  public:
55  static SDataStruct* getData();
56 
58  static bool resetData();
59 
61  virtual ~CSingleton();
62 
63  protected:
66  SDataStruct data_;
67 
70 
71  private:
74 
77 
80  };
81 
83  template<typename SDataStruct>
84  CSingleton<SDataStruct>* CSingleton<SDataStruct>::singleton_ = 0;
85 
86  template<typename SDataStruct>
88  {
89  if (0 == singleton_) // is it the first call?
90  {
91  singleton_ = new CSingleton<SDataStruct>(); // create sole instance
92  if(0 == singleton_)
93  {
94 #ifdef DEBUG
95  std::cerr<<"\nCSingleton::createDb() Error: Could not dynamically allocate the database";
96 #endif
97  return 0;
98  }
99 #ifdef DEBUG
100  std::cout<<"\nCSingleton::getData() : Creating singleton";
101 #endif
102  }
103  return &(singleton_->data_);
104  }
105 
106  template<typename SDataStruct>
108  {
109  if(0 != singleton_)
110  {
111  CSingleton<SDataStruct>* tmp = singleton_;
112  singleton_ = 0;
113  delete tmp;
114  tmp = 0;
115 
116  if(0 != getData())
117  {
118 #ifdef DEBUG
119  std::cout<<"\nCSingleton::resetData() : Reset the singleton";
120 #endif
121  return true;
122  }
123  else
124  { return false; }
125  }
126 #ifdef DEBUG
127  std::cout<<"\nCSingleton::resetData() : Reset not required";
128 #endif
129  return true;
130  }
131 
132  template<typename SDataStruct>
134  {
135  //NOTE : Because this doesn't delete the object, but deletes
136  //the singleton instead, you can't create two objects of this type
137  //EVEN within the member functions. Only one object must exist, else
138  //you will get segfaults.
139  if(0 != singleton_)
140  {
141  delete singleton_;
142  singleton_ = 0;
143 #ifdef DEBUG
144  std::cout<<"\nCSingleton::~CSingleton() : Destroying singleton";
145 #endif
146  }
147  }
148 }
149 
150 #endif /* CSINGLETON_HPP_ */
CSingleton(const CSingleton &)
Definition: CSingleton.hpp:76
virtual ~CSingleton()
Definition: CSingleton.hpp:133
Definition: CSingleton.hpp:47
static CSingleton * singleton_
Definition: CSingleton.hpp:69
SDataStruct data_
Definition: CSingleton.hpp:66
static SDataStruct * getData()
Definition: CSingleton.hpp:87
CSingleton()
Definition: CSingleton.hpp:73
CSingleton & operator=(const CSingleton &)
Definition: CSingleton.hpp:79
static bool resetData()
Definition: CSingleton.hpp:107