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:
30  Blob() : meta_(), pointer_(nullptr) {}
31  ~Blob() { Reset(); }
32 
33  Blob(Blob&& other) noexcept
34  : meta_(std::move(other.meta_)),
35  pointer_(std::move(other.pointer_)),
36  destroy_(std::move(other.destroy_)) {
37  other.meta_ = {};
38  other.pointer_ = nullptr;
39  other.destroy_ = nullptr;
40  }
41 
42  Blob& operator=(Blob&& other) noexcept {
43  meta_ = std::move(other.meta_);
44  pointer_ = std::move(other.pointer_);
45  destroy_ = std::move(other.destroy_);
46  other.meta_ = {};
47  other.pointer_ = nullptr;
48  other.destroy_ = nullptr;
49  return *this;
50  }
51 
55  template <class T>
56  bool IsType() const { return meta_.Match<T>(); }
57 
61  inline const TypeMeta& meta() const { return meta_; }
62 
66  inline const char* TypeName() const { return meta_.name(); }
67 
72  template <class 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_);
78  }
79 
80  const void* GetRaw() const {
81  return pointer_;
82  }
83  void* GetRaw() {
84  return pointer_;
85  }
86 
95  template <class T>
96  T* GetMutable(bool* is_new_object=nullptr) {
97  if (IsType<T>()) {
98  if (is_new_object) *is_new_object = false;
99  return static_cast<T*>(pointer_);
100  } else {
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());
104  }
105  }
106 
115  template <class T>
116  T* Reset(T* allocated) {
117  if (pointer_ && destroy_) {
118  destroy_(pointer_);
119  }
120  meta_ = TypeMeta::Make<T>();
121  pointer_ = static_cast<void*>(allocated);
122  destroy_ = &Destroy<T>;
123  return allocated;
124  }
125 
136  template <class T>
137  typename std::remove_const<T>::type* ShareExternal(
138  typename std::remove_const<T>::type* allocated) {
139  return static_cast<T*>(ShareExternal(
140  static_cast<void*>(allocated),
141  TypeMeta::Make<typename std::remove_const<T>::type>()));
142  }
143 
144  void* ShareExternal(void* allocated, const TypeMeta& meta) {
145  if (pointer_ && destroy_) {
146  destroy_(pointer_);
147  }
148  meta_ = meta;
149  pointer_ = static_cast<void*>(allocated);
150  destroy_ = nullptr;
151  return allocated;
152  }
153 
157  inline void Reset() {
158  if (pointer_ && destroy_) {
159  destroy_(pointer_);
160  }
161  pointer_ = nullptr;
162  meta_ = TypeMeta();
163  destroy_ = nullptr;
164  }
165 
172  void Serialize(
173  const string& name,
174  BlobSerializerBase::SerializationAcceptor acceptor,
175  int chunk_size = kDefaultChunkSize) const;
176 
187  string Serialize(const string& name) const;
188 
192  void swap(Blob& rhs) {
193  using std::swap;
194  swap(meta_, rhs.meta_);
195  swap(pointer_, rhs.pointer_);
196  swap(destroy_, rhs.destroy_);
197  }
198 
204  void Deserialize(const string& content);
205  void Deserialize(const BlobProto& proto);
206 
207  private:
211  template <class T>
212  static void Destroy(void* pointer) {
213  delete static_cast<T*>(pointer);
214  }
215  typedef void (*DestroyCall)(void *);
216  TypeMeta meta_;
217  void* pointer_ = nullptr;
218  DestroyCall destroy_ = nullptr;
219 
220  DISABLE_COPY_AND_ASSIGN(Blob);
221 };
222 
223 inline void swap(Blob& lhs, Blob& rhs) {
224  lhs.swap(rhs);
225 }
226 
227 } // namespace caffe2
228 #endif // CAFFE2_CORE_BLOB_H_
static std::enable_if< std::is_fundamental< T >::value, TypeMeta >::type Make()
Returns a TypeMeta object that corresponds to the typename T.
Definition: typeid.h:246
const char * TypeName() const
Returns a printable typename of the blob.
Definition: blob.h:66
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:66
void Reset()
Resets the Blob to an empty one.
Definition: blob.h:157
void Deserialize(const string &content)
Deserializes from a string containing either BlobProto or TensorProto.
const char * name() const
Returns a printable name for the type.
Definition: typeid.h:145
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
Definition: blob.h:192
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.
Definition: blob.h:73
Blob is a general container that hosts a typed pointer.
Definition: blob.h:25
T * GetMutable(bool *is_new_object=nullptr)
Gets a mutable pointer to the stored object.
Definition: blob.h:96
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
Definition: blob.h:116
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:137
const TypeMeta & meta() const
Returns the meta info of the blob.
Definition: blob.h:61
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.
Definition: blob.h:56
Blob()
Initializes an empty Blob.
Definition: blob.h:30