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" 30 Blob() : meta_(), pointer_(nullptr) {}
34 : meta_(std::move(other.meta_)),
35 pointer_(std::move(other.pointer_)),
36 destroy_(std::move(other.destroy_)) {
38 other.pointer_ =
nullptr;
39 other.destroy_ =
nullptr;
42 Blob& operator=(
Blob&& other) noexcept {
43 meta_ = std::move(other.meta_);
44 pointer_ = std::move(other.pointer_);
45 destroy_ = std::move(other.destroy_);
47 other.pointer_ =
nullptr;
48 other.destroy_ =
nullptr;
56 bool IsType()
const {
return meta_.Match<T>(); }
73 const T&
Get()
const {
74 CAFFE_ENFORCE(IsType<T>(),
75 "wrong type for the Blob instance. Blob contains ",
76 meta_.
name(),
" while caller expects ", TypeMeta::Name<T>());
77 return *
static_cast<const T*
>(pointer_);
80 const void* GetRaw()
const {
98 if (is_new_object) *is_new_object =
false;
99 return static_cast<T*
>(pointer_);
101 if (is_new_object) *is_new_object =
true;
102 VLOG(1) <<
"Create new mutable object " << TypeMeta::Name<T>();
103 return Reset<T>(
new T());
117 if (pointer_ && destroy_) {
120 meta_ = TypeMeta::Make<T>();
121 pointer_ =
static_cast<void*
>(allocated);
122 destroy_ = &Destroy<T>;
138 typename std::remove_const<T>::type* allocated) {
140 static_cast<void*>(allocated),
145 if (pointer_ && destroy_) {
149 pointer_ =
static_cast<void*
>(allocated);
158 if (pointer_ && destroy_) {
174 BlobSerializerBase::SerializationAcceptor acceptor,
175 int chunk_size = kDefaultChunkSize)
const;
187 string Serialize(
const string& name)
const;
194 swap(meta_, rhs.meta_);
195 swap(pointer_, rhs.pointer_);
196 swap(destroy_, rhs.destroy_);
212 static void Destroy(
void* pointer) {
213 delete static_cast<T*
>(pointer);
215 typedef void (*DestroyCall)(
void *);
217 void* pointer_ =
nullptr;
218 DestroyCall destroy_ =
nullptr;
220 DISABLE_COPY_AND_ASSIGN(
Blob);
228 #endif // CAFFE2_CORE_BLOB_H_
const char * TypeName() const
Returns a printable typename of the blob.
void Reset()
Resets the Blob to an empty one.
void Deserialize(const string &content)
Deserializes from a string containing either BlobProto or TensorProto.
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
const T & Get() const
Gets the const reference of the stored object.
Blob is a general container that hosts a typed pointer.
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
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 TypeMeta & meta() const
Returns the meta info of the blob.
void Serialize(const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size=kDefaultChunkSize) const
Serializes the current blob, if possible.
bool IsType() const
Checks if the content stored in the blob is of type T.
Blob()
Initializes an empty Blob.