MySQL++ Reference Manual

Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members

# lockable.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008
    00009
    00010
    00011
    00012
    00013
    00014
    00015
    00016
    00017
    00018 /***********************************************************************
    00019  Copyright (c) 2005 by Educational Technology Resources, Inc.
    00020  Others may also hold copyrights on code in this file.  See the CREDITS
    00021  file in the top directory of the distribution for details.
    00022
    00023  This file is part of MySQL++.
    00024
    00025  MySQL++ is free software; you can redistribute it and/or modify it
    00026  under the terms of the GNU Lesser General Public License as published
    00027  by the Free Software Foundation; either version 2.1 of the License, or
    00028  (at your option) any later version.
    00029
    00030  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00031  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00032  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00033  License for more details.
    00034
    00035  You should have received a copy of the GNU Lesser General Public
    00036  License along with MySQL++; if not, write to the Free Software
    00037  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00038  USA
    00039 ***********************************************************************/
    00040
    00041 #ifndef MYSQLPP_LOCKABLE_H
    00042 #define MYSQLPP_LOCKABLE_H
    00043
    00044 namespace mysqlpp {
    00045
    00048
    00049 class Lock
    00050 {
    00051 public:
    00053         virtual ~Lock() { }
    00054
    00058         virtual bool lock() = 0;
    00059
    00061         virtual void unlock() = 0;
    00062
    00064         virtual bool locked() const = 0;
    00065
    00067         virtual void set(bool b) = 0;
    00068 };
    00069
    00070
    00077
    00078 class BasicLock : public Lock
    00079 {
    00080 public:
    00082         BasicLock(bool locked = false) :
    00083         locked_(locked)
    00084         {
    00085         }
    00086
    00088         ~BasicLock() { }
    00089
    00093         bool lock()
    00094         {
    00095                 if (locked_) {
    00096                         return true;
    00097                 }
    00098                 locked_ = true;
    00099                 return false;
    00100         }
    00101
    00103         void unlock() { locked_ = false; }
    00104
    00106         bool locked() const { return locked_; }
    00107
    00109         void set(bool b) { locked_ = b; }
    00110
    00111 private:
    00112         bool locked_;
    00113 };
    00114
    00115
    00120
    00121 class Lockable
    00122 {
    00123 protected:
    00125         Lockable(bool locked) :
    00126         pimpl_(new BasicLock(locked))
    00127         {
    00128         }
    00129
    00131         virtual ~Lockable()
    00132         {
    00133                 delete pimpl_;
    00134         }
    00135
    00139         virtual bool lock() { return pimpl_->lock(); }
    00140
    00142         virtual void unlock() { pimpl_->unlock(); }
    00143
    00145         bool locked() const { return pimpl_->locked(); }
    00146
    00147 protected:
    00150         void set_lock(bool b) { pimpl_->set(b); }
    00151
    00152 private:
    00153         // Don't allow default construction
    00154         Lockable();
    00155
    00156         // Pointer to implementation object
    00157         Lock* pimpl_;
    00158 };
    00159
    00160 } // end namespace mysqlpp
    00161
    00162 #endif // MYSQLPP_LOCKABLE_H
    00163