- Add unified Singleton support
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@643 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
8ca3a61730
commit
383889a203
17 changed files with 193 additions and 136 deletions
|
@ -1,4 +1,4 @@
|
|||
// Src/templates/parameter.tcc - Copyright 2005, 2006, University
|
||||
// src/templates/parameter.tcc - Copyright 2005, 2006, University
|
||||
// of Padova, dept. of Pure and Applied
|
||||
// Mathematics
|
||||
//
|
||||
|
|
59
src/templates/singleton.hh
Normal file
59
src/templates/singleton.hh
Normal file
|
@ -0,0 +1,59 @@
|
|||
// singleton.hh - Copyright 2005, 2006, University
|
||||
// of Padova, dept. of Pure and Applied
|
||||
// Mathematics
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#ifndef SINGLETON_HH
|
||||
#define SINGLETON_HH 1
|
||||
|
||||
#include <glibmm/thread.h>
|
||||
#include "config.h"
|
||||
|
||||
namespace sgpem
|
||||
{
|
||||
|
||||
/** \brief An abstract implementation of the Singleton design pattern.
|
||||
*
|
||||
* Singleton implementers constuctor will have to declare friendliness
|
||||
* to Singleton::get_instance(). This also attempts to achieve
|
||||
* thread-safeness.
|
||||
*/
|
||||
template<typename Instantiated_class>
|
||||
class SG_DLLEXPORT Singleton
|
||||
{
|
||||
public:
|
||||
|
||||
/** \brief Ensures thread safety is respected, and returns the instantiated object
|
||||
*
|
||||
* This is done by locking _instance via a mutex, before intantiating the
|
||||
* Instantiated_class attribute (if not done before).
|
||||
*
|
||||
* \return The instantiated object
|
||||
*/
|
||||
static Instantiated_class& get_instance();
|
||||
|
||||
private:
|
||||
static Instantiated_class* _instance;
|
||||
static Glib::StaticMutex _mutex;
|
||||
}; //~ class Singleton
|
||||
|
||||
} //~ namespace sgpem
|
||||
|
||||
|
||||
#include "singleton.tcc"
|
||||
|
||||
#endif //~ SINGLETON_HH
|
||||
|
45
src/templates/singleton.tcc
Normal file
45
src/templates/singleton.tcc
Normal file
|
@ -0,0 +1,45 @@
|
|||
// singleton.tcc - Copyright 2005, 2006, University
|
||||
// of Padova, dept. of Pure and Applied
|
||||
// Mathematics
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#ifndef SINGLETON_TCC
|
||||
#define SINGLETON_TCC 1
|
||||
|
||||
#include "singleton.hh"
|
||||
|
||||
template<typename Instantiated_class>
|
||||
Instantiated_class*
|
||||
sgpem::Singleton<Instantiated_class>::_instance = NULL;
|
||||
|
||||
template<typename Instantiated_class>
|
||||
Glib::StaticMutex
|
||||
sgpem::Singleton<Instantiated_class>::_mutex = GLIBMM_STATIC_MUTEX_INIT;
|
||||
|
||||
|
||||
template<typename Instantiated_class>
|
||||
Instantiated_class&
|
||||
sgpem::Singleton<Instantiated_class>::get_instance()
|
||||
{
|
||||
Glib::Mutex::Lock lock(_mutex);
|
||||
if(_instance == NULL)
|
||||
_instance = new Instantiated_class();
|
||||
return *_instance;
|
||||
}
|
||||
|
||||
|
||||
#endif //~ SINGLETON_TCC
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue