/usr/local/lib64/python3.6/site-packages/torch/include/c10/util
NameSizeModeActions
accumulate.h42050644editdlrm
AlignOf.h48350644editdlrm
Array.h113540644editdlrm
ArrayRef.h90580644editdlrm
Backtrace.h3640644editdlrm
BFloat16-inl.h92220644editdlrm
BFloat16-math.h52170644editdlrm
BFloat16.h23720644editdlrm
Bitset.h34140644editdlrm
C++17.h133290644editdlrm
complex.h175420644editdlrm
complex_math.h109020644editdlrm
complex_utils.h9580644editdlrm
ConstexprCrc.h66330644editdlrm
copysign.h8660644editdlrm
DeadlockDetection.h19200644editdlrm
Deprecated.h35790644editdlrm
either.h64230644editdlrm
env.h8350644editdlrm
Exception.h247230644editdlrm
ExclusivelyOwned.h47590644editdlrm
Flags.h100560644editdlrm
flat_hash_map.h616550644editdlrm
FunctionRef.h23010644editdlrm
Half-inl.h86740644editdlrm
Half.h189650644editdlrm
hash.h50840644editdlrm
IdWrapper.h23480644editdlrm
intrusive_ptr.h355630644editdlrm
in_place.h3500644editdlrm
irange.h26790644editdlrm
LeftRight.h60160644editdlrm
llvmMathExtras.h291680644editdlrm
Logging.h112560644editdlrm
logging_is_google_glog.h20310644editdlrm
logging_is_not_google_glog.h82710644editdlrm
MathConstants.h8580644editdlrm
math_compat.h72960644editdlrm
MaybeOwned.h66890644editdlrm
Metaprogramming.h152880644editdlrm
numa.h6960644editdlrm
Optional.h355920644editdlrm
order_preserving_flat_hash_map.h654820644editdlrm
overloaded.h7090644editdlrm
python_stub.h560644editdlrm
qint8.h4720644editdlrm
qint32.h3190644editdlrm
quint4x2.h3660644editdlrm
quint8.h3200644editdlrm
Registry.h122420644editdlrm
reverse_iterator.h87960644editdlrm
ScopeExit.h13450644editdlrm
signal_handler.h31540644editdlrm
SmallBuffer.h12430644editdlrm
SmallVector.h344560644editdlrm
sparse_bitset.h265110644editdlrm
StringUtil.h45380644editdlrm
string_utils.h39890644editdlrm
string_view.h201890644editdlrm
tempfile.h60290644editdlrm
ThreadLocal.h38830644editdlrm
ThreadLocalDebugInfo.h26030644editdlrm
thread_name.h1480644editdlrm
Type.h6070644editdlrm
TypeCast.h69720644editdlrm
typeid.h187930644editdlrm
TypeIndex.h52510644editdlrm
TypeList.h169010644editdlrm
TypeTraits.h53680644editdlrm
Unicode.h2950644editdlrm
UniqueVoidPtr.h41170644editdlrm
Unroll.h6670644editdlrm
variant.h951820644editdlrm
win32-headers.h8580644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/include/c10/util/MaybeOwned.h (6689B)
#pragma once #include #include #include #include namespace c10 { /// Traits class describing how to borrow from T. As a synopsis, here /// is how we might implement borrowing from an arbitrary type T using /// a raw pointer to const: /// /// template /// struct MaybeOwnedTraits { /// using owned_type = T; /// using borrow_type = const T*; /// /// static borrow_type createBorrow(const owned_type& from) { /// return &from; /// } /// /// static void assignBorrow(borrow_type& lhs, borrow_type rhs) { /// lhs = rhs; /// } /// /// static void destroyBorrow(borrow_type& toDestroy) {} /// /// static const owned_type& referenceFromBorrow(const borrow_type& borrow) { /// return *borrow; /// } /// /// static const owned_type* pointerFromBorrow(const borrow_type& borrow) { /// return borrow; /// } /// /// static bool debugBorrowIsValid(const borrow_type& borrow) { /// return borrow != nullptr; /// } /// }; /// /// (This implementation is not in use because MaybeOwned is an unsafe /// abstraction and we don't want to encourage it widely, just for /// skipping reference counting in critical paths.) /// /// For examples that are in use, see intrusive_ptr.h and TensorBody.h. template struct MaybeOwnedTraits; /// A smart pointer around either a borrowed or owned T. When /// constructed with borrowed(), the caller MUST ensure that the /// borrowed-from argument outlives this MaybeOwned. Compare to /// Rust's std::borrow::Cow /// (https://doc.rust-lang.org/std/borrow/enum.Cow.html), but note /// that it is probably not suitable for general use because C++ has /// no borrow checking. Included here to support /// Tensor::expect_contiguous. template class MaybeOwned final { using borrow_type = typename MaybeOwnedTraits::borrow_type; using owned_type = typename MaybeOwnedTraits::owned_type; bool isBorrowed_; union { borrow_type borrow_; owned_type own_; }; /// Don't use this; use borrowed() instead. explicit MaybeOwned(const owned_type& t) : isBorrowed_(true), borrow_(MaybeOwnedTraits::createBorrow(t)) {} /// Don't use this; use owned() instead. explicit MaybeOwned(T&& t) noexcept( std::is_nothrow_move_constructible::value) : isBorrowed_(false), own_(std::move(t)) {} /// Don't use this; use owned() instead. template explicit MaybeOwned(in_place_t, Args&&... args) : isBorrowed_(false), own_(std::forward(args)...) {} public: explicit MaybeOwned() : isBorrowed_(true), borrow_() {} // Copying a borrow yields another borrow of the original, as with a // T*. Copying an owned T yields another owned T for safety: no // chains of borrowing by default! (Note you could get that behavior // with MaybeOwned::borrowed(*rhs) if you wanted it.) MaybeOwned(const MaybeOwned& rhs) : isBorrowed_(rhs.isBorrowed_) { if (C10_LIKELY(rhs.isBorrowed_)) { MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); } else { new (&own_) T(rhs.own_); } } MaybeOwned& operator=(const MaybeOwned& rhs) { if (this == &rhs) { return *this; } if (C10_UNLIKELY(!isBorrowed_)) { if (rhs.isBorrowed_) { own_.~T(); MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); isBorrowed_ = true; } else { own_ = rhs.own_; } } else { if (C10_LIKELY(rhs.isBorrowed_)) { MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); } else { MaybeOwnedTraits::destroyBorrow(borrow_); new (&own_) T(rhs.own_); isBorrowed_ = false; } } TORCH_INTERNAL_ASSERT_DEBUG_ONLY(isBorrowed_ == rhs.isBorrowed_); return *this; } MaybeOwned(MaybeOwned&& rhs) noexcept( std::is_nothrow_move_constructible::value) : isBorrowed_(rhs.isBorrowed_) { if (C10_LIKELY(rhs.isBorrowed_)) { MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); } else { new (&own_) T(std::move(rhs.own_)); } } MaybeOwned& operator=(MaybeOwned&& rhs) noexcept( std::is_nothrow_move_assignable::value) { if (this == &rhs) { return *this; } if (C10_UNLIKELY(!isBorrowed_)) { if (rhs.isBorrowed_) { own_.~T(); MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); isBorrowed_ = true; } else { own_ = std::move(rhs.own_); } } else { if (C10_LIKELY(rhs.isBorrowed_)) { MaybeOwnedTraits::assignBorrow(borrow_, rhs.borrow_); } else { MaybeOwnedTraits::destroyBorrow(borrow_); new (&own_) T(std::move(rhs.own_)); isBorrowed_ = false; } } TORCH_INTERNAL_ASSERT_DEBUG_ONLY(isBorrowed_ == rhs.isBorrowed_); return *this; } static MaybeOwned borrowed(const T& t) { return MaybeOwned(t); } static MaybeOwned owned(T&& t) noexcept( std::is_nothrow_move_constructible::value) { return MaybeOwned(std::move(t)); } template static MaybeOwned owned(in_place_t, Args&&... args) { return MaybeOwned(in_place, std::forward(args)...); } ~MaybeOwned() { if (C10_UNLIKELY(!isBorrowed_)) { own_.~T(); } else { MaybeOwnedTraits::destroyBorrow(borrow_); } } // This is an implementation detail! You should know what you're doing // if you are testing this. If you just want to guarantee ownership move // this into a T bool unsafeIsBorrowed() const { return isBorrowed_; } const T& operator*() const& { if (isBorrowed_) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY( MaybeOwnedTraits::debugBorrowIsValid(borrow_)); } return C10_LIKELY(isBorrowed_) ? MaybeOwnedTraits::referenceFromBorrow(borrow_) : own_; } const T* operator->() const { if (isBorrowed_) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY( MaybeOwnedTraits::debugBorrowIsValid(borrow_)); } return C10_LIKELY(isBorrowed_) ? MaybeOwnedTraits::pointerFromBorrow(borrow_) : &own_; } // If borrowed, copy the underlying T. If owned, move from // it. borrowed/owned state remains the same, and either we // reference the same borrow as before or we are an owned moved-from // T. T operator*() && { if (isBorrowed_) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY( MaybeOwnedTraits::debugBorrowIsValid(borrow_)); return MaybeOwnedTraits::referenceFromBorrow(borrow_); } else { return std::move(own_); } } }; } // namespace c10