2016-02-02 15:40:07 +03:00
|
|
|
#ifndef MULTIVERSO_BLOB_H_
|
|
|
|
#define MULTIVERSO_BLOB_H_
|
|
|
|
|
2016-02-15 08:17:32 +03:00
|
|
|
#include <cstring>
|
2016-05-04 12:21:56 +03:00
|
|
|
#include <iostream>
|
2016-08-10 06:22:15 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-02-02 15:40:07 +03:00
|
|
|
|
|
|
|
namespace multiverso {
|
|
|
|
|
2016-05-19 15:58:17 +03:00
|
|
|
// Manage a chunk of memory. Blob can share memory with other Blobs.
|
|
|
|
// Never use external memory. All external memory should be managed by itself
|
|
|
|
class Blob {
|
|
|
|
public:
|
|
|
|
// an empty blob
|
2016-05-22 16:51:58 +03:00
|
|
|
Blob() : data_(nullptr), size_(0) {}
|
2016-05-19 15:58:17 +03:00
|
|
|
|
|
|
|
explicit Blob(size_t size);
|
|
|
|
|
|
|
|
// Construct from external memory. Will copy a new piece
|
|
|
|
Blob(const void* data, size_t size);
|
|
|
|
|
|
|
|
Blob(void* data, size_t size);
|
|
|
|
|
|
|
|
Blob(const Blob& rhs);
|
|
|
|
|
|
|
|
~Blob();
|
|
|
|
|
|
|
|
// Shallow copy by default. Call \ref CopyFrom for a deep copy
|
|
|
|
void operator=(const Blob& rhs);
|
|
|
|
|
|
|
|
inline char operator[](size_t i) const {
|
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline T& As(size_t i = 0) const {
|
|
|
|
return (reinterpret_cast<T*>(data_))[i];
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
inline size_t size() const { return size_ / sizeof(T); }
|
|
|
|
|
|
|
|
// DeepCopy, for a shallow copy, use operator=
|
|
|
|
void CopyFrom(const Blob& src);
|
|
|
|
|
|
|
|
inline char* data() const { return data_; }
|
|
|
|
inline size_t size() const { return size_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Memory is shared and auto managed
|
|
|
|
char *data_;
|
|
|
|
size_t size_;
|
|
|
|
};
|
2016-02-02 15:40:07 +03:00
|
|
|
|
2016-04-08 11:57:29 +03:00
|
|
|
} // namespace multiverso
|
|
|
|
|
|
|
|
#endif // MULTIVERSO_BLOB_H_
|