Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
invariant_lock.h
Go to the documentation of this file.00001 00004 /* 00005 * Copyright © 2000, 2001 Sofus Mortensen, Paul Hollingsworth 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_UTIL_H 00020 #define COMET_UTIL_H 00021 00022 #include <comet/config.h> 00023 00024 #include <stdexcept> 00025 00026 #include <comet/error.h> 00027 00028 namespace comet { 00032 00035 namespace invariant_lock_impl { 00036 00040 class invariant_lock 00041 { 00042 protected: 00043 invariant_lock() {} 00044 invariant_lock(const invariant_lock &rhs) {} 00045 }; 00046 00052 template<typename CLASS> struct enforcer : public invariant_lock 00053 { 00054 void (CLASS::*m_passert) () const; 00055 const CLASS *m_pobj; 00056 00057 enforcer(const CLASS *pobj, void (CLASS::*passert) () const 00058 ) : m_pobj(pobj), m_passert(passert) 00059 { 00060 (m_pobj->*m_passert)(); 00061 } 00062 ~enforcer() 00063 { 00064 (m_pobj->*m_passert)(); 00065 } 00066 }; 00067 00072 template<typename CLASS> struct simple_enforcer : public invariant_lock 00073 { 00074 const CLASS *m_pobj; 00075 simple_enforcer(const CLASS *pobj) : m_pobj(pobj) 00076 { 00077 m_pobj->assert_valid(); 00078 } 00079 00080 ~simple_enforcer() 00081 { 00082 m_pobj->assert_valid(); 00083 } 00084 }; 00085 } // namespace invariant_lock_impl 00086 00094 template<typename CLASS> 00095 invariant_lock_impl::enforcer<CLASS> create_invariant_lock(const CLASS *pobj, void (CLASS::*assert_member) () const) 00096 { 00097 return invariant_lock_impl::enforcer<CLASS>(pobj, assert_member); 00098 } 00099 00108 template<typename CLASS> 00109 invariant_lock_impl::simple_enforcer<CLASS> create_invariant_lock(const CLASS *pobj) 00110 { 00111 return pobj; 00112 } 00113 00121 typedef invariant_lock_impl::invariant_lock &invariant_lock; 00123 00124 } // namespace 00125 00126 #endif