2006-06-21 11:09:50 +02:00
|
|
|
// 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"
|
|
|
|
|
2006-06-29 10:44:30 +02:00
|
|
|
namespace sgpem
|
2006-06-21 11:09:50 +02:00
|
|
|
{
|
2006-06-29 10:44:30 +02:00
|
|
|
|
|
|
|
/** \brief An abstract implementation of the Singleton design pattern.
|
2006-06-21 11:09:50 +02:00
|
|
|
*
|
|
|
|
* Singleton implementers constuctor will have to declare friendliness
|
|
|
|
* to Singleton::get_instance(). This also attempts to achieve
|
|
|
|
* thread-safeness.
|
|
|
|
*/
|
|
|
|
template<typename Instantiated_class>
|
2006-06-29 10:44:30 +02:00
|
|
|
class SG_DLLEXPORT Singleton
|
2006-06-21 11:09:50 +02:00
|
|
|
{
|
|
|
|
public:
|
2006-06-29 10:44:30 +02:00
|
|
|
|
|
|
|
/** \brief Ensures thread safety is respected, and returns the instantiated object
|
2006-06-21 11:09:50 +02:00
|
|
|
*
|
|
|
|
* 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;
|
2006-06-23 15:06:39 +02:00
|
|
|
static Glib::StaticMutex SG_DLLLOCAL _mutex;
|
2006-06-29 10:44:30 +02:00
|
|
|
}
|
|
|
|
; //~ class Singleton
|
2006-06-21 11:09:50 +02:00
|
|
|
|
|
|
|
} //~ namespace sgpem
|
|
|
|
|
|
|
|
#endif //~ SINGLETON_HH
|
|
|
|
|