QxOrm  1.4.9
C++ Object Relational Mapping library
class_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 
00040 #ifndef QT_NO_DEBUG
00041 #ifndef _QX_MODE_RELEASE
00042 #if _QX_USE_MEM_LEAK_DETECTION
00043 
00044 #ifndef _CLASS_LEVEL_LOCK_H
00045 #define _CLASS_LEVEL_LOCK_H
00046 
00047 #ifdef _MSC_VER
00048 #pragma once
00049 #endif
00050 
00051 #include "fast_mutex.h"
00052 
00053 namespace qx {
00054 namespace memory {
00055 
00056 #ifdef _NOTHREADS
00057 
00061     template <class _Host, bool _RealLock = false>
00062     class class_level_lock
00063     {
00064     public:
00066         class lock
00067         {
00068         public:
00069             lock() {}
00070         };
00071 
00072         typedef _Host volatile_type;
00073     };
00074 #else
00075 
00082     template <class _Host, bool _RealLock = true>
00083     class class_level_lock
00084     {
00085         static fast_mutex _S_mtx;
00086 
00087     public:
00088         class lock;
00089         friend class lock;
00090 
00092         class lock
00093         {
00094             lock(const lock&);
00095             lock& operator=(const lock&);
00096         public:
00097             lock()
00098             {
00099                 if (_RealLock)
00100                     _S_mtx.lock();
00101             }
00102             ~lock()
00103             {
00104                 if (_RealLock)
00105                     _S_mtx.unlock();
00106             }
00107         };
00108 
00109         typedef volatile _Host volatile_type;
00110     };
00111 
00112 #if HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
00113 
00114     template <class _Host>
00115     class class_level_lock<_Host, false>
00116     {
00117     public:
00119         class lock
00120         {
00121         public:
00122             lock() {}
00123         };
00124 
00125         typedef _Host volatile_type;
00126     };
00127 #endif // HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
00128 
00129     template <class _Host, bool _RealLock>
00130     fast_mutex class_level_lock<_Host, _RealLock>::_S_mtx;
00131 #endif // _NOTHREADS
00132 
00133 } // namespace memory
00134 } // namespace qx
00135 
00136 #endif // _CLASS_LEVEL_LOCK_H
00137 #endif // _QX_USE_MEM_LEAK_DETECTION
00138 #endif // _QX_MODE_RELEASE
00139 #endif // QT_NO_DEBUG