QxOrm  1.5.0
C++ Object Relational Mapping library
object_level_lock.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 // vim:tabstop=4:shiftwidth=4:expandtab:
00003 
00004 /*
00005  * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
00006  *
00007  * This software is provided 'as-is', without any express or implied
00008  * warranty.  In no event will the authors be held liable for any
00009  * damages arising from the use of this software.
00010  *
00011  * Permission is granted to anyone to use this software for any purpose,
00012  * including commercial applications, and to alter it and redistribute
00013  * it freely, subject to the following restrictions:
00014  *
00015  * 1. The origin of this software must not be misrepresented; you must
00016  *    not claim that you wrote the original software.  If you use this
00017  *    software in a product, an acknowledgement in the product
00018  *    documentation would be appreciated but is not required.
00019  * 2. Altered source versions must be plainly marked as such, and must
00020  *    not be misrepresented as being the original software.
00021  * 3. This notice may not be removed or altered from any source
00022  *    distribution.
00023  *
00024  * This file is part of Stones of Nvwa:
00025  *      http://sourceforge.net/projects/nvwa
00026  *
00027  */
00028 
00043 #ifndef QT_NO_DEBUG
00044 #ifndef _QX_MODE_RELEASE
00045 #if _QX_USE_MEM_LEAK_DETECTION
00046 
00047 #ifndef _OBJECT_LEVEL_LOCK_H
00048 #define _OBJECT_LEVEL_LOCK_H
00049 
00050 #ifdef _MSC_VER
00051 #pragma once
00052 #endif
00053 
00054 #include "fast_mutex.h"
00055 
00056 namespace qx {
00057 namespace memory {
00058 
00059 # ifdef _NOTHREADS
00060 
00064     template <class _Host>
00065     class object_level_lock
00066     {
00067     public:
00069         class lock
00070         {
00071 #ifndef _QX_MODE_RELEASE
00072 #ifndef QT_NO_DEBUG
00073             const object_level_lock& _M_host;
00074 #endif // QT_NO_DEBUG
00075 #endif // _QX_MODE_RELEASE
00076             lock(const lock&);
00077             lock& operator=(const lock&);
00078         public:
00079             explicit lock(const object_level_lock& __host)
00080 #ifndef _QX_MODE_RELEASE
00081 #ifndef QT_NO_DEBUG
00082                 : _M_host(__host)
00083 #endif // QT_NO_DEBUG
00084 #endif // _QX_MODE_RELEASE
00085             {}
00086 #ifndef _QX_MODE_RELEASE
00087 #ifndef QT_NO_DEBUG
00088             // The purpose of this method is allow one to write code
00089             // like "assert(guard.get_locked_object() == this)" to
00090             // ensure that the locked object is exactly the object being
00091             // accessed.
00092             const object_level_lock* get_locked_object() const
00093             {
00094                 return &_M_host;
00095             }
00096 #endif // QT_NO_DEBUG
00097 #endif // _QX_MODE_RELEASE
00098         };
00099 
00100         typedef _Host volatile_type;
00101     };
00102 # else
00103 
00107     template <class _Host>
00108     class object_level_lock
00109     {
00110         mutable fast_mutex _M_mtx;
00111 
00112     public:
00113         class lock;
00114         friend class lock;
00115 
00117         class lock
00118         {
00119             const object_level_lock& _M_host;
00120 
00121             lock(const lock&);
00122             lock& operator=(const lock&);
00123         public:
00124             explicit lock(const object_level_lock& __host) : _M_host(__host)
00125             {
00126                 _M_host._M_mtx.lock();
00127             }
00128             ~lock()
00129             {
00130                 _M_host._M_mtx.unlock();
00131             }
00132 #ifndef _QX_MODE_RELEASE
00133 #ifndef QT_NO_DEBUG
00134             // The purpose of this method is allow one to write code
00135             // like "assert(guard.get_locked_object() == this)" to
00136             // ensure that the locked object is exactly the object being
00137             // accessed.
00138             const object_level_lock* get_locked_object() const
00139             {
00140                 return &_M_host;
00141             }
00142 #endif // QT_NO_DEBUG
00143 #endif // _QX_MODE_RELEASE
00144         };
00145 
00146         typedef volatile _Host volatile_type;
00147     };
00148 # endif // _NOTHREADS
00149 
00150 } // namespace memory
00151 } // namespace qx
00152 
00153 #endif // _OBJECT_LEVEL_LOCK_H
00154 #endif // _QX_USE_MEM_LEAK_DETECTION
00155 #endif // _QX_MODE_RELEASE
00156 #endif // QT_NO_DEBUG