Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
shared-object.hpp
Go to the documentation of this file.
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
47 public:
54 friend class SharedHandle;
55 private:
58 public:
60 Object(void);
62 virtual ~Object(void);
63 };
64 private:
66 Object* o;
68 void subscribe(void);
70 void cancel(void);
71 public:
73 SharedHandle(void);
77 SharedHandle(const SharedHandle& sh);
81 ~SharedHandle(void);
83 explicit operator bool(void) const;
84 protected:
86 SharedHandle::Object* object(void) const;
89 };
90
91
94 : rc(0) {}
97 assert(!rc);
98 }
99
100
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
117 if (n != o) {
118 cancel(); o=n; subscribe();
119 }
120 }
122 SharedHandle::SharedHandle(void) : o(nullptr) {}
125 subscribe();
126 }
129 subscribe();
130 }
133 if (&sh != this) {
134 cancel(); o=sh.o; subscribe();
135 }
136 return *this;
137 }
139 SharedHandle::operator bool(void) const {
140 return o != nullptr;
141 }
144 cancel();
145 }
146
147}
148
149// STATISTICS: kernel-memory
Base class for heap allocated objects.
Definition heap.hpp:340
virtual ~Object(void)
Delete shared object.
The shared handle.
SharedHandle & operator=(const SharedHandle &sh)
Assignment operator maintaining reference count.
SharedHandle(void)
Create shared handle with no object pointing to.
SharedHandle::Object * object(void) const
Access to the shared object.
~SharedHandle(void)
Destructor that maintains reference count.
A class for thread-safe reference counting.
Definition ref-count.hpp:39
bool dec(void)
Decrement counter and return true if it hits zero.
Definition ref-count.hpp:63
void inc(void)
Increment counter.
Definition ref-count.hpp:59
#define GECODE_KERNEL_EXPORT
Definition kernel.hh:70
Gecode toplevel namespace
#define forceinline
Definition config.hpp:194