DLG4::VolumeBuilders
A fluent interface for Geant4 geometry definition.
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
Linkable< T > Class Template Reference

#include <Linkable.hh>

Classes

struct  false_template
 

Public Member Functions

 Linkable ()
 
 ~Linkable ()
 
 Linkable (const Linkable< T > &other)
 
 Linkable (Linkable< T > &other, std::true_type)
 
 Linkable (const T &other)
 
template<typename U = T>
std::enable_if_t<!std::is_pointer_v< U >, U * > operator-> () const
 
template<typename U = T>
std::enable_if_t< std::is_pointer_v< U >, std::remove_pointer_t< U > * > operator-> () const
 
template<typename U = T>
std::enable_if_t<!std::is_pointer_v< U >, U & > operator* () const
 
template<typename U = T>
std::enable_if_t< std::is_pointer_v< U >, std::remove_pointer_t< U > & > operator* () const
 
const Linkable< T > & operator= (const Linkable< T > &other) const
 
Linkableoperator= (const T &other) const
 
void LinkToRaw (T *ptr)
 
void Link (Linkable< T > &other)
 
void ShareLink (std::shared_ptr< T > other)
 
template<typename... Args, std::enable_if_t< !(std::conjunction_v< std::is_same< std::decay_t< Args >, Linkable< T > >... >), int > = 0>
void ConstructAndLink (Args &&... args)
 
template<typename... Args, std::enable_if_t< !(std::conjunction_v< std::is_same< std::decay_t< Args >, Linkable< T > >... >), int > = 0>
void ConstructAndRawLink (Args &&... args)
 
 operator bool () const noexcept
 
T * get_mutable () const
 
const T * get () const
 
void make_persistent ()
 
 operator T* () const
 

Detailed Description

template<typename T>
class DLG4::VolumeBuilders::_internals_::Linkable< T >

Definition at line 144 of file Linkable.hh.

Constructor & Destructor Documentation

◆ Linkable() [1/4]

template<typename T >
Linkable ( )
inline

Default constructor.

Definition at line 154 of file Linkable.hh.

155 : ref_() {
156 }

◆ ~Linkable()

template<typename T >
~Linkable ( )
inline

Definition at line 159 of file Linkable.hh.

159 {
160 std::lock_guard<std::recursive_mutex> lock(s_link_mutex);
161 // remove ourselves from the backlinks of the downlink
162 // this prevents corrupt backlink propagation after we are deleted.
163 if (downlink_) {
164 auto &parent_backlinks = downlink_->backlinks_;
165 auto it = std::remove(parent_backlinks.begin(), parent_backlinks.end(), this);
166 if (it != parent_backlinks.end()) {
167 parent_backlinks.erase(it, parent_backlinks.end());
168 }
169 }
170 // remove downlinks from our backlinks
171 // Does this orphan chains?:
172 // If we have a ref, link to will error anyway
173 // If we don't have a ref, we just become the root.
174 // No harm. Nothing linked, nothing lost.
175 // But backlinks become detatched from each other, have different views.
176 // So relink to the first backlink, making it the root.
177 Linkable<T> *first_link = nullptr;
178 for (auto *child : backlinks_) {
179 if (child->downlink_ == this) {
180 if (bool is_first_link = false; !is_first_link) {
181 first_link = child;
182 child->downlink_ = nullptr;
183 } else {
184 child->downlink_ = first_link;
185 }
186 }
187 }
188 backlinks_.clear();
189 //if this matters, something is already very wrong. But it can help unmask that.
190 }

◆ Linkable() [2/4]

template<typename T >
Linkable ( const Linkable< T > &  other)
inline

Copy constructor does deep copy, not link This is safest and default.

Definition at line 194 of file Linkable.hh.

194 :
195 ref_(std::make_shared<T>(*other.ref_)) {
196 }

◆ Linkable() [3/4]

template<typename T >
Linkable ( Linkable< T > &  other,
std::true_type   
)
inline

The EXPLICIT linking copy ctor.

Definition at line 199 of file Linkable.hh.

199 {
200 ref_ = nullptr;
201 LinkTreeTo(other);
202 }

◆ Linkable() [4/4]

template<typename T >
Linkable ( const T &  other)
inlineexplicit

Copy constructor does deep copy, not link This is safest and default.

Definition at line 206 of file Linkable.hh.

207 : ref_(std::make_shared<T>(other)) {
208 }

Member Function Documentation

◆ ConstructAndLink()

template<typename T >
template<typename... Args, std::enable_if_t< !(std::conjunction_v< std::is_same< std::decay_t< Args >, Linkable< T > >... >), int > = 0>
void ConstructAndLink ( Args &&...  args)
inline

Link and own a T and link to it.

Definition at line 289 of file Linkable.hh.

289 {
290 auto data = std::make_shared<T>(std::forward<Args>(args)...);
291 LinkTreeTo(data);
292 }

◆ ConstructAndRawLink()

template<typename T >
template<typename... Args, std::enable_if_t< !(std::conjunction_v< std::is_same< std::decay_t< Args >, Linkable< T > >... >), int > = 0>
void ConstructAndRawLink ( Args &&...  args)
inline

Non owning construct and link.

Definition at line 299 of file Linkable.hh.

299 {
300 auto raw_data = new T(std::forward<Args>(args)...);
301 LinkToRaw(raw_data);
302 }
void LinkToRaw(T *ptr)
Link to an existing T object via pointer (non-owning)
Definition Linkable.hh:254

◆ get()

template<typename T >
const T * get ( ) const
inline

Definition at line 313 of file Linkable.hh.

313 {
314 return ref_.get();
315 };

◆ get_mutable()

template<typename T >
T * get_mutable ( ) const
inline

Definition at line 309 of file Linkable.hh.

309 {
310 return ref_.get();
311 };

◆ Link()

template<typename T >
void Link ( Linkable< T > &  other)
inline

Reset Link to another Linakble:

Definition at line 265 of file Linkable.hh.

265 {
266 LinkTreeTo(other);
267 }

◆ LinkToRaw()

template<typename T >
void LinkToRaw ( T *  ptr)
inline

Link to an existing T object via pointer (non-owning)

Definition at line 254 of file Linkable.hh.

254 {
255 auto data = std::shared_ptr<T>(ptr, [](T *)
256 {
257 /* do nothing */
258 });
259 LinkTreeTo(data);
260 }

◆ make_persistent()

template<typename T >
void make_persistent ( )
inline

Definition at line 317 of file Linkable.hh.

317 {
318 std::lock_guard<std::mutex> lock(s_registry_mutex);
319 black_hole->push_back(ref_);
320 }

◆ operator bool()

template<typename T >
operator bool ( ) const
inlineexplicitnoexcept

Definition at line 305 of file Linkable.hh.

305 {
306 return static_cast<bool>(ref_);
307 }

◆ operator T*()

template<typename T >
operator T* ( ) const
inline

Definition at line 329 of file Linkable.hh.

329 {
330 if (!ref_) {
331 throw std::runtime_error("Attempt to convert empty Linkable to T*");
332 }
333 return ref_.get(); // Return a copy of the contained object
334 }

◆ operator*() [1/2]

template<typename T >
template<typename U = T>
std::enable_if_t<!std::is_pointer_v< U >, U & > operator* ( ) const
inline

operator* for non-pointer T: returns reference to T

Definition at line 230 of file Linkable.hh.

230 {
231 return *ref_;
232 }

◆ operator*() [2/2]

template<typename T >
template<typename U = T>
std::enable_if_t< std::is_pointer_v< U >, std::remove_pointer_t< U > & > operator* ( ) const
inline

operator* for pointer T: returns reference to the pointed-to object

Definition at line 237 of file Linkable.hh.

237 {
238 return **ref_;
239 }

◆ operator->() [1/2]

template<typename T >
template<typename U = T>
std::enable_if_t<!std::is_pointer_v< U >, U * > operator-> ( ) const
inline

-> foward For non-pointer T

Definition at line 216 of file Linkable.hh.

216 {
217 return ref_.get();
218 }

◆ operator->() [2/2]

template<typename T >
template<typename U = T>
std::enable_if_t< std::is_pointer_v< U >, std::remove_pointer_t< U > * > operator-> ( ) const
inline

operator-> forward For pointer T

Definition at line 223 of file Linkable.hh.

223 {
224 return *ref_;
225 }

◆ operator=() [1/2]

template<typename T >
const Linkable< T > & operator= ( const Linkable< T > &  other) const
inline

Allow CONTENT update (not reference) through assignment This is allowed even for const wrapper.

Definition at line 243 of file Linkable.hh.

243 {
244 *ref_ = *(other.ref_);
245 return *this; // return const ref to self.
246 }

◆ operator=() [2/2]

template<typename T >
Linkable & operator= ( const T &  other) const
inline

Definition at line 248 of file Linkable.hh.

248 {
249 *ref_ = other;
250 return *this;
251 }

◆ ShareLink()

template<typename T >
void ShareLink ( std::shared_ptr< T >  other)
inline

Definition at line 274 of file Linkable.hh.

274 {
275 if (!other) {
276 throw std::invalid_argument("\n Error in Linkable::Link: null pointer passed.\n");
277 }
278 auto data = other;
279 LinkTreeTo(data);
280 }

The documentation for this class was generated from the following file: