ED247 Library  VA2.3.0
Implementation of ED247-A standard
ed247_sample.h
1 /* -*- mode: c++; c-basic-offset: 2 -*- */
2 /* sample : store a payload in a preallocated memory */
3 #ifndef _ED247_SAMPLE_H_
4 #define _ED247_SAMPLE_H_
5 #include "ed247.h"
6 #include <vector>
7 
8 namespace ed247
9 {
10 
11  //
12  // Simple preallocated buffer
13  //
14  class Sample
15  {
16  public:
17  Sample(uint32_t capacity);
18  ~Sample();
19 
20  // Empty Ctor. Call allocate() before any other functions.
21  Sample();
22 
23  // No implicit copy. Use copy() methods.
24  Sample(const Sample & other) = delete;
25  Sample& operator = (const Sample & other) = delete;
26 
27  // Can be moved
28  Sample(Sample&& other);
29 
30  // Allocate data. Shall be called only once and only if Empty Ctor is used.
31  void allocate(uint32_t capacity);
32 
33  const uint32_t& size() const { return _size; }
34  const uint32_t& capacity() const { return _capacity; }
35  const char* data() const { return _data; }
36  bool empty() const { return _size == 0; }
37 
38  // Fill data & size. Return false if capacity() is too small.
39  bool copy(const char* data, const uint32_t& size);
40  bool copy(const void* data, const uint32_t& size) { return copy((const char*)data, size); }
41 
42  // Direct access. You have to check capacity() yourself.
43  char* data_rw() { return _data; }
44  void set_size(const uint32_t & size) { _size = size; }
45  void reset() { _size = 0; }
46 
47  private:
48  char* _data;
49  uint32_t _size;
50  uint32_t _capacity;
51  };
52 
53  //
54  // Preallocated buffer with stream informations
55  //
56  class StreamSample : public Sample
57  {
58  public:
59  StreamSample(uint32_t capacity) :
60  Sample(capacity),
61  _data_timestamp(LIBED247_TIMESTAMP_DEFAULT),
62  _recv_timestamp(LIBED247_TIMESTAMP_DEFAULT),
63  _frame_details(LIBED247_SAMPLE_DETAILS_DEFAULT)
64  {}
65 
66  // No implicit copy. Use copy() methods.
67  StreamSample(const StreamSample & other) = delete;
68  StreamSample & operator = (const StreamSample & other) = delete;
69 
70  // Can be moved
71  StreamSample(StreamSample&& other) :
72  Sample(std::move(other)),
73  _data_timestamp(std::move(other._data_timestamp)),
74  _recv_timestamp(std::move(other._recv_timestamp)),
75  _frame_details(std::move(other._frame_details))
76  {}
77 
78 
79  using Sample::copy;
80 
81  // Copy a sample. Return false if capacity() is too small.
82  bool copy(const StreamSample & sample);
83 
84  void set_data_timestamp(const ed247_timestamp_t& data_timestamp) { _data_timestamp = data_timestamp; }
85  void set_recv_timestamp(const ed247_timestamp_t& recv_timestamp) { _recv_timestamp = recv_timestamp; }
86  void set_frame_details(const ed247_sample_details_t& frame_details) { _frame_details = frame_details; }
87  const ed247_timestamp_t& data_timestamp() const { return _data_timestamp; }
88  const ed247_timestamp_t& recv_timestamp() const { return _recv_timestamp; }
89  const ed247_sample_details_t& frame_details() const { return _frame_details; }
90 
91  // Set recive timestamp to now using default date function or user provided one
92  void update_recv_timestamp() { ed247_get_receive_timestamp(&_recv_timestamp); }
93 
94  protected:
95  ed247_timestamp_t _data_timestamp;
96  ed247_timestamp_t _recv_timestamp;
97  ed247_sample_details_t _frame_details;
98 
99  private:
100  using Sample::allocate; // Delete
101  };
102 
103 
104  //
105  // preallocated ring buffer
106  //
108  public:
109  StreamSampleRingBuffer(uint32_t capacity, uint32_t samples_capacity);
111 
112  uint32_t capacity() const { return _samples.size(); }
113  uint32_t samples_capacity() const { return _samples_capacity; }
114  uint32_t size() const { return _index_size; }
115  bool empty() const { return _index_size == 0; }
116  bool full() const { return _index_size >= _samples.size(); }
117 
118  // "push" a new sample.
119  // If ring buffer is full, override the oldest sample.
120  // Since memory is preallocated, return a reference to the "new" sample.
121  // Returned sample is not reseted but its content sahll be ignored.
122  StreamSample& push_back();
123 
124  // Pop the oldest sample.
125  // if ring buffer is empty, return an arbitrary sample. (i.e. call empty() before)
126  StreamSample& pop_front();
127 
128  // Return the oldest sample without removing it.
129  // if ring buffer is empty, return an arbitrary sample. (i.e. call empty() before)
130  StreamSample& front() { return _samples[_index_read]; }
131 
132  // Return the last pushed sample
133  // if ring buffer is empty, return an arbitrary sample. (i.e. call empty() before)
134  StreamSample& back()
135  {
136  return _samples[_index_write == 0 ? (_samples.size()-1) : (_index_write-1)];
137  }
138 
139  // Return the oldest + index sample
140  // index is not checked. May have undefined behavior.
141  StreamSample& at(uint32_t index)
142  {
143  return _samples[(_index_read + index) % _samples.size()];
144  }
145 
146  private:
147  std::vector<StreamSample> _samples;
148  uint32_t _samples_capacity;
149  uint32_t _index_read;
150  uint32_t _index_write;
151  uint32_t _index_size;
152  };
153 
154 }
155 
156 #endif
Definition: ed247_sample.h:14
Timestamp structure, seconds from EPOCH (January 1st 1970) and nanoseconds offset with reference to p...
Definition: ed247.h:209
LIBED247_EXPORT void ed247_get_receive_timestamp(ed247_timestamp_t *timestamp)
Return the time to timestamp the incoming streams.
Definition: ed247_time.cpp:107
Definition: ed247_sample.h:107
Definition: ed247_sample.h:56
Definition: ed247_channel.cpp:37
Sample Details.
Definition: ed247.h:909