Derecho  0.9
Distributed systems toolkit for RDMA
Bytes.hpp
Go to the documentation of this file.
1 #pragma once
3 #include "../mutils-networking/connection.hpp"
4 
5 namespace mutils{
6 
7 /*
8  Intended as a way to alias C-style byte arrays for serialization. Does not own its own memory; user
9  must ensure underlying array is not freed while Bytes lives, and is responsible for freeing
10  underlying array when Bytes is destroyed.
11  */
12  struct Bytes : public ByteRepresentable{
13 
14  char const * const bytes;
15  const std::size_t size;
16 
17  Bytes(decltype(bytes) b, decltype(size) s)
18  :bytes(b),size(s){}
19 
20  std::size_t to_bytes(char* v) const{
21  ((std::size_t*)(v))[0] = size;
22  memcpy(v + sizeof(size),bytes,size);
23  return size + sizeof(size);
24  }
25 
26  std::size_t bytes_size() const {
27  return size + sizeof(size);
28  }
29 
30  void post_object(const std::function<void (char const * const,std::size_t)>& f) const{
31  f((char*)&size,sizeof(size));
32  f(bytes,size);
33  }
34 
36 
37  //from_bytes is disabled in this implementation, because it's intended only for nocopy-aware scenarios
38  template<typename T, typename V>
39  static std::unique_ptr<Bytes> from_bytes(T*, V*){
40  static_assert(std::is_same<T,V>::value,"Error: from_bytes disabled for mutils::Bytes. See comment in source.");
41  }
42 
44  return context_ptr<Bytes>{new Bytes(v + sizeof(std::size_t),((std::size_t*)(v))[0])};
45  }
46 
47  };
48 }
static context_ptr< Bytes > from_bytes_noalloc(DeserializationManager *, char const *const v)
Definition: Bytes.hpp:43
char const *const bytes
Definition: Bytes.hpp:14
void ensure_registered(DeserializationManager &)
Definition: Bytes.hpp:35
A non-POD type which wishes to mark itself byte representable should extend this class.
std::unique_ptr< T, ContextDeleter< T > > context_ptr
Definition: context_ptr.hpp:20
The manager for any RemoteDeserializationContexts.
const std::size_t size
Definition: Bytes.hpp:15
Definition: Bytes.hpp:5
std::size_t to_bytes(char *v) const
Write this class&#39;s marshalled representation into the array found at v.
Definition: Bytes.hpp:20
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: Bytes.hpp:30
static std::unique_ptr< Bytes > from_bytes(T *, V *)
Definition: Bytes.hpp:39
Bytes(decltype(bytes) b, decltype(size) s)
Definition: Bytes.hpp:17
std::size_t bytes_size() const
the size of the marshalled representation of this object.
Definition: Bytes.hpp:26