9 #include <hilti/rt/any.h> 10 #include <hilti/rt/extension-points.h> 11 #include <hilti/rt/types/bytes.h> 12 #include <hilti/rt/types/string.h> 13 #include <hilti/rt/types/struct.h> 14 #include <hilti/rt/util.h> 80 if (
auto ptr = other._get() )
81 _ptr = std::make_shared<T>(*ptr);
83 _ptr = std::shared_ptr<T>();
98 assert(_ptr.index() != std::variant_npos);
99 return _get() ==
nullptr;
106 const T*
get()
const {
return _get(); }
122 assert(_ptr.index() != std::variant_npos);
124 if (
auto x = std::get_if<std::shared_ptr<T>>(&_ptr) )
128 if (
auto ptr = std::get<T*>(_ptr) ) {
130 return ptr->shared_from_this();
132 throw IllegalReference(
"cannot dynamically create reference for type");
135 throw IllegalReference(
"unexpected state of value reference");
136 }
catch (
const std::bad_weak_ptr& ) {
137 throw IllegalReference(
"reference to non-heap instance");
145 void reset() { _ptr = std::shared_ptr<T>(); }
180 operator const T&()
const {
return *_safeGet(); }
196 bool operator==(
const T& other)
const {
return *_safeGet() == other; }
212 bool operator!=(
const T& other)
const {
return *_safeGet() != other; }
221 *_safeGet() = std::move(other);
232 if ( &other !=
this )
233 *_safeGet() = *other._safeGet();
243 if ( &other !=
this ) {
246 *_get() = *other._get();
251 other._ptr =
nullptr;
266 if ( _get() != other.get() )
267 _ptr = std::move(other);
296 const T* _get()
const noexcept {
297 if (
auto ptr = std::get_if<T*>(&_ptr) )
300 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
307 if (
auto ptr = std::get_if<T*>(&_ptr) )
310 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
316 const T* _safeGet()
const {
317 assert(_ptr.index() != std::variant_npos);
319 if (
auto ptr = _get() )
322 throw NullReference(
"attempt to access null reference");
326 assert(_ptr.index() != std::variant_npos);
328 if (
auto ptr = _get() )
331 throw NullReference(
"attempt to access null reference");
334 std::variant<std::shared_ptr<T>, T*> _ptr;
347 using Base = std::shared_ptr<T>;
381 bool isNull()
const {
return this->
get() ==
nullptr; }
393 void reset() { Base::operator=(
nullptr); }
436 explicit operator bool()
const {
return !
isNull(); }
445 Base::operator=(std::make_shared<T>(std::move(other)));
461 if ( &other ==
this )
464 Base::operator=(other);
470 Base::operator=(std::move(other));
481 void _check()
const {
483 throw NullReference(
"attempt to access null reference");
499 using Base = std::weak_ptr<T>;
534 bool isNull()
const {
return this->lock() ==
nullptr; }
541 auto is_default = ! this->owner_before(Base{}) && ! Base{}.owner_before(*
this);
542 return this->expired() && ! is_default;
549 const T*
get()
const {
return this->lock().get(); }
568 return *this->lock();
579 return *this->lock();
590 return this->lock().get();
601 return this->lock().get();
605 explicit operator bool()
const {
return !
isNull(); }
623 Base::operator=(other);
635 if ( &other ==
this )
638 Base::operator=(other);
644 Base::operator=(std::move(other));
649 void _check()
const {
651 throw ExpiredReference(
"attempt to access expired reference");
654 throw NullReference(
"attempt to access null reference");
683 }
catch (
const hilti::rt::bad_any_cast& ) {
684 throw IllegalReference(
"invalid target type");
695 namespace reference {
701 template<
typename T,
typename... Args>
710 template<
typename T,
typename... Args>
717 namespace detail::adl {
727 return "<expired ref>";
752 return "<expired ref>";
769 return x ? escapeBytes((*x).str(),
false) :
"Null";
773 inline std::string detail::to_string_for_print<WeakReference<Bytes>>(
const WeakReference<Bytes>& x) {
775 return "<expired ref>";
780 return escapeBytes((*x).str(),
false);
784 inline std::string detail::to_string_for_print<ValueReference<Bytes>>(
const ValueReference<Bytes>& x) {
785 return escapeBytes((*x).str(),
false);
789 inline std::ostream& operator<<(std::ostream& out, const StrongReference<T>& x) {
795 inline std::ostream& operator<<(std::ostream& out, const ValueReference<T>& x) {
801 inline std::ostream& operator<<(std::ostream& out, const WeakReference<T>& x) {
T & operator*()
Definition: reference.h:577
ValueReference(T t)
Definition: reference.h:61
WeakReference(const ValueReference< T > &t)
Definition: reference.h:509
std::string to_string(T &&x)
Definition: extension-points.h:26
std::string to_string_for_print(const T &x)
Definition: extension-points.h:45
std::shared_ptr< T > asSharedPtr() const
Definition: reference.h:121
void reset()
Definition: reference.h:393
bool operator==(const ValueReference< T > &other) const
Definition: reference.h:188
const T * operator->() const
Definition: reference.h:166
StrongReference(const ValueReference< T > &t)
Definition: reference.h:357
Definition: reference.h:663
Definition: reference.h:497
const T * operator->() const
Definition: reference.h:420
ValueReference & operator=(T other)
Definition: reference.h:220
WeakReference & operator=(const ValueReference< T > &other)
Definition: reference.h:612
T & operator*()
Definition: reference.h:159
Definition: optional.h:79
ValueReference()
Definition: reference.h:53
WeakReference & operator=(WeakReference &&other) noexcept
Definition: reference.h:643
StrongReference()
Definition: reference.h:350
bool operator!=(const T &other) const
Definition: reference.h:212
T * operator->()
Definition: reference.h:173
void cannot_be_reached() __attribute__((noreturn))
Definition: util.cc:42
bool operator!=(const ValueReference< T > &other) const
Definition: reference.h:204
StrongReference(T t)
Definition: reference.h:364
const T & operator*() const
Definition: reference.h:400
ValueReference(std::shared_ptr< T > t)
Definition: reference.h:73
StrongReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:475
ValueReference & operator=(std::shared_ptr< T > other) noexcept
Definition: reference.h:265
StrongReference & operator=(const StrongReference &other)
Definition: reference.h:460
ValueReference(const ValueReference &other)
Definition: reference.h:79
ValueReference< T > derefAsValue() const
Definition: reference.h:555
void reset()
Definition: reference.h:558
T * as() const
Definition: reference.h:677
T & operator*()
Definition: reference.h:410
T * operator->()
Definition: reference.h:599
Definition: reference.h:345
const T & operator*() const
Definition: reference.h:566
void reset()
Definition: reference.h:689
StrongReference(StrongReference &&other) noexcept
Definition: reference.h:376
WeakReference(std::nullptr_t)
Definition: reference.h:519
StrongReference(const StrongReference &other)
Definition: reference.h:373
Definition: reference.h:47
WeakReference & operator=(const WeakReference &other)
Definition: reference.h:634
T * operator->()
Definition: reference.h:430
std::enable_shared_from_this< T > Controllable
Definition: reference.h:20
bool isExpired() const
Definition: reference.h:540
~ValueReference()
Definition: reference.h:90
StrongReference & operator=(T other)
Definition: reference.h:444
WeakReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:628
void reset()
Definition: reference.h:145
ValueReference & operator=(const ValueReference &other)
Definition: reference.h:231
bool operator==(const T &other) const
Definition: reference.h:196
const T & operator*() const
Definition: reference.h:152
WeakReference(const StrongReference< T > &t)
Definition: reference.h:516
bool isNull() const
Definition: reference.h:97
StrongReference & operator=(const ValueReference< T > &other)
Definition: reference.h:454
StrongReferenceGeneric(StrongReference< T > x)
Definition: reference.h:670
ValueReference< T > derefAsValue() const
Definition: reference.h:387
bool isNull() const
Definition: reference.h:534
const T * operator->() const
Definition: reference.h:588
ValueReference & operator=(ValueReference &&other) noexcept
Definition: reference.h:242
StrongReference & operator=(StrongReference &&other) noexcept
Definition: reference.h:469
StrongReference(std::nullptr_t)
Definition: reference.h:367
WeakReference & operator=(const StrongReference< T > &other)
Definition: reference.h:622
bool isNull() const
Definition: reference.h:381