Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
61514adc366aba50543e3e95e4ff0e8599973a43
[simgrid.git] / src / mc / Channel.hpp
1 /* Copyright (c) 2015-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_CHANNEL_HPP
8 #define SIMGRID_MC_CHANNEL_HPP
9
10 #include <unistd.h>
11
12 #include <type_traits>
13
14 #include "src/mc/mc_protocol.h"
15
16 namespace simgrid {
17 namespace mc {
18
19 class Channel {
20   int socket_ = -1;
21   template<class M>
22   static constexpr bool messageType()
23   {
24     return std::is_class<M>::value && std::is_trivial<M>::value;
25   }
26 public:
27
28   Channel() {}
29   Channel(int sock) : socket_(sock) {}
30   ~Channel();
31
32   // No copy:
33   Channel(Channel const&) = delete;
34   Channel& operator=(Channel const&) = delete;
35
36   // Move:
37   Channel(Channel&& that) : socket_(that.socket_)
38   {
39     that.socket_ = -1;
40   }
41   Channel& operator=(Channel&& that)
42   {
43     this->socket_ = that.socket_;
44     that.socket_ = -1;
45     return *this;
46   }
47
48   // Send
49   int send(const void* message, size_t size) const;
50   int send(e_mc_message_type type) const
51   {
52     s_mc_message message = { type };
53     return this->send(&message, sizeof(message));
54   }
55   template<class M>
56   typename std::enable_if< messageType<M>(), int >::type
57   send(M const& m) const
58   {
59     return this->send(&m, sizeof(M));
60   }
61
62   // Receive
63   ssize_t receive(void* message, size_t size, bool block = true) const;
64   template<class M>
65   typename std::enable_if< messageType<M>(), ssize_t >::type
66   receive(M& m) const
67   {
68     return this->receive(&m, sizeof(M));
69   }
70
71   int getSocket() const
72   {
73     return socket_;
74   }
75
76 };
77
78 }
79 }
80
81 #endif