Caffe2 - C++ API
A deep learning, cross platform ML framework
blob.h
1 #ifndef CAFFE2_CORE_BLOB_H_
2 #define CAFFE2_CORE_BLOB_H_
3 
4 #include <cstddef>
5 #include <sstream>
6 #include <typeinfo>
7 #include <type_traits>
8 #include <vector>
9 
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"
15 
16 namespace caffe2 {
17 
25 class Blob {
26  public:
27  typedef void (*DestroyCall)(void*);
28 
32  Blob() : meta_(), pointer_(nullptr) {}
33  ~Blob() { Reset(); }
34 
35  Blob(Blob&& other) noexcept
36  : meta_(std::move(other.meta_)),
37  pointer_(std::move(other.pointer_)),
38  destroy_(std::move(other.destroy_)) {
39  other.meta_ = {};
40  other.pointer_ = nullptr;
41  other.destroy_ = nullptr;
42  }
43 
44  Blob& operator=(Blob&& other) noexcept {
45  meta_ = std::move(other.meta_);
46  pointer_ = std::move(other.pointer_);
47  destroy_ = std::move(other.destroy_);
48  other.meta_ = {};
49  other.pointer_ = nullptr;
50  other.destroy_ = nullptr;
51  return *this;
52  }
53 
57  template <class T>
58  bool IsType() const { return meta_.Match<T>(); }
59 
63  inline const TypeMeta& meta() const { return meta_; }
64 
68  inline const char* TypeName() const { return meta_.name(); }
69 
74  template <class T>
75  const T& Get() const {
76  CAFFE_ENFORCE(
77  IsType<T>(),
78  "wrong type for the Blob instance. Blob contains ",
79  meta_.name(),
80  " while caller expects ",
81  TypeMeta::TypeName<T>());
82  return *static_cast<const T*>(pointer_);
83  }
84 
85  const void* GetRaw() const {
86  return pointer_;
87  }
88  void* GetRaw() {
89  return pointer_;
90  }
91 
100  template <class T>
101  T* GetMutable(bool* is_new_object=nullptr) {
102  if (IsType<T>()) {
103  if (is_new_object) *is_new_object = false;
104  return static_cast<T*>(pointer_);
105  } else {
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());
109  }
110  }
111 
120  template <class T>
121  T* Reset(T* allocated) {
122  if (pointer_ && destroy_) {
123  destroy_(pointer_);
124  }
125  meta_ = TypeMeta::Make<T>();
126  pointer_ = static_cast<void*>(allocated);
127  destroy_ = &Destroy<T>;
128  return allocated;
129  }
130 
131  inline void*
132  Reset(void* allocated, const TypeMeta& meta, const DestroyCall& destroy) {
133  if (pointer_ && destroy_) {
134  destroy_(pointer_);
135  }
136  meta_ = meta;
137  pointer_ = static_cast<void*>(allocated);
138  destroy_ = destroy;
139  return allocated;
140  }
141 
146  inline DestroyCall Release() {
147  DestroyCall d = destroy_;
148  destroy_ = nullptr;
149  return d;
150  }
151 
162  template <class T>
163  typename std::remove_const<T>::type* ShareExternal(
164  typename std::remove_const<T>::type* allocated) {
165  return static_cast<T*>(ShareExternal(
166  static_cast<void*>(allocated),
167  TypeMeta::Make<typename std::remove_const<T>::type>()));
168  }
169 
170  void* ShareExternal(void* allocated, const TypeMeta& meta) {
171  if (pointer_ && destroy_) {
172  destroy_(pointer_);
173  }
174  meta_ = meta;
175  pointer_ = static_cast<void*>(allocated);
176  destroy_ = nullptr;
177  return allocated;
178  }
179 
183  inline void Reset() {
184  if (pointer_ && destroy_) {
185  destroy_(pointer_);
186  }
187  pointer_ = nullptr;
188  meta_ = TypeMeta();
189  destroy_ = nullptr;
190  }
191 
198  void Serialize(
199  const string& name,
200  BlobSerializerBase::SerializationAcceptor acceptor,
201  int chunk_size = kDefaultChunkSize) const;
202 
213  string Serialize(const string& name) const;
214 
218  void swap(Blob& rhs) {
219  using std::swap;
220  swap(meta_, rhs.meta_);
221  swap(pointer_, rhs.pointer_);
222  swap(destroy_, rhs.destroy_);
223  }
224 
230  void Deserialize(const string& content);
231  void Deserialize(const BlobProto& proto);
232 
233  private:
237  template <class T>
238  static void Destroy(void* pointer) {
239  delete static_cast<T*>(pointer);
240  }
241  TypeMeta meta_;
242  void* pointer_ = nullptr;
243  DestroyCall destroy_ = nullptr;
244 
245  DISABLE_COPY_AND_ASSIGN(Blob);
246 };
247 
248 inline void swap(Blob& lhs, Blob& rhs) {
249  lhs.swap(rhs);
250 }
251 
252 } // namespace caffe2
253 #endif // CAFFE2_CORE_BLOB_H_
Blob is a general container that hosts a typed pointer.
Definition: blob.h:25
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:163
const char * TypeName() const
Returns a printable typename of the blob.
Definition: blob.h:68
void Reset()
Resets the Blob to an empty one.
Definition: blob.h:183
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.
Definition: blob.h:146
const char * name() const
Returns a printable name for the type.
Definition: typeid.h:167
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
Definition: blob.h:101
const TypeMeta & meta() const
Returns the meta info of the blob.
Definition: blob.h:63
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
Definition: blob.h:218
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
Definition: blob.h:121
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:58
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:88
const T & Get() const
Gets the const reference of the stored object.
Definition: blob.h:75
Blob()
Initializes an empty Blob.
Definition: blob.h:32
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:268