shared-object.hpp
Go to the documentation of this file.00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2017 00008 * 00009 * This file is part of Gecode, the generic constraint 00010 * development environment: 00011 * http://www.gecode.org 00012 * 00013 * Permission is hereby granted, free of charge, to any person obtaining 00014 * a copy of this software and associated documentation files (the 00015 * "Software"), to deal in the Software without restriction, including 00016 * without limitation the rights to use, copy, modify, merge, publish, 00017 * distribute, sublicense, and/or sell copies of the Software, and to 00018 * permit persons to whom the Software is furnished to do so, subject to 00019 * the following conditions: 00020 * 00021 * The above copyright notice and this permission notice shall be 00022 * included in all copies or substantial portions of the Software. 00023 * 00024 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00025 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00026 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00027 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00028 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00029 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00030 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00031 * 00032 */ 00033 00034 namespace Gecode { 00035 00046 class SharedHandle { 00047 public: 00053 class GECODE_KERNEL_EXPORT Object : public HeapAllocated { 00054 friend class SharedHandle; 00055 private: 00057 Support::RefCount rc; 00058 public: 00060 Object(void); 00062 virtual ~Object(void); 00063 }; 00064 private: 00066 Object* o; 00068 void subscribe(void); 00070 void cancel(void); 00071 public: 00073 SharedHandle(void); 00075 SharedHandle(SharedHandle::Object* so); 00077 SharedHandle(const SharedHandle& sh); 00079 SharedHandle& operator =(const SharedHandle& sh); 00081 ~SharedHandle(void); 00083 explicit operator bool(void) const; 00084 protected: 00086 SharedHandle::Object* object(void) const; 00088 void object(SharedHandle::Object* n); 00089 }; 00090 00091 00092 forceinline 00093 SharedHandle::Object::Object(void) 00094 : rc(0) {} 00095 forceinline 00096 SharedHandle::Object::~Object(void) { 00097 assert(!rc); 00098 } 00099 00100 00101 forceinline SharedHandle::Object* 00102 SharedHandle::object(void) const { 00103 return o; 00104 } 00105 forceinline void 00106 SharedHandle::subscribe(void) { 00107 if (o != nullptr) o->rc.inc(); 00108 } 00109 forceinline void 00110 SharedHandle::cancel(void) { 00111 if ((o != nullptr) && o->rc.dec()) 00112 delete o; 00113 o=nullptr; 00114 } 00115 forceinline void 00116 SharedHandle::object(SharedHandle::Object* n) { 00117 if (n != o) { 00118 cancel(); o=n; subscribe(); 00119 } 00120 } 00121 forceinline 00122 SharedHandle::SharedHandle(void) : o(nullptr) {} 00123 forceinline 00124 SharedHandle::SharedHandle(SharedHandle::Object* so) : o(so) { 00125 subscribe(); 00126 } 00127 forceinline 00128 SharedHandle::SharedHandle(const SharedHandle& sh) : o(sh.o) { 00129 subscribe(); 00130 } 00131 forceinline SharedHandle& 00132 SharedHandle::operator =(const SharedHandle& sh) { 00133 if (&sh != this) { 00134 cancel(); o=sh.o; subscribe(); 00135 } 00136 return *this; 00137 } 00138 forceinline 00139 SharedHandle::operator bool(void) const { 00140 return o != nullptr; 00141 } 00142 forceinline 00143 SharedHandle::~SharedHandle(void) { 00144 cancel(); 00145 } 00146 00147 } 00148 00149 // STATISTICS: kernel-memory