Motorcortex Core  version: 2.7.6
ps_outbuffer.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2015 VECTIONEER.
4  */
5 
6 #ifndef PS_OUTBUFFER_H
7 #define PS_OUTBUFFER_H
8 
9 #include "ps_datacontainer.h"
10 #include <atomic>
11 #include <shared_mutex>
12 
13 namespace mcx {
14 
15 namespace parameter_server {
16 
17 class OutputBuffer {
18  static constexpr unsigned int BUFFER_SIZE = 16;
19  static constexpr unsigned int BUFFER_MASK{BUFFER_SIZE - 1};
20 
21 public:
22  OutputBuffer();
23 
24  virtual ~OutputBuffer();
25 
26  OutputBuffer(const OutputBuffer&) = delete;
27 
28  OutputBuffer& operator=(const OutputBuffer&) = delete;
29 
30  bool allocate(unsigned int element_size);
31 
32  void deallocate();
33 
34  void init(const DataContainer& in);
35 
36  void write(const DataContainer& in);
37 
38  unsigned int read(DataContainer* dst);
39 
40  unsigned int readWithConversion(DataContainer* dst, const ConversionData& conversion);
41 
42  static constexpr unsigned int size() { return BUFFER_SIZE; }
43 
44  unsigned int pos() const { return enqueue_pos_.load(std::memory_order_acquire); }
45 
46 private:
47  struct Cell {
48 #if __cplusplus >= 201703L
49  std::shared_mutex guard;
50 #else
51  std::shared_timed_mutex guard;
52 #endif
53  DataContainer data;
54  };
55  Cell* buffer_;
56  std::atomic<unsigned int> enqueue_pos_{};
57 };
58 
59 } // namespace parameter_server
60 
61 } // namespace mcx
62 
63 #endif /* PS_OUTBUFFER_H */
mcx::parameter_server::OutputBuffer
Definition: ps_outbuffer.h:17
mcx::parameter_server::DataContainer
Definition: ps_datacontainer.h:40
mcx::parameter_server::ConversionData
Definition: ps_datacontainer.h:32