Generated on Fri Jan 10 2020 11:38:25 for Gecode by doxygen 1.8.16
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 
34 namespace Gecode {
35 
46  class SharedHandle {
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 
102  SharedHandle::object(void) const {
103  return o;
104  }
105  forceinline 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
Object(void)
Initialize.
SharedHandle::Object * object(void) const
Access to the shared object.
The shared object.
RegSimpleC rc
Gecode toplevel namespace
bool dec(void)
Decrement counter and return true if it hits zero.
Definition: ref-count.hpp:63
A class for thread-safe reference counting.
Definition: ref-count.hpp:39
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
Base class for heap allocated objects.
Definition: heap.hpp:340
void inc(void)
Increment counter.
Definition: ref-count.hpp:59
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:81
~SharedHandle(void)
Destructor that maintains reference count.
virtual ~Object(void)
Delete shared object.
#define forceinline
Definition: config.hpp:185
SharedHandle & operator=(const SharedHandle &sh)
Assignment operator maintaining reference count.
The shared handle.
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:71
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
SharedHandle(void)
Create shared handle with no object pointing to.