Global.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00034 #ifndef KEYVALUE_UTIL_GLOBAL_H_
00035 #define KEYVALUE_UTIL_GLOBAL_H_
00036 
00037 #include <boost/utility.hpp>
00038 
00039 #include "keyvalue/extern/SharedPtr.h"
00040 #include "keyvalue/sys/exception/KV_ASSERT.h"
00041 
00042 namespace keyvalue {
00043 namespace util {
00044 
00073 template <typename ObjectType>
00074 class Global : private boost::noncopyable {
00075 
00076 public:
00077 
00090   static shared_ptr<ObjectType>
00091   get();
00092 
00098   static void
00099   set(shared_ptr<ObjectType> object);
00100 
00101 private:
00102 
00117   Global();
00118 
00124   static Global&
00125   getInstance();
00126 
00127   shared_ptr<ObjectType> object_;
00128 
00129 }; // class Global
00130 
00131 /*--------------------------------------------------------------------------
00132  * get()
00133  *------------------------------------------------------------------------*/
00134 
00135 template <typename ObjectType>
00136 shared_ptr<ObjectType>
00137 Global<ObjectType>::get() {
00138   KV_ASSERT(getInstance().object_, "Called unitialized global object!");
00139   return getInstance().object_;
00140 }
00141 
00142 /*--------------------------------------------------------------------------
00143  * set()
00144  *------------------------------------------------------------------------*/
00145 
00146 template <typename ObjectType>
00147 void
00148 Global<ObjectType>::set(shared_ptr<ObjectType> object) {
00149   getInstance().object_ = object;
00150 }
00151 
00152 /*--------------------------------------------------------------------------
00153  * Global()
00154  *------------------------------------------------------------------------*/
00155 
00156 template <typename ObjectType>
00157 Global<ObjectType>::Global() : object_(make_shared<ObjectType>()) {
00158 }
00159 
00160 /*--------------------------------------------------------------------------
00161  * getInstance()
00162  *------------------------------------------------------------------------*/
00163 
00164 template <typename ObjectType>
00165 Global<ObjectType>&
00166 Global<ObjectType>::getInstance() {
00167   static Global<ObjectType> singleton;
00168   return singleton;
00169 }
00170 
00171 } // namespace util
00172 } // namespace keyvalue
00173 
00174 #endif // KEYVALUE_UTIL_GLOBAL_H_