mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ndarray_ring.h
Go to the documentation of this file.
1 #ifndef MF_NDARRAY_RING_H_
2 #define MF_NDARRAY_RING_H_
3 
4 #include "../ndarray/generic/ndarray_view_generic.h"
5 #include "../ndarray/generic/ndarray_timed_view_generic.h"
6 #include "ring.h"
7 #include "timed_ring.h"
8 #include "shared_ring.h"
9 
10 namespace mf {
11 
12 // TODO remove?, adapt tests
13 
14 namespace detail {
15  template<std::size_t Frame_dim, typename Elem>
16  class ndarray_ring_base {
17  public:
18  constexpr static std::size_t concrete_dim = Frame_dim + 1;
19  using frame_shape_type = ndsize<Frame_dim>;
20  using frame_view_type = ndarray_view<Frame_dim, Elem>;
21  using section_view_type = ndarray_view<concrete_dim, Elem>;
22 
23  private:
24  frame_shape_type frame_shape_;
25 
26  protected:
27  ndarray_generic_properties properties_(std::size_t duration) const noexcept {
28  return ndarray_generic_properties(
29  frame_format::default_format<Elem>(),
30  frame_shape_.product(),
31  duration
32  );
33  }
34 
35  public:
36  explicit ndarray_ring_base(const frame_shape_type& shp) : frame_shape_(shp) { }
37 
38  const frame_shape_type& frame_shape() const noexcept { return frame_shape_; }
39  };
40 }
41 
42 
44 template<std::size_t Frame_dim, typename Elem>
45 class ndarray_ring : public detail::ndarray_ring_base<Frame_dim, Elem>, public ring {
46  using base = detail::ndarray_ring_base<Frame_dim, Elem>;
47 
48 public:
49  using base::concrete_dim;
53 
54  ndarray_ring(const frame_shape_type& frame_shape, time_unit dur) :
55  base(frame_shape),
56  ring(base::properties_(dur)) { }
57 
59  return from_generic<concrete_dim, Elem>(ring::begin_write(duration), base::frame_shape());
60  }
61 
63  return from_generic<concrete_dim, Elem>(ring::begin_read(duration), base::frame_shape());
64  }
65 };
66 
67 
69 template<std::size_t Frame_dim, typename Elem>
70 class ndarray_timed_ring : public detail::ndarray_ring_base<Frame_dim, Elem>, public timed_ring {
71  using base = detail::ndarray_ring_base<Frame_dim, Elem>;
72 
73 public:
74  using base::concrete_dim;
78 
79  ndarray_timed_ring(const frame_shape_type& frame_shape, time_unit dur) :
80  base(frame_shape),
81  timed_ring(base::properties_(dur)) { }
82 
84  return from_generic<concrete_dim, Elem>(timed_ring::begin_write(duration), base::frame_shape());
85  }
86 
88  return from_generic<concrete_dim, Elem>(timed_ring::begin_write_span(span), base::frame_shape());
89  }
90 
92  return from_generic<concrete_dim, Elem>(timed_ring::begin_read(duration), base::frame_shape());
93  }
94 
96  return from_generic<concrete_dim, Elem>(timed_ring::begin_read_span(span), base::frame_shape());
97  }
98 };
99 
100 
102 template<std::size_t Frame_dim, typename Elem>
103 class ndarray_shared_ring : public detail::ndarray_ring_base<Frame_dim, Elem>, public shared_ring {
104  using base = detail::ndarray_ring_base<Frame_dim, Elem>;
105 
106 public:
107  using base::concrete_dim;
111 
112  ndarray_shared_ring(const frame_shape_type& frame_shape, std::size_t dur, bool seekable, time_unit end_time = -1) :
113  base(frame_shape),
114  shared_ring(base::properties_(dur), seekable, end_time) { }
115 
117  return from_generic<concrete_dim, Elem>(shared_ring::begin_write(write_duration), base::frame_shape());
118  }
119 
120  bool try_begin_write(time_unit write_duration, section_view_type& section) {
121  shared_ring::section_view_type generic_section;
122  if(shared_ring::try_begin_write(write_duration, generic_section)) {
123  section.reset(from_generic<concrete_dim, Elem>(generic_section, base::frame_shape()));
124  return true;
125  } else {
126  return false;
127  }
128  }
129 
131  return from_generic<concrete_dim, Elem>(shared_ring::begin_read_span(span), base::frame_shape());
132  }
133 
135  return from_generic<concrete_dim, Elem>(shared_ring::begin_read(read_duration), base::frame_shape());
136  }
137 
138  bool try_begin_read(time_unit read_duration, section_view_type& section) {
139  shared_ring::section_view_type generic_section;
140  if(shared_ring::try_begin_read(read_duration, generic_section)) {
141  section.reset(from_generic<concrete_dim, Elem>(generic_section, base::frame_shape()));
142  return true;
143  } else {
144  return false;
145  }
146  }
147 };
148 
149 }
150 
151 #endif
Ring buffer.
Definition: ring.h:12
section_view_type begin_read_span(time_span)
Definition: timed_ring.cc:83
ndarray_shared_ring(const frame_shape_type &frame_shape, std::size_t dur, bool seekable, time_unit end_time=-1)
Definition: ndarray_ring.h:112
Mapping between coordinates, indices, and addresses of multi-dimensional data.
Definition: ndarray_view.h:16
section_view_type begin_read(time_unit)
Definition: timed_ring.cc:77
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
section_view_type begin_write(time_unit duration)
Definition: ring.cc:86
bool try_begin_read(time_unit read_duration, section_view_type &section)
Definition: ndarray_ring.h:138
Ndarray view with absolute time indices associated to first dimension.
Definition: ndarray_timed_view.h:13
ndarray_timed_ring(const frame_shape_type &frame_shape, time_unit dur)
Definition: ndarray_ring.h:79
Shared ring wrapper with concrete frame type.
Definition: ndarray_ring.h:103
section_view_type begin_read_span(time_span span)
Definition: ndarray_ring.h:95
section_view_type begin_write_span(time_span)
Definition: timed_ring.cc:71
section_view_type begin_write(time_unit write_duration)
Definition: ndarray_ring.h:116
section_view_type begin_read_span(time_span)
Begin reading frames at time span span.
Definition: shared_ring.cc:142
section_view_type begin_read(time_unit read_duration)
Definition: ndarray_ring.h:134
section_view_type begin_read_span(time_span span)
Definition: ndarray_ring.h:130
section_view_type begin_write(time_unit)
Definition: timed_ring.cc:65
section_view_type begin_write(time_unit duration)
Definition: ndarray_ring.h:83
time_unit end_time() const
End of file time. */.
Definition: shared_ring.h:195
Timed ring wrapper with concrete frame type.
Definition: ndarray_ring.h:70
section_view_type begin_read(time_unit duration)
Definition: ndarray_ring.h:91
Timed ring buffer with changed semantics, for dual-thread use.
Definition: shared_ring.h:32
void reset(const ndarray_view &other) noexcept
Definition: ndarray_view.tcc:129
section_view_type begin_write(time_unit duration)
Definition: ndarray_ring.h:58
Vector of n-dimensional coordinates.
Definition: ndcoord.h:18
section_view_type begin_read(time_unit duration)
Definition: ndarray_ring.h:62
section_view_type begin_write_span(time_span span)
Definition: ndarray_ring.h:87
Ring buffer which adds absolute time index to frames.
Definition: timed_ring.h:13
section_view_type try_begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time, if they are available.
Definition: shared_ring.cc:206
One-dimensional time span.
Definition: common.h:61
Ring wrapper with concrete frame type.
Definition: ndarray_ring.h:45
bool try_begin_write(time_unit write_duration, section_view_type &section)
Definition: ndarray_ring.h:120
section_view_type begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time.
Definition: shared_ring.cc:23
section_view_type try_begin_write(time_unit write_duration)
Begin writing write_duration frames at current write start time, if they are available.
Definition: shared_ring.cc:84
ndarray_ring(const frame_shape_type &frame_shape, time_unit dur)
Definition: ndarray_ring.h:54
Generic ndarray_timed_view where lower dimension(s) are type-erased.
Definition: ndarray_timed_view_generic.h:12
section_view_type begin_read(time_unit read_duration)
Begin reading read_duration frames at current read start time.
Definition: shared_ring.cc:161
section_view_type begin_read(time_unit duration)
Definition: ring.cc:101