9 #include <c10/util/intrusive_ptr.h> 10 #include <c10/util/typeid.h> 11 #include <c10/macros/Macros.h> 29 Blob() noexcept : meta_(), pointer_(
nullptr), has_ownership_(false) {}
38 Blob& operator=(
Blob&& other) noexcept {
48 return meta_.Match<
T>();
74 "wrong type for the Blob instance. Blob contains ",
76 " while caller expects ",
77 TypeMeta::TypeName<T>());
81 return *
static_cast<const T*
>(pointer_);
84 const void* GetRaw()
const noexcept {
87 void* GetRaw() noexcept {
102 std::is_default_constructible<T>::value,
103 "GetMutable can't be called with non-default-constructible types. " 104 "Try using specialized methods");
106 return static_cast<T*
>(pointer_);
110 return Reset<T>(
new T());
115 T* GetMutableOrNull() {
117 return static_cast<T*
>(pointer_);
134 meta_ = TypeMeta::Make<T>();
135 pointer_ =
static_cast<void*
>(allocated);
136 has_ownership_ =
true;
152 typename std::remove_const<T>::type* allocated) {
153 return static_cast<T*
>(ShareExternal(
154 static_cast<void*>(allocated),
159 void* ShareExternal(
void* allocated,
const TypeMeta& meta) {
162 pointer_ =
static_cast<void*
>(allocated);
163 has_ownership_ =
false;
174 has_ownership_ =
false;
182 swap(meta_, rhs.meta_);
183 swap(pointer_, rhs.pointer_);
184 swap(has_ownership_, rhs.has_ownership_);
189 if (has_ownership_) {
190 AT_ASSERTM(pointer_ !=
nullptr,
"Can't have ownership of nullptr");
191 (*meta_.deleteFn())(pointer_);
196 void* pointer_ =
nullptr;
197 bool has_ownership_ =
false;
199 C10_DISABLE_COPY_AND_ASSIGN(
Blob);
202 inline void swap(
Blob& lhs,
Blob& rhs) {
206 inline std::ostream& operator<<(std::ostream& out,
const Blob& v) {
207 return out <<
"Blob[" << v.
TypeName() <<
"]";
Blob is a general container that hosts a typed pointer.
bool IsType() const noexcept
Checks if the content stored in the blob is of type T.
std::remove_const< T >::type * ShareExternal(typename std::remove_const< T >::type *allocated)
Sets the underlying object to the allocated one, but does not take over the ownership of the passed i...
Blob() noexcept
Initializes an empty Blob.
void Reset()
Resets the Blob to an empty one.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
const TypeMeta & meta() const noexcept
Returns the meta info of the blob.
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
intrusive_ptr<T> is an alternative to shared_ptr<T> that has better performance because it does the r...
T * GetMutable()
Gets a mutable pointer to the stored object.
const char * TypeName() const noexcept
Returns a printable typename of the blob.
const T & Get() const
Gets the const reference of the stored object.