Derecho  0.9
Distributed systems toolkit for RDMA
Object.cpp
Go to the documentation of this file.
2 
3 namespace objectstore{
4 
5  Blob::Blob(const char* const b, const decltype(size) s) :
6  bytes(nullptr), size(0) {
7  if(s > 0) {
8  bytes = new char[s];
9  memcpy(bytes, b, s);
10  size = s;
11  }
12  }
13 
14  Blob::Blob(const Blob& other) :
15  bytes(nullptr), size(0) {
16  if(other.size > 0) {
17  bytes = new char[other.size];
18  memcpy(bytes, other.bytes, other.size);
19  size = other.size;
20  }
21  }
22 
23  Blob::Blob(Blob&& other) :
24  bytes(other.bytes), size(other.size) {
25  other.bytes = nullptr;
26  other.size = 0;
27  }
28 
29  Blob::Blob() : bytes(nullptr), size(0) {}
30 
32  if(bytes) delete bytes;
33  }
34 
36  char* swp_bytes = other.bytes;
37  std::size_t swp_size = other.size;
38  other.bytes = bytes;
39  other.size = size;
40  bytes = swp_bytes;
41  size = swp_size;
42  return *this;
43  }
44 
45  Blob& Blob::operator=(const Blob& other) {
46  if(bytes != nullptr) {
47  delete bytes;
48  }
49  size = other.size;
50  if(size > 0) {
51  bytes = new char[size];
52  memcpy(bytes, other.bytes, size);
53  } else {
54  bytes = nullptr;
55  }
56  return *this;
57  }
58 
59  std::size_t Blob::to_bytes(char* v) const {
60  ((std::size_t*)(v))[0] = size;
61  if(size > 0) {
62  memcpy(v + sizeof(size), bytes, size);
63  }
64  return size + sizeof(size);
65  }
66 
67  std::size_t Blob::bytes_size() const {
68  return size + sizeof(size);
69  }
70 
71  void Blob::post_object(const std::function<void(char const* const, std::size_t)>& f) const {
72  f((char*)&size, sizeof(size));
73  f(bytes, size);
74  }
75 
76  // from_bytes_noalloc() implementation borrowed from mutils-serialization.
78  return mutils::context_ptr<Blob>{from_bytes(ctx, v).release()};
79  }
80 
81  std::unique_ptr<Blob> Blob::from_bytes(mutils::DeserializationManager*, const char* const v) {
82  return std::make_unique<Blob>(v + sizeof(std::size_t), ((std::size_t*)(v))[0]);
83  }
84 
85 
86  bool Object::operator==(const Object& other) {
87  return (this->oid == other.oid) && (this->ver == other.ver);
88  }
89 
90  bool Object::is_valid() const {
91  return (oid == INV_OID);
92  }
93 
94  // constructor 0 : copy constructor
95  Object::Object(const OID& _oid, const Blob& _blob) : ver(INVALID_VERSION,0),
96  oid(_oid),
97  blob(_blob) {}
98  // constructor 0.5 : copy constructor
99  Object::Object(const std::tuple<persistent::version_t,uint64_t> _ver, const OID& _oid, const Blob& _blob) : ver(_ver), oid(_oid), blob(_blob) {}
100 
101  // constructor 1 : copy consotructor
102  Object::Object(const uint64_t _oid, const char* const _b, const std::size_t _s) : ver(INVALID_VERSION,0),
103  oid(_oid),
104  blob(_b, _s) {}
105  // constructor 1.5 : copy constructor
106  Object::Object(const std::tuple<persistent::version_t,uint64_t> _ver, const uint64_t _oid, const char* const _b, const std::size_t _s) : ver(_ver), oid(_oid), blob(_b, _s) {}
107 
108  // constructor 2 : move constructor
109  Object::Object(Object&& other) : ver(other.ver),
110  oid(other.oid),
111  blob(std::move(other.blob)) {}
112  // constructor 3 : copy constructor
113  Object::Object(const Object& other) : ver(other.ver),
114  oid(other.oid),
115  blob(other.blob) {}
116  // constructor 4 : default invalid constructor
118 }
std::size_t size
Definition: Object.hpp:29
STL namespace.
mutils::context_ptr< Blob > from_bytes_noalloc(mutils::DeserializationManager *ctx, const char *const v, mutils::context_ptr< Blob >=mutils::context_ptr< Blob >{})
Definition: Object.cpp:77
std::size_t bytes_size() const
the size of the marshalled representation of this object.
Definition: Object.cpp:67
std::unique_ptr< T, ContextDeleter< T > > context_ptr
Definition: context_ptr.hpp:20
The manager for any RemoteDeserializationContexts.
std::size_t to_bytes(char *v) const
Write this class&#39;s marshalled representation into the array found at v.
Definition: Object.cpp:59
virtual ~Blob()
Definition: Object.cpp:31
#define INVALID_VERSION
Definition: PersistLog.hpp:28
bool is_valid() const
Definition: Object.cpp:90
bool operator==(const Object &other)
Definition: Object.cpp:86
std::tuple< persistent::version_t, uint64_t > ver
Definition: Object.hpp:75
uint64_t OID
Definition: Object.hpp:70
Blob & operator=(Blob &&other)
Definition: Object.cpp:35
static std::unique_ptr< Blob > from_bytes(mutils::DeserializationManager *, const char *const v)
Definition: Object.cpp:81
#define INV_OID
Definition: Object.hpp:71
void post_object(const std::function< void(char const *const, std::size_t)> &f) const
Pass a pointer to a buffer containing this class&#39;s marshalled representation into the function f...
Definition: Object.cpp:71