this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2017 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34namespace Gecode { 35 36 /** 37 * \brief The shared handle 38 * 39 * A shared handle provides access to an object that lives outside a space, 40 * and is shared between entities that possibly reside inside different 41 * spaces. 42 * 43 * This is the base class that all shared handles must inherit from. 44 * 45 */ 46 class SharedHandle { 47 public: 48 /** 49 * \brief The shared object 50 * 51 * Shared objects must inherit from this base class. 52 */ 53 class GECODE_KERNEL_EXPORT Object : public HeapAllocated { 54 friend class SharedHandle; 55 private: 56 /// The counter used for reference counting 57 Support::RefCount rc; 58 public: 59 /// Initialize 60 Object(void); 61 /// Delete shared object 62 virtual ~Object(void); 63 }; 64 private: 65 /// The shared object 66 Object* o; 67 /// Subscribe handle to object 68 void subscribe(void); 69 /// Cancel subscription of handle to object 70 void cancel(void); 71 public: 72 /// Create shared handle with no object pointing to 73 SharedHandle(void); 74 /// Create shared handle that points to shared object \a so 75 SharedHandle(SharedHandle::Object* so); 76 /// Copy constructor maintaining reference count 77 SharedHandle(const SharedHandle& sh); 78 /// Assignment operator maintaining reference count 79 SharedHandle& operator =(const SharedHandle& sh); 80 /// Destructor that maintains reference count 81 ~SharedHandle(void); 82 /// Whether handle points to an object 83 explicit operator bool(void) const; 84 protected: 85 /// Access to the shared object 86 SharedHandle::Object* object(void) const; 87 /// Modify shared object 88 void object(SharedHandle::Object* n); 89 }; 90 91 92 forceinline 93 SharedHandle::Object::Object(void) 94 : rc(0) {} 95 forceinline 96 SharedHandle::Object::~Object(void) { 97 assert(!rc); 98 } 99 100 101 forceinline SharedHandle::Object* 102 SharedHandle::object(void) const { 103 return o; 104 } 105 forceinline void 106 SharedHandle::subscribe(void) { 107 if (o != nullptr) o->rc.inc(); 108 } 109 forceinline void 110 SharedHandle::cancel(void) { 111 if ((o != nullptr) && o->rc.dec()) 112 delete o; 113 o=nullptr; 114 } 115 forceinline void 116 SharedHandle::object(SharedHandle::Object* n) { 117 if (n != o) { 118 cancel(); o=n; subscribe(); 119 } 120 } 121 forceinline 122 SharedHandle::SharedHandle(void) : o(nullptr) {} 123 forceinline 124 SharedHandle::SharedHandle(SharedHandle::Object* so) : o(so) { 125 subscribe(); 126 } 127 forceinline 128 SharedHandle::SharedHandle(const SharedHandle& sh) : o(sh.o) { 129 subscribe(); 130 } 131 forceinline SharedHandle& 132 SharedHandle::operator =(const SharedHandle& sh) { 133 if (&sh != this) { 134 cancel(); o=sh.o; subscribe(); 135 } 136 return *this; 137 } 138 forceinline 139 SharedHandle::operator bool(void) const { 140 return o != nullptr; 141 } 142 forceinline 143 SharedHandle::~SharedHandle(void) { 144 cancel(); 145 } 146 147} 148 149// STATISTICS: kernel-memory