blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Quest Software, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Quest Software, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00038 #ifndef BLOCXX_GLOBAL_PTR_HPP_INCLUDE_GUARD_ 00039 #define BLOCXX_GLOBAL_PTR_HPP_INCLUDE_GUARD_ 00040 #include "blocxx/BLOCXX_config.h" 00041 #include "blocxx/ThreadOnce.hpp" 00042 #include "blocxx/SafeBool.hpp" 00043 00044 namespace BLOCXX_NAMESPACE 00045 { 00050 template <typename T> 00051 struct DefaultConstructorFactory 00052 { 00053 static T* create() 00054 { 00055 return new T; 00056 } 00057 }; 00058 00082 template <typename T, typename FactoryT = DefaultConstructorFactory<T> > 00083 class GlobalPtr 00084 { 00085 private: 00086 struct InitPtr 00087 { 00088 InitPtr(T*& p) 00089 : m_p(p) 00090 {} 00091 T*& m_p; 00092 00093 void operator()() 00094 { 00095 m_p = static_cast<T*>(FactoryT::create()); 00096 } 00097 }; 00098 00099 public: 00105 T* get() const 00106 { 00107 callOnce(m_onceFlag, InitPtr(m_p)); 00108 00109 return m_p; 00110 } 00111 00118 T* set(T* newP) 00119 { 00120 T* oldP = get(); 00121 m_p = newP; 00122 return oldP; 00123 } 00124 00125 BLOCXX_SAFE_BOOL_IMPL(GlobalPtr, T*, GlobalPtr::m_p, get() != 0); 00126 00127 T & operator*() const 00128 { 00129 return *get(); 00130 } 00131 T * operator->() const 00132 { 00133 return get(); 00134 } 00135 00136 00137 // These members should be treated as private. They aren't marked private because if they are, POD initializaer syntax can't be used. 00138 mutable T* m_p; 00139 mutable OnceFlag m_onceFlag; 00140 }; 00141 00146 #define BLOCXX_GLOBAL_PTR_INIT { 0, BLOCXX_ONCE_INIT } 00147 00148 00149 } // end namespace BLOCXX_NAMESPACE 00150 00151 00152 #endif 00153 00154