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 **************************************************************************/ 00021 00028 #ifndef KEYVALUE_UTIL_SAVER_H_ 00029 #define KEYVALUE_UTIL_SAVER_H_ 00030 00031 #include <boost/utility.hpp> 00032 00033 namespace keyvalue { 00034 namespace util { 00035 00049 template <typename SavedType> 00050 class Saver : private boost::noncopyable { 00051 00052 public: 00053 00059 explicit 00060 Saver(SavedType& data); 00061 00070 Saver(SavedType& data, const SavedType& onExit); 00071 00072 ~Saver(); 00073 00079 SavedType 00080 getSaved() const; 00081 00082 private: 00083 00084 SavedType& data_; 00085 SavedType onExit_; 00086 00087 }; // class Saver 00088 00089 /*-------------------------------------------------------------------------- 00090 * Saver() 00091 *------------------------------------------------------------------------*/ 00092 00093 template <typename SavedType> 00094 Saver<SavedType>::Saver(SavedType& data) : data_(data), onExit_(data_) { 00095 } 00096 00097 template <typename SavedType> 00098 Saver<SavedType>::Saver(SavedType& data, const SavedType& onExit) : 00099 data_(data), onExit_(onExit) { 00100 } 00101 00102 /*-------------------------------------------------------------------------- 00103 * ~Saver() 00104 *------------------------------------------------------------------------*/ 00105 00106 template <typename SavedType> 00107 Saver<SavedType>::~Saver() { 00108 data_ = onExit_; 00109 } 00110 00111 /*-------------------------------------------------------------------------- 00112 * Saver() 00113 *------------------------------------------------------------------------*/ 00114 00115 template <typename SavedType> 00116 SavedType 00117 Saver<SavedType>::getSaved() const { 00118 return data_; 00119 } 00120 00121 } // namespace util 00122 } // namespace keyvalue 00123 00124 #endif // KEYVALUE_UTIL_SAVER_H_