forb
stream.hpp
1 #ifndef __forb_stream_hpp__
2 #define __forb_stream_hpp__
3 
4 #include <sys/types.h>
5 #include <byteswap.h>
6 #include <limits>
7 
8 #include <forb/declarations.hpp>
9 
10 namespace forb {
11  namespace streams {
12 
16  template<typename T>
17  T marshal(T v) {
18  static_assert(
19  sizeof(T) == 1
20  || sizeof(T) == 2
21  || sizeof(T) == 4
22  || sizeof(T) == 8,
23  "Cannot marshal the type T");
24 
25  switch (sizeof(T)) {
26  case 2:
27  return bswap_16(v);
28  case 4:
29  return bswap_32(v);
30  case 8:
31  return bswap_64(v);
32  default:
33  // Should only happen for chars
34  return v;
35  }
36  }
37 
41  template<typename T>
42  T unmarshal(T v) {
43  return marshal(v);
44  }
45 
47  template<typename T>
48  void marshal(T v[], size_t N) {
49  for (size_t idx = 0; idx < N; ++idx) {
50  v[idx] = marshal(v[idx]);
51  }
52  }
53 
55  template<typename T>
56  void unmarshal(T v[], size_t N) {
57  return marshal(v, N);
58  }
59 
69  class stream {
70  public:
72  using size_t = std::size_t;
73 
78  enum class type : call_id_t {
79  SOCKET = 0,
80  SHMEM = 1 << (std::numeric_limits<call_id_t>::digits - 1)
81  };
82 
85  virtual ~stream() noexcept = default;
86 
89  virtual void send(const void *buffer, size_t sise) = 0;
90 
93  virtual void recv(void *buffer, size_t sise) = 0;
94 
96  virtual void close() = 0;
97 
99  virtual type get_type() const = 0;
100 
103  virtual bool require_marshal() const { return false; };
104  };
105 
106  } // namespace streams
107 
108 } // namespace forb
109 
110 #endif // #ifndef __forb_stream_hpp__
virtual ~stream() noexcept=default
This class is virtual, so it requires a virtual destructor.
virtual void close()=0
Closes the stream.
Virtual class that represents a stream.
Definition: stream.hpp:69
This function implements a memcpy abstract that is valid also for volatile data.
Definition: base_skeleton.hpp:15
virtual void recv(void *buffer, size_t sise)=0
Blocking receive operation.
virtual void send(const void *buffer, size_t sise)=0
Blocking send operation.
type
The type of the stream.
Definition: stream.hpp:78
virtual bool require_marshal() const
Returns true if the execution platform requires marshalling before sending data over the stream...
Definition: stream.hpp:103
virtual type get_type() const =0
Returns the type of the stream, either type::SOCKET or type::SHMEM.
uint16_t call_id_t
The type that will be used to transmit the call_id.
Definition: declarations.hpp:12