1 #ifndef SYMENGINE_RCP_H
2 #define SYMENGINE_RCP_H
10 #include <symengine/symengine_config.h>
11 #include <symengine/symengine_assert.h>
13 #if defined(WITH_SYMENGINE_RCP)
15 #if defined(WITH_SYMENGINE_THREAD_SAFE)
22 #include <symengine/utilities/teuchos/Teuchos_RCP.hpp>
23 #include <symengine/utilities/teuchos/Teuchos_TypeNameTraits.hpp>
30 #if defined(WITH_SYMENGINE_RCP)
40 inline explicit Ptr(T *ptr) : ptr_(ptr)
42 SYMENGINE_ASSERT(ptr_ !=
nullptr)
44 inline Ptr(
const Ptr<T> &ptr) : ptr_(ptr.ptr_) {}
46 inline Ptr(
const Ptr<T2> &ptr) : ptr_(ptr.get())
49 Ptr<T> &operator=(
const Ptr<T> &ptr)
54 #if defined(HAVE_DEFAULT_CONSTRUCTORS)
55 inline Ptr(Ptr &&) =
default;
56 Ptr<T> &operator=(Ptr &&) =
default;
58 inline T *operator->()
const
62 inline T &operator*()
const
70 inline T *getRawPtr()
const
74 inline const Ptr<T> ptr()
const
84 inline Ptr<T> outArg(T &arg)
94 inline Ptr<T> ptrFromRef(T &arg)
109 RCP(ENull null_arg =
null) : ptr_(nullptr) {}
110 explicit RCP(T *p) : ptr_(p)
112 SYMENGINE_ASSERT(ptr_ !=
nullptr)
116 RCP(
const RCP<T> &rp) : ptr_(rp.ptr_)
123 RCP(
const RCP<T2> &r_ptr) : ptr_(r_ptr.get())
129 RCP(RCP<T> &&rp) SYMENGINE_NOEXCEPT : ptr_(rp.ptr_)
136 SYMENGINE_NOEXCEPT : ptr_(r_ptr.get())
140 ~RCP() SYMENGINE_NOEXCEPT
142 if (ptr_ !=
nullptr and --(ptr_->refcount_) == 0)
145 T *operator->()
const
147 SYMENGINE_ASSERT(ptr_ !=
nullptr)
152 SYMENGINE_ASSERT(ptr_ !=
nullptr)
161 return Ptr<T>(get());
165 return ptr_ ==
nullptr;
168 bool operator==(
const RCP<T2> &p2)
const
170 return ptr_ == p2.ptr_;
175 return ptr_ != p2.ptr_;
178 RCP<T> &operator=(
const RCP<T> &r_ptr)
180 T *r_ptr_ptr_ = r_ptr.ptr_;
181 if (not r_ptr.is_null())
182 (r_ptr_ptr_->refcount_)++;
183 if (not is_null() and --(ptr_->refcount_) == 0)
189 RCP<T> &operator=(RCP<T> &&r_ptr)
196 if (not is_null() and --(ptr_->refcount_) == 0)
211 inline RCP<T> rcp(T *p)
216 template <
class T2,
class T1>
217 inline RCP<T2> rcp_static_cast(
const RCP<T1> &p1)
220 T2 *check =
static_cast<T2 *
>(p1.get());
221 return RCP<T2>(check);
224 template <
class T2,
class T1>
225 inline RCP<T2> rcp_dynamic_cast(
const RCP<T1> &p1)
227 if (not p1.is_null()) {
230 p =
dynamic_cast<T2 *
>(p1.get());
238 template <
class T2,
class T1>
239 inline RCP<T2> rcp_const_cast(
const RCP<T1> &p1)
242 T2 *check =
const_cast<T2 *
>(p1.get());
243 return RCP<T2>(check);
247 inline bool operator==(
const RCP<T> &p, ENull)
249 return p.get() ==
nullptr;
252 template <
typename T>
258 void print_stack_on_segfault();
263 using Teuchos::outArg;
264 using Teuchos::print_stack_on_segfault;
266 using Teuchos::ptrFromRef;
269 using Teuchos::rcp_const_cast;
270 using Teuchos::rcp_dynamic_cast;
271 using Teuchos::rcp_static_cast;
272 using Teuchos::typeName;
284 #if defined(WITH_SYMENGINE_RCP)
285 return rcp(
static_cast<T *
>(
this));
287 return rcp_static_cast<T>(weak_self_ptr_.create_strong());
294 #if defined(WITH_SYMENGINE_RCP)
295 return rcp(
static_cast<const T *
>(
this));
297 return rcp_static_cast<const T>(weak_self_ptr_.create_strong());
305 #if defined(WITH_SYMENGINE_RCP)
306 return rcp(
static_cast<const T2 *
>(
this));
308 return rcp_static_cast<const T2>(weak_self_ptr_.create_strong());
312 unsigned int use_count()
const
314 #if defined(WITH_SYMENGINE_RCP)
317 return weak_self_ptr_.strong_count();
323 #if defined(WITH_SYMENGINE_RCP)
334 #if defined(WITH_SYMENGINE_THREAD_SAFE)
337 mutable unsigned int refcount_;
340 EnableRCPFromThis() : refcount_(0) {}
344 mutable RCP<T> weak_self_ptr_;
346 void set_weak_self_ptr(
const RCP<T> &w)
351 void set_weak_self_ptr(
const RCP<const T> &w)
const
353 weak_self_ptr_ = rcp_const_cast<T>(w);
357 #if defined(WITH_SYMENGINE_RCP)
362 template <
typename T_,
typename... Args>
363 friend inline RCP<T_> make_rcp(Args &&...args);
366 template <
typename T,
typename... Args>
367 inline RCP<T> make_rcp(Args &&...args)
369 #if defined(WITH_SYMENGINE_RCP)
370 return rcp(
new T(std::forward<Args>(args)...));
372 RCP<T> p = rcp(
new T(std::forward<Args>(args)...));
373 p->set_weak_self_ptr(p.create_weak());
RCP< const T2 > rcp_from_this_cast() const
Get RCP<T2> pointer to self (it will cast the pointer to T2)
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
RCP< const T > rcp_from_this() const
Get RCP<const T> pointer to self (it will cast the pointer to const T)
Main namespace for SymEngine package.