// -*- C++ -*- /*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ /** * \file mar_transaction_cpp.h * \brief TODO */ #ifndef INCLUDED_MAR_TRANSACTION_CPP #define INCLUDED_MAR_TRANSACTION_CPP #ifndef INCLUDED_MAR_UTIL_CPP #include "mar_util_cpp.h" #endif // INCLUDED_MAR_UTIL_CPP #if !defined(WIN32) #ifndef INCLUDED_CLIMITS #define INCLUDED_CLIMITS #include #endif // INCLUDED_CLIMITS #if !defined(PATH_MAX) #define MAR_MAXPATH 255 #else // PATH_MAX #define MAR_MAXPATH PATH_MAX #endif // PATH_MAX #else // WIN32 #ifndef INCLUDED_CSTDLIB #define INCLUDED_CSTDLIB #include #endif // INCLUDED_CSTDLIB #if !defined(_MAX_PATH) #define MAR_MAXPATH 260 #else // _MAX_PATH #define MAR_MAXPATH _MAX_PATH #endif // _MAX_PATH #endif // WIN32 namespace mar { template class transaction { public: typedef file_policyT file_policy_type; private: char path_[MAR_MAXPATH]; char master_[MAR_MAXPATH]; archive ar_; bool done_; transaction(const transaction& rhs); transaction& operator =(const transaction& rhs); public: ~transaction() MAR_NOTHROW { try { close(false); } catch (...) { } } transaction(const char* path, const char* master) : ar_(opencopy(path, master)), done_(false) { strncpy(path_, path, sizeof(path_)); strncpy(master_, master, sizeof(master_)); path_[sizeof(path_) - 1] = '\0'; master_[sizeof(master_) - 1] = '\0'; } void close(bool commit = true) { if (done_) return; done_ = true; int err(0); if (ar_.isopen() && -1 == ar_.release()) err = mar_errno(); if (commit && 0 == err) file_policy_type::rename(path_, master_); else file_policy_type::unlink(path_); if (err) throw error(err); } void commit() { close(true); } void rollback() { close(false); } archive retain() { return ar_; } const archive& retain() const { return ar_; } }; } #endif // INCLUDED_MAR_TRANSACTION_CPP