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();
245 if ( &other !=
this ) {
248 *_safeGet() = *other._safeGet();
249 other._ptr =
nullptr;
279 const T* _get()
const {
280 if (
auto ptr = std::get_if<T*>(&_ptr) )
283 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
290 if (
auto ptr = std::get_if<T*>(&_ptr) )
293 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
299 const T* _safeGet()
const {
300 assert(_ptr.index() != std::variant_npos);
302 if (
auto ptr = _get() )
305 throw NullReference(
"attempt to access null reference");
309 assert(_ptr.index() != std::variant_npos);
311 if (
auto ptr = _get() )
314 throw NullReference(
"attempt to access null reference");
317 std::variant<std::shared_ptr<T>, T*> _ptr;
330 using Base = std::shared_ptr<T>;
364 bool isNull()
const {
return this->
get() ==
nullptr; }
376 void reset() { Base::operator=(
nullptr); }
419 explicit operator bool()
const {
return !
isNull(); }
428 Base::operator=(std::make_shared<T>(std::move(other)));
444 Base::operator=(other);
450 Base::operator=(std::move(other));
461 void _check()
const {
463 throw NullReference(
"attempt to access null reference");
479 using Base = std::weak_ptr<T>;
514 bool isNull()
const {
return this->lock() ==
nullptr; }
521 auto is_default = ! this->owner_before(Base{}) && ! Base{}.owner_before(*
this);
522 return this->expired() && ! is_default;
529 const T*
get()
const {
return this->lock().get(); }
548 return *this->lock();
559 return *this->lock();
570 return this->lock().get();
581 return this->lock().get();
585 explicit operator bool()
const {
return !
isNull(); }
603 Base::operator=(other);
615 Base::operator=(other);
621 Base::operator=(std::move(other));
626 void _check()
const {
628 throw ExpiredReference(
"attempt to access expired reference");
631 throw NullReference(
"attempt to access null reference");
660 }
catch (
const hilti::rt::bad_any_cast& ) {
661 throw IllegalReference(
"invalid target type");
672 namespace reference {
678 template<
typename T,
typename... Args>
687 template<
typename T,
typename... Args>
694 namespace detail::adl {
704 return "<expired ref>";
729 return "<expired ref>";
746 return x ? escapeBytes((*x).str(),
false) :
"Null";
750 inline std::string detail::to_string_for_print<WeakReference<Bytes>>(
const WeakReference<Bytes>& x) {
752 return "<expired ref>";
757 return escapeBytes((*x).str(),
false);
761 inline std::string detail::to_string_for_print<ValueReference<Bytes>>(
const ValueReference<Bytes>& x) {
762 return escapeBytes((*x).str(),
false);
766 inline std::ostream& operator<<(std::ostream& out, const StrongReference<T>& x) {
772 inline std::ostream& operator<<(std::ostream& out, const ValueReference<T>& x) {
778 inline std::ostream& operator<<(std::ostream& out, const WeakReference<T>& x) {
T & operator*()
Definition: reference.h:557
ValueReference(T t)
Definition: reference.h:61
WeakReference(const ValueReference< T > &t)
Definition: reference.h:489
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:376
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:347
Definition: reference.h:640
Definition: reference.h:477
const T * operator->() const
Definition: reference.h:403
ValueReference & operator=(T other)
Definition: reference.h:220
WeakReference & operator=(const ValueReference< T > &other)
Definition: reference.h:592
T & operator*()
Definition: reference.h:159
Definition: optional.h:79
ValueReference()
Definition: reference.h:53
WeakReference & operator=(WeakReference &&other) noexcept
Definition: reference.h:620
StrongReference()
Definition: reference.h:333
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:52
bool operator!=(const ValueReference< T > &other) const
Definition: reference.h:204
StrongReference(T t)
Definition: reference.h:340
const T & operator*() const
Definition: reference.h:383
ValueReference(std::shared_ptr< T > t)
Definition: reference.h:73
StrongReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:455
StrongReference & operator=(const StrongReference &other)
Definition: reference.h:443
ValueReference(const ValueReference &other)
Definition: reference.h:79
ValueReference< T > derefAsValue() const
Definition: reference.h:535
void reset()
Definition: reference.h:538
T * as() const
Definition: reference.h:654
T & operator*()
Definition: reference.h:393
T * operator->()
Definition: reference.h:579
Definition: reference.h:328
const T & operator*() const
Definition: reference.h:546
void reset()
Definition: reference.h:666
StrongReference(StrongReference &&other) noexcept
Definition: reference.h:359
WeakReference(std::nullptr_t)
Definition: reference.h:499
StrongReference(const StrongReference &other)
Definition: reference.h:356
Definition: reference.h:47
WeakReference & operator=(const WeakReference &other)
Definition: reference.h:614
T * operator->()
Definition: reference.h:413
std::enable_shared_from_this< T > Controllable
Definition: reference.h:20
bool isExpired() const
Definition: reference.h:520
~ValueReference()
Definition: reference.h:90
StrongReference & operator=(T other)
Definition: reference.h:427
WeakReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:608
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:496
bool isNull() const
Definition: reference.h:97
StrongReference & operator=(const ValueReference< T > &other)
Definition: reference.h:437
StrongReferenceGeneric(StrongReference< T > x)
Definition: reference.h:647
ValueReference< T > derefAsValue() const
Definition: reference.h:370
bool isNull() const
Definition: reference.h:514
const T * operator->() const
Definition: reference.h:568
ValueReference & operator=(ValueReference &&other) noexcept
Definition: reference.h:244
StrongReference & operator=(StrongReference &&other) noexcept
Definition: reference.h:449
StrongReference(std::nullptr_t)
Definition: reference.h:350
WeakReference & operator=(const StrongReference< T > &other)
Definition: reference.h:602
bool isNull() const
Definition: reference.h:364