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;
261 if ( _get() != other.get() )
262 _ptr = std::move(other);
291 const T* _get()
const {
292 if (
auto ptr = std::get_if<T*>(&_ptr) )
295 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
302 if (
auto ptr = std::get_if<T*>(&_ptr) )
305 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
311 const T* _safeGet()
const {
312 assert(_ptr.index() != std::variant_npos);
314 if (
auto ptr = _get() )
317 throw NullReference(
"attempt to access null reference");
321 assert(_ptr.index() != std::variant_npos);
323 if (
auto ptr = _get() )
326 throw NullReference(
"attempt to access null reference");
329 std::variant<std::shared_ptr<T>, T*> _ptr;
342 using Base = std::shared_ptr<T>;
376 bool isNull()
const {
return this->
get() ==
nullptr; }
388 void reset() { Base::operator=(
nullptr); }
431 explicit operator bool()
const {
return !
isNull(); }
440 Base::operator=(std::make_shared<T>(std::move(other)));
456 Base::operator=(other);
462 Base::operator=(std::move(other));
473 void _check()
const {
475 throw NullReference(
"attempt to access null reference");
491 using Base = std::weak_ptr<T>;
526 bool isNull()
const {
return this->lock() ==
nullptr; }
533 auto is_default = ! this->owner_before(Base{}) && ! Base{}.owner_before(*
this);
534 return this->expired() && ! is_default;
541 const T*
get()
const {
return this->lock().get(); }
560 return *this->lock();
571 return *this->lock();
582 return this->lock().get();
593 return this->lock().get();
597 explicit operator bool()
const {
return !
isNull(); }
615 Base::operator=(other);
627 Base::operator=(other);
633 Base::operator=(std::move(other));
638 void _check()
const {
640 throw ExpiredReference(
"attempt to access expired reference");
643 throw NullReference(
"attempt to access null reference");
672 }
catch (
const hilti::rt::bad_any_cast& ) {
673 throw IllegalReference(
"invalid target type");
684 namespace reference {
690 template<
typename T,
typename... Args>
699 template<
typename T,
typename... Args>
706 namespace detail::adl {
716 return "<expired ref>";
741 return "<expired ref>";
758 return x ? escapeBytes((*x).str(),
false) :
"Null";
762 inline std::string detail::to_string_for_print<WeakReference<Bytes>>(
const WeakReference<Bytes>& x) {
764 return "<expired ref>";
769 return escapeBytes((*x).str(),
false);
773 inline std::string detail::to_string_for_print<ValueReference<Bytes>>(
const ValueReference<Bytes>& x) {
774 return escapeBytes((*x).str(),
false);
778 inline std::ostream& operator<<(std::ostream& out, const StrongReference<T>& x) {
784 inline std::ostream& operator<<(std::ostream& out, const ValueReference<T>& x) {
790 inline std::ostream& operator<<(std::ostream& out, const WeakReference<T>& x) {
T & operator*()
Definition: reference.h:569
ValueReference(T t)
Definition: reference.h:61
WeakReference(const ValueReference< T > &t)
Definition: reference.h:501
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:388
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:352
Definition: reference.h:652
Definition: reference.h:489
const T * operator->() const
Definition: reference.h:415
ValueReference & operator=(T other)
Definition: reference.h:220
WeakReference & operator=(const ValueReference< T > &other)
Definition: reference.h:604
T & operator*()
Definition: reference.h:159
Definition: optional.h:79
ValueReference()
Definition: reference.h:53
WeakReference & operator=(WeakReference &&other) noexcept
Definition: reference.h:632
StrongReference()
Definition: reference.h:345
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:359
const T & operator*() const
Definition: reference.h:395
ValueReference(std::shared_ptr< T > t)
Definition: reference.h:73
StrongReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:467
ValueReference & operator=(std::shared_ptr< T > other) noexcept
Definition: reference.h:260
StrongReference & operator=(const StrongReference &other)
Definition: reference.h:455
ValueReference(const ValueReference &other)
Definition: reference.h:79
ValueReference< T > derefAsValue() const
Definition: reference.h:547
void reset()
Definition: reference.h:550
T * as() const
Definition: reference.h:666
T & operator*()
Definition: reference.h:405
T * operator->()
Definition: reference.h:591
Definition: reference.h:340
const T & operator*() const
Definition: reference.h:558
void reset()
Definition: reference.h:678
StrongReference(StrongReference &&other) noexcept
Definition: reference.h:371
WeakReference(std::nullptr_t)
Definition: reference.h:511
StrongReference(const StrongReference &other)
Definition: reference.h:368
Definition: reference.h:47
WeakReference & operator=(const WeakReference &other)
Definition: reference.h:626
T * operator->()
Definition: reference.h:425
std::enable_shared_from_this< T > Controllable
Definition: reference.h:20
bool isExpired() const
Definition: reference.h:532
~ValueReference()
Definition: reference.h:90
StrongReference & operator=(T other)
Definition: reference.h:439
WeakReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:620
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:508
bool isNull() const
Definition: reference.h:97
StrongReference & operator=(const ValueReference< T > &other)
Definition: reference.h:449
StrongReferenceGeneric(StrongReference< T > x)
Definition: reference.h:659
ValueReference< T > derefAsValue() const
Definition: reference.h:382
bool isNull() const
Definition: reference.h:526
const T * operator->() const
Definition: reference.h:580
ValueReference & operator=(ValueReference &&other) noexcept
Definition: reference.h:244
StrongReference & operator=(StrongReference &&other) noexcept
Definition: reference.h:461
StrongReference(std::nullptr_t)
Definition: reference.h:362
WeakReference & operator=(const StrongReference< T > &other)
Definition: reference.h:614
bool isNull() const
Definition: reference.h:376