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>(); }
189 bool operator==(
const T& other)
const {
return *_safeGet() == other; }
205 bool operator!=(
const T& other)
const {
return *_safeGet() != other; }
214 *_safeGet() = std::move(other);
225 if ( &other !=
this )
226 *_safeGet() = *other._safeGet();
238 if ( &other !=
this ) {
241 *_safeGet() = *other._safeGet();
242 other._ptr =
nullptr;
272 const T* _get()
const {
273 if (
auto ptr = std::get_if<T*>(&_ptr) )
276 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
283 if (
auto ptr = std::get_if<T*>(&_ptr) )
286 if (
auto ptr = std::get_if<std::shared_ptr<T>>(&_ptr) )
292 const T* _safeGet()
const {
293 assert(_ptr.index() != std::variant_npos);
295 if (
auto ptr = _get() )
298 throw NullReference(
"attempt to access null reference");
302 assert(_ptr.index() != std::variant_npos);
304 if (
auto ptr = _get() )
307 throw NullReference(
"attempt to access null reference");
310 std::variant<std::shared_ptr<T>, T*> _ptr;
323 using Base = std::shared_ptr<T>;
357 bool isNull()
const {
return this->
get() ==
nullptr; }
369 void reset() { Base::operator=(
nullptr); }
412 explicit operator bool()
const {
return !
isNull(); }
421 Base::operator=(std::make_shared<T>(std::move(other)));
437 Base::operator=(other);
443 Base::operator=(std::move(other));
454 void _check()
const {
456 throw NullReference(
"attempt to access null reference");
472 using Base = std::weak_ptr<T>;
507 bool isNull()
const {
return this->lock() ==
nullptr; }
514 auto is_default = ! this->owner_before(Base{}) && ! Base{}.owner_before(*
this);
515 return this->expired() && ! is_default;
522 const T*
get()
const {
return this->lock().get(); }
541 return *this->lock();
552 return *this->lock();
563 return this->lock().get();
574 return this->lock().get();
578 explicit operator bool()
const {
return !
isNull(); }
596 Base::operator=(other);
608 Base::operator=(other);
614 Base::operator=(std::move(other));
619 void _check()
const {
621 throw ExpiredReference(
"attempt to access expired reference");
624 throw NullReference(
"attempt to access null reference");
653 }
catch (
const hilti::rt::bad_any_cast& ) {
654 throw IllegalReference(
"invalid target type");
665 namespace reference {
671 template<
typename T,
typename... Args>
680 template<
typename T,
typename... Args>
687 namespace detail::adl {
697 return "<expired ref>";
722 return "<expired ref>";
739 return x ? escapeBytes((*x).str(),
false) :
"Null";
743 inline std::string detail::to_string_for_print<WeakReference<Bytes>>(
const WeakReference<Bytes>& x) {
745 return "<expired ref>";
750 return escapeBytes((*x).str(),
false);
754 inline std::string detail::to_string_for_print<ValueReference<Bytes>>(
const ValueReference<Bytes>& x) {
755 return escapeBytes((*x).str(),
false);
759 inline std::ostream& operator<<(std::ostream& out, const StrongReference<T>& x) {
765 inline std::ostream& operator<<(std::ostream& out, const ValueReference<T>& x) {
771 inline std::ostream& operator<<(std::ostream& out, const WeakReference<T>& x) {
T & operator*()
Definition: reference.h:550
ValueReference(T t)
Definition: reference.h:61
WeakReference(const ValueReference< T > &t)
Definition: reference.h:482
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:369
bool operator==(const ValueReference< T > &other) const
Definition: reference.h:181
const T * operator->() const
Definition: reference.h:166
StrongReference(const ValueReference< T > &t)
Definition: reference.h:340
Definition: reference.h:633
Definition: reference.h:470
const T * operator->() const
Definition: reference.h:396
ValueReference & operator=(T other)
Definition: reference.h:213
WeakReference & operator=(const ValueReference< T > &other)
Definition: reference.h:585
T & operator*()
Definition: reference.h:159
Definition: optional.h:79
ValueReference()
Definition: reference.h:53
WeakReference & operator=(WeakReference &&other) noexcept
Definition: reference.h:613
StrongReference()
Definition: reference.h:326
bool operator!=(const T &other) const
Definition: reference.h:205
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:197
StrongReference(T t)
Definition: reference.h:333
const T & operator*() const
Definition: reference.h:376
ValueReference(std::shared_ptr< T > t)
Definition: reference.h:73
StrongReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:448
StrongReference & operator=(const StrongReference &other)
Definition: reference.h:436
ValueReference(const ValueReference &other)
Definition: reference.h:79
ValueReference< T > derefAsValue() const
Definition: reference.h:528
void reset()
Definition: reference.h:531
T * as() const
Definition: reference.h:647
T & operator*()
Definition: reference.h:386
T * operator->()
Definition: reference.h:572
Definition: reference.h:321
const T & operator*() const
Definition: reference.h:539
void reset()
Definition: reference.h:659
StrongReference(StrongReference &&other) noexcept
Definition: reference.h:352
WeakReference(std::nullptr_t)
Definition: reference.h:492
StrongReference(const StrongReference &other)
Definition: reference.h:349
Definition: reference.h:47
WeakReference & operator=(const WeakReference &other)
Definition: reference.h:607
T * operator->()
Definition: reference.h:406
std::enable_shared_from_this< T > Controllable
Definition: reference.h:20
bool isExpired() const
Definition: reference.h:513
~ValueReference()
Definition: reference.h:90
StrongReference & operator=(T other)
Definition: reference.h:420
WeakReference & operator=(std::nullptr_t) noexcept
Definition: reference.h:601
void reset()
Definition: reference.h:145
ValueReference & operator=(const ValueReference &other)
Definition: reference.h:224
bool operator==(const T &other) const
Definition: reference.h:189
const T & operator*() const
Definition: reference.h:152
WeakReference(const StrongReference< T > &t)
Definition: reference.h:489
bool isNull() const
Definition: reference.h:97
StrongReference & operator=(const ValueReference< T > &other)
Definition: reference.h:430
StrongReferenceGeneric(StrongReference< T > x)
Definition: reference.h:640
ValueReference< T > derefAsValue() const
Definition: reference.h:363
bool isNull() const
Definition: reference.h:507
const T * operator->() const
Definition: reference.h:561
ValueReference & operator=(ValueReference &&other) noexcept
Definition: reference.h:237
StrongReference & operator=(StrongReference &&other) noexcept
Definition: reference.h:442
StrongReference(std::nullptr_t)
Definition: reference.h:343
WeakReference & operator=(const StrongReference< T > &other)
Definition: reference.h:595
bool isNull() const
Definition: reference.h:357