SObjectizer-5 Extra
fixed_size.hpp
Go to the documentation of this file.
1 /*!
2  * \file
3  * \brief Implementation of fixed-size mchain.
4  *
5  * \since
6  * v.1.4.0
7  */
8 
9 #pragma once
10 
11 #include <so_5/impl/mchain_details.hpp>
12 #include <so_5/impl/make_mchain.hpp>
13 #include <so_5/impl/internal_env_iface.hpp>
14 
15 #include <array>
16 
17 namespace so_5
18 {
19 
20 namespace extra
21 {
22 
23 namespace mchains
24 {
25 
26 namespace fixed_size
27 {
28 
29 namespace details
30 {
31 
32 //
33 // demand_queue_t
34 //
35 /*!
36  * \brief Implementation of demands queue for fixed-size message chain with
37  * "static" storage.
38  *
39  * \since
40  * v.1.4.0
41  */
42 template< std::size_t Size >
44  {
45  public :
46  // NOTE: constructor of this format is necessary
47  // because the standard implementation of mchain from SO-5
48  // requires it.
50 
51  //! Is queue full?
52  [[nodiscard]] bool
53  is_full() const { return Size == m_size; }
54 
55  //! Is queue empty?
56  [[nodiscard]] bool
57  is_empty() const { return 0u == m_size; }
58 
59  //! Access to front item of the queue.
62  {
64  return m_storage[ m_head ];
65  }
66 
67  //! Remove the front item from queue.
68  void
70  {
73  m_head = (m_head + 1u) % Size;
74  --m_size;
75  }
76 
77  //! Add a new item to the end of the queue.
78  void
79  push_back( so_5::mchain_props::demand_t && demand )
80  {
82  auto index = (m_head + m_size) % Size;
83  m_storage[ index ] = std::move(demand);
84  ++m_size;
85  }
86 
87  //! Size of the queue.
88  std::size_t
89  size() const { return m_size; }
90 
91  private :
92  //! Queue's storage.
94 
95  //! Index of the queue head.
96  std::size_t m_head{ 0u };
97  //! The current size of the queue.
98  std::size_t m_size{ 0u };
99  };
100 
101 } /* namespace details */
102 
103 //
104 // create_mchain
105 //
106 /*!
107  * \brief Helper function for creation of fixed-size mchain.
108  *
109  * Creates a mchain without waiting on attempt to push a new message
110  * into full mchain.
111  *
112  * Usage example:
113  * \code
114  so_5::wrapped_env_t sobj;
115 
116  auto reply_ch = so_5::extra::mchains::fixed_size::create_mchain<1>(
117  sobj.environment(),
118  so_5::mchain_props::overflow_reaction_t::drop_newest);
119  * \endcode
120  *
121  * \brief
122  * v.1.4.0
123  */
124 template< std::size_t Size >
127  environment_t & env,
129  {
131 
135  // NOTE: this value won't be used.
136  Size,
137  // NOTE: this value won't be used.
140  env,
142  }
143 
144 //
145 // create_mchain
146 //
147 /*!
148  * \brief Helper function for creation of fixed-size mchain.
149  *
150  * Creates a mchain with waiting on attempt to push a new message
151  * into full mchain.
152  *
153  * Usage example:
154  * \code
155  so_5::wrapped_env_t sobj;
156 
157  auto reply_ch = so_5::extra::mchains::fixed_size::create_mchain<5>(
158  sobj.environment(),
159  std::chrono::milliseconds{250},
160  so_5::mchain_props::overflow_reaction_t::remove_oldest);
161  * \endcode
162  *
163  * \brief
164  * v.1.4.0
165  */
166 template< std::size_t Size >
169  environment_t & env,
172  {
174 
178  // NOTE: this value won't be used.
179  Size,
180  // NOTE: this value won't be used.
183  wait_timeout ),
184  env,
186  }
187 
188 //
189 // create_mchain
190 //
191 /*!
192  * \brief Helper function for creation of fixed-size mchain.
193  *
194  * Usage example:
195  * \code
196  so_5::wrapped_env_t sobj;
197 
198  auto params = so_5::make_limited_with_waiting_mchain_params(
199  1, // Will be ignored.
200  so_5::mchain_props::memory_usage_t::preallocated, // Will be ignored.
201  so_5::mchain_props::overflow_reaction_t::throw_exception,
202  std::chrono::seconds{3});
203  params.disable_msg_tracing();
204  params.not_empty_notificator([]{...});
205 
206  auto reply_ch = so_5::extra::mchains::fixed_size::create_mchain<20>(
207  sobj.environment(),
208  params);
209  * \endcode
210  *
211  * \attention
212  * Value of params.capacity() will be ignored.
213  *
214  * \brief
215  * v.1.4.0
216  */
217 template< std::size_t Size >
220  environment_t & env,
221  const so_5::mchain_params_t & params )
222  {
224 
227  // NOTE: some of params's value won't be used.
228  params,
229  env,
231  }
232 
233 } /* namespace fixed_size */
234 
235 } /* namespace mchains */
236 
237 } /* namespace extra */
238 
239 } /* namespace so_5 */
so_5::mchain_props::demand_t & front()
Access to front item of the queue.
Definition: fixed_size.hpp:61
std::size_t size() const
Size of the queue.
Definition: fixed_size.hpp:89
Ranges for error codes of each submodules.
Definition: details.hpp:13
std::size_t m_size
The current size of the queue.
Definition: fixed_size.hpp:98
Implementation of demands queue for fixed-size message chain with "static" storage.
Definition: fixed_size.hpp:43
mchain_t create_mchain(environment_t &env, const so_5::mchain_params_t &params)
Helper function for creation of fixed-size mchain.
Definition: fixed_size.hpp:219
void push_back(so_5::mchain_props::demand_t &&demand)
Add a new item to the end of the queue.
Definition: fixed_size.hpp:79
void pop_front()
Remove the front item from queue.
Definition: fixed_size.hpp:69
mchain_t create_mchain(environment_t &env, so_5::mchain_props::duration_t wait_timeout, so_5::mchain_props::overflow_reaction_t overflow_reaction)
Helper function for creation of fixed-size mchain.
Definition: fixed_size.hpp:168
std::array< so_5::mchain_props::demand_t, Size > m_storage
Queue&#39;s storage.
Definition: fixed_size.hpp:93
mchain_t create_mchain(environment_t &env, so_5::mchain_props::overflow_reaction_t overflow_reaction)
Helper function for creation of fixed-size mchain.
Definition: fixed_size.hpp:126
std::size_t m_head
Index of the queue head.
Definition: fixed_size.hpp:96