Caffe2 - C++ API
A deep learning, cross platform ML framework
blob.h
1 
17 #ifndef CAFFE2_CORE_BLOB_H_
18 #define CAFFE2_CORE_BLOB_H_
19 
20 #include <cstddef>
21 #include <sstream>
22 #include <typeinfo>
23 #include <type_traits>
24 #include <vector>
25 
26 #include "caffe2/core/blob_serializer_base.h"
27 #include "caffe2/core/common.h"
28 #include "caffe2/core/typeid.h"
29 #include "caffe2/core/logging.h"
30 #include "caffe2/proto/caffe2.pb.h"
31 
32 namespace caffe2 {
33 
41 class Blob {
42  public:
43  typedef void (*DestroyCall)(void*);
44 
48  Blob() : meta_(), pointer_(nullptr) {}
49  ~Blob() { Reset(); }
50 
51  Blob(Blob&& other) noexcept
52  : meta_(std::move(other.meta_)),
53  pointer_(std::move(other.pointer_)),
54  destroy_(std::move(other.destroy_)) {
55  other.meta_ = {};
56  other.pointer_ = nullptr;
57  other.destroy_ = nullptr;
58  }
59 
60  Blob& operator=(Blob&& other) noexcept {
61  meta_ = std::move(other.meta_);
62  pointer_ = std::move(other.pointer_);
63  destroy_ = std::move(other.destroy_);
64  other.meta_ = {};
65  other.pointer_ = nullptr;
66  other.destroy_ = nullptr;
67  return *this;
68  }
69 
73  template <class T>
74  bool IsType() const { return meta_.Match<T>(); }
75 
79  inline const TypeMeta& meta() const { return meta_; }
80 
84  inline const char* TypeName() const { return meta_.name(); }
85 
90  template <class T>
91  const T& Get() const {
92  CAFFE_ENFORCE(
93  IsType<T>(),
94  "wrong type for the Blob instance. Blob contains ",
95  meta_.name(),
96  " while caller expects ",
97  TypeMeta::TypeName<T>());
98  return *static_cast<const T*>(pointer_);
99  }
100 
101  const void* GetRaw() const {
102  return pointer_;
103  }
104  void* GetRaw() {
105  return pointer_;
106  }
107 
116  template <class T>
117  T* GetMutable(bool* is_new_object=nullptr) {
118  if (IsType<T>()) {
119  if (is_new_object) *is_new_object = false;
120  return static_cast<T*>(pointer_);
121  } else {
122  if (is_new_object) *is_new_object = true;
123  VLOG(1) << "Create new mutable object " << TypeMeta::TypeName<T>();
124  return Reset<T>(new T());
125  }
126  }
127 
136  template <class T>
137  T* Reset(T* allocated) {
138  if (pointer_ && destroy_) {
139  destroy_(pointer_);
140  }
141  meta_ = TypeMeta::Make<T>();
142  pointer_ = static_cast<void*>(allocated);
143  destroy_ = &Destroy<T>;
144  return allocated;
145  }
146 
147  inline void*
148  Reset(void* allocated, const TypeMeta& meta, const DestroyCall& destroy) {
149  if (pointer_ && destroy_) {
150  destroy_(pointer_);
151  }
152  meta_ = meta;
153  pointer_ = static_cast<void*>(allocated);
154  destroy_ = destroy;
155  return allocated;
156  }
157 
162  inline DestroyCall Release() {
163  DestroyCall d = destroy_;
164  destroy_ = nullptr;
165  return d;
166  }
167 
178  template <class T>
179  typename std::remove_const<T>::type* ShareExternal(
180  typename std::remove_const<T>::type* allocated) {
181  return static_cast<T*>(ShareExternal(
182  static_cast<void*>(allocated),
183  TypeMeta::Make<typename std::remove_const<T>::type>()));
184  }
185 
186  void* ShareExternal(void* allocated, const TypeMeta& meta) {
187  if (pointer_ && destroy_) {
188  destroy_(pointer_);
189  }
190  meta_ = meta;
191  pointer_ = static_cast<void*>(allocated);
192  destroy_ = nullptr;
193  return allocated;
194  }
195 
199  inline void Reset() {
200  if (pointer_ && destroy_) {
201  destroy_(pointer_);
202  }
203  pointer_ = nullptr;
204  meta_ = TypeMeta();
205  destroy_ = nullptr;
206  }
207 
214  void Serialize(
215  const string& name,
216  BlobSerializerBase::SerializationAcceptor acceptor,
217  int chunk_size = kDefaultChunkSize) const;
218 
229  string Serialize(const string& name) const;
230 
234  void swap(Blob& rhs) {
235  using std::swap;
236  swap(meta_, rhs.meta_);
237  swap(pointer_, rhs.pointer_);
238  swap(destroy_, rhs.destroy_);
239  }
240 
246  void Deserialize(const string& content);
247  void Deserialize(const BlobProto& proto);
248 
249  private:
253  template <class T>
254  static void Destroy(void* pointer) {
255  delete static_cast<T*>(pointer);
256  }
257  TypeMeta meta_;
258  void* pointer_ = nullptr;
259  DestroyCall destroy_ = nullptr;
260 
261  DISABLE_COPY_AND_ASSIGN(Blob);
262 };
263 
264 inline void swap(Blob& lhs, Blob& rhs) {
265  lhs.swap(rhs);
266 }
267 
268 } // namespace caffe2
269 #endif // CAFFE2_CORE_BLOB_H_
Blob is a general container that hosts a typed pointer.
Definition: blob.h:41
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...
Definition: blob.h:179
const char * TypeName() const
Returns a printable typename of the blob.
Definition: blob.h:84
void Reset()
Resets the Blob to an empty one.
Definition: blob.h:199
void Serialize(const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size=kDefaultChunkSize) const
Serializes the current blob, if possible.
Copyright (c) 2016-present, Facebook, Inc.
DestroyCall Release()
Releases the ownership, if any, this Blob has on the underlying pointer.
Definition: blob.h:162
const char * name() const
Returns a printable name for the type.
Definition: typeid.h:183
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
Definition: blob.h:117
const TypeMeta & meta() const
Returns the meta info of the blob.
Definition: blob.h:79
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
Definition: blob.h:234
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
Definition: blob.h:137
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.
Definition: blob.h:74
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:104
const T & Get() const
Gets the const reference of the stored object.
Definition: blob.h:91
Blob()
Initializes an empty Blob.
Definition: blob.h:48
static std::enable_if< std::is_fundamental< T >::value||std::is_pointer< T >::value, TypeMeta >::type Make()
Returns a TypeMeta object that corresponds to the typename T.
Definition: typeid.h:284