1 #ifndef CAFFE2_CORE_BLOB_H_ 2 #define CAFFE2_CORE_BLOB_H_ 10 #include "caffe2/core/blob_serializer_base.h" 11 #include "caffe2/core/common.h" 12 #include "caffe2/core/typeid.h" 13 #include "caffe2/core/logging.h" 14 #include "caffe2/proto/caffe2.pb.h" 27 typedef void (*DestroyCall)(
void*);
32 Blob() : meta_(), pointer_(nullptr) {}
36 : meta_(std::move(other.meta_)),
37 pointer_(std::move(other.pointer_)),
38 destroy_(std::move(other.destroy_)) {
40 other.pointer_ =
nullptr;
41 other.destroy_ =
nullptr;
44 Blob& operator=(
Blob&& other) noexcept {
45 meta_ = std::move(other.meta_);
46 pointer_ = std::move(other.pointer_);
47 destroy_ = std::move(other.destroy_);
49 other.pointer_ =
nullptr;
50 other.destroy_ =
nullptr;
58 bool IsType()
const {
return meta_.Match<T>(); }
75 const T&
Get()
const {
78 "wrong type for the Blob instance. Blob contains ",
80 " while caller expects ",
81 TypeMeta::TypeName<T>());
82 return *
static_cast<const T*
>(pointer_);
85 const void* GetRaw()
const {
103 if (is_new_object) *is_new_object =
false;
104 return static_cast<T*
>(pointer_);
106 if (is_new_object) *is_new_object =
true;
107 VLOG(1) <<
"Create new mutable object " << TypeMeta::TypeName<T>();
108 return Reset<T>(
new T());
122 if (pointer_ && destroy_) {
125 meta_ = TypeMeta::Make<T>();
126 pointer_ =
static_cast<void*
>(allocated);
127 destroy_ = &Destroy<T>;
133 if (pointer_ && destroy_) {
137 pointer_ =
static_cast<void*
>(allocated);
147 DestroyCall d = destroy_;
164 typename std::remove_const<T>::type* allocated) {
166 static_cast<void*>(allocated),
171 if (pointer_ && destroy_) {
175 pointer_ =
static_cast<void*
>(allocated);
184 if (pointer_ && destroy_) {
200 BlobSerializerBase::SerializationAcceptor acceptor,
201 int chunk_size = kDefaultChunkSize)
const;
213 string Serialize(
const string& name)
const;
220 swap(meta_, rhs.meta_);
221 swap(pointer_, rhs.pointer_);
222 swap(destroy_, rhs.destroy_);
238 static void Destroy(
void* pointer) {
239 delete static_cast<T*
>(pointer);
242 void* pointer_ =
nullptr;
243 DestroyCall destroy_ =
nullptr;
245 DISABLE_COPY_AND_ASSIGN(
Blob);
253 #endif // CAFFE2_CORE_BLOB_H_ Blob is a general container that hosts a typed pointer.
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...
const char * TypeName() const
Returns a printable typename of the blob.
void Reset()
Resets the Blob to an empty one.
void Serialize(const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size=kDefaultChunkSize) const
Serializes the current blob, if possible.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
DestroyCall Release()
Releases the ownership, if any, this Blob has on the underlying pointer.
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
const TypeMeta & meta() const
Returns the meta info of the blob.
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
void Deserialize(const string &content)
Deserializes from a string containing either BlobProto or TensorProto.
bool IsType() const
Checks if the content stored in the blob is of type T.
const T & Get() const
Gets the const reference of the stored object.
Blob()
Initializes an empty Blob.