Subordination
A framework for distributed programming
kernel_header.hh
1 #ifndef SUBORDINATION_KERNEL_KERNEL_HEADER_HH
2 #define SUBORDINATION_KERNEL_KERNEL_HEADER_HH
3 
4 #include <iosfwd>
5 #include <memory>
6 
7 #include <unistdx/net/socket_address>
8 
9 #include <subordination/ppl/application.hh>
10 #include <subordination/ppl/kernel_header_flag.hh>
11 
12 namespace sbn {
13 
14  class kernel_header {
15 
16  public:
19 
20  private:
21  flag_type _flags = flag_type(0);
22  sys::socket_address _src {};
23  sys::socket_address _dst {};
24  application_type _aid = this_application::get_id();
25  const application* _aptr = nullptr;
26 
27  public:
28  kernel_header() = default;
29 
30  kernel_header(const kernel_header&) = delete;
31 
33  operator=(const kernel_header&) = delete;
34 
35  inline
36  ~kernel_header() {
37  if (this->owns_application()) {
38  delete this->_aptr;
39  }
40  }
41 
42  inline const sys::socket_address&
43  from() const noexcept {
44  return this->_src;
45  }
46 
47  inline void
48  from(const sys::socket_address& rhs) noexcept {
49  this->_src = rhs;
50  }
51 
52  inline const sys::socket_address&
53  to() const noexcept {
54  return this->_dst;
55  }
56 
57  inline void
58  to(const sys::socket_address& rhs) noexcept {
59  this->_dst = rhs;
60  }
61 
62  inline application_type
63  app() const noexcept {
64  return this->_aid;
65  }
66 
67  inline void
68  setapp(application_type rhs) noexcept {
69  this->_aid = rhs;
70  }
71 
72  inline bool
73  is_foreign() const noexcept {
74  return !this->is_native();
75  }
76 
77  inline bool
78  is_native() const noexcept {
79  return this->_aid == this_application::get_id();
80  }
81 
82  inline const application*
83  aptr() const noexcept {
84  return this->_aptr;
85  }
86 
87  inline void
88  aptr(const application* rhs) noexcept {
89  if (this->owns_application()) {
90  delete this->_aptr;
91  }
92  if (rhs) {
93  this->_flags |= flag_type::has_application;
94  this->_aid = rhs->id();
95  } else {
96  this->_flags &= ~flag_type::has_application;
97  }
98  this->_aptr = rhs;
99  }
100 
101  inline bool
102  has_application() const noexcept {
103  return this->_flags & kernel_header_flag::has_application;
104  }
105 
106  inline bool
107  owns_application() const noexcept {
108  return this->_flags & flag_type::owns_application;
109  }
110 
111  inline bool
112  has_source_and_destination() const noexcept {
113  return this->_flags &
114  kernel_header_flag::has_source_and_destination;
115  }
116 
117  inline void
118  prepend_source_and_destination() {
119  this->_flags |= kernel_header_flag::has_source_and_destination;
120  }
121 
122  inline void
123  do_not_prepend_source_and_destination() {
124  this->_flags &= ~kernel_header_flag::has_source_and_destination;
125  }
126 
127  void
128  write_header(sys::pstream& out) const;
129 
130  void
131  read_header(sys::pstream& in);
132 
133  friend std::ostream&
134  operator<<(std::ostream& out, const kernel_header& rhs);
135 
136  };
137 
138  std::ostream&
139  operator<<(std::ostream& out, const kernel_header& rhs);
140 
141  inline sys::pstream&
142  operator<<(sys::pstream& out, const kernel_header& rhs) {
143  rhs.write_header(out);
144  return out;
145  }
146 
147  inline sys::pstream&
148  operator>>(sys::pstream& in, kernel_header& rhs) {
149  rhs.read_header(in);
150  return in;
151  }
152 
153 }
154 
155 #endif // vim:filetype=cpp
Definition: kernel_header.hh:14
Definition: application.hh:25
Definition: kernel_header_flag.hh:10