OSDN Git Service

基本ファイルを追加
[winaudioj/sfwasapi.git] / sfwasapi / singleton.h
1 #pragma once
2 /*
3   ==============================================================================
4
5    This file is part of the S.F.Tracker
6    Copyright 2005-7 by Satoshi Fujiwara.
7
8    S.F.Tracker can be redistributed and/or modified under the terms of the
9    GNU General Public License, as published by the Free Software Foundation;
10    either version 2 of the License, or (at your option) any later version.
11
12    S.F.Tracker is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with S.F.Tracker; if not, visit www.gnu.org/licenses or write to the
19    Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
20    Boston, MA 02111-1307 USA
21
22   ==============================================================================
23 */
24 /** @file
25  *  @brief 
26  *  @author S.F. (Satoshi Fujiwara)
27  */
28 #include <boost/thread.hpp>
29 #include <boost/shared_ptr.hpp>
30 namespace sf {
31     template<typename BaseClass,template <class> class PointerType = boost::shared_ptr > struct singleton 
32     {
33         typedef PointerType<BaseClass> ptr;
34         friend  BaseClass;
35  
36         static ptr& instance()
37         {
38             boost::call_once(init,flag_);
39             return instance_;
40         };
41
42
43         singleton(){};
44     private:
45         singleton(const singleton& );
46                 static void init(){instance_.reset(new BaseClass);};
47         static PointerType<BaseClass> instance_;
48         static boost::once_flag flag_;
49     };
50
51     template<class BaseClass,template <class> class PointerType> boost::once_flag singleton<BaseClass,PointerType>::flag_ = BOOST_ONCE_INIT;
52     template<class BaseClass,template <class> class PointerType> PointerType<BaseClass> singleton<BaseClass,PointerType>::instance_;
53 };
54
55