sgpemv2/src/templates/singleton.hh

58 lines
1.8 KiB
C++
Raw Normal View History

// 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 SG_DLLLOCAL _mutex;
}
; //~ class Singleton
} //~ namespace sgpem
#endif //~ SINGLETON_HH