1 #ifndef AUDIOGRAPHER_PROCESS_CONTEXT_H
2 #define AUDIOGRAPHER_PROCESS_CONTEXT_H
12 * Processing context. Constness only applies to data, not flags
16 class ProcessContext {
20 typedef FlagField::Flag Flag;
28 /// Basic constructor with data, frame and channel count
29 ProcessContext (T * data, nframes_t frames, ChannelCount channels)
30 : _data (data), _frames (frames), _channels (channels) {}
32 /// Normal copy constructor
33 ProcessContext (ProcessContext<T> const & other)
34 : _data (other._data), _frames (other._frames), _channels (other._channels), _flags (other._flags) {}
36 /// "Copy constructor" with unique data, frame and channel count, but copies flags
38 ProcessContext (ProcessContext<Y> const & other, T * data, nframes_t frames, ChannelCount channels)
39 : _data (data), _frames (frames), _channels (channels), _flags (other.flags()) {}
41 /// "Copy constructor" with unique data and frame count, but copies channel count and flags
43 ProcessContext (ProcessContext<Y> const & other, T * data, nframes_t frames)
44 : _data (data), _frames (frames), _channels (other.channels()), _flags (other.flags()) {}
46 /// "Copy constructor" with unique data, but copies frame and channel count + flags
48 ProcessContext (ProcessContext<Y> const & other, T * data)
49 : _data (data), _frames (other.frames()), _channels (other.channels()), _flags (other.flags()) {}
51 virtual ~ProcessContext () {}
53 /// \a data points to the array of data to process
54 inline T const * data() const { return _data; }
55 inline T * data() { return _data; }
57 /// \a frames tells how many frames the array pointed by data contains
58 inline nframes_t const & frames() const { return _frames; }
59 inline nframes_t & frames() { return _frames; }
61 /** \a channels tells how many interleaved channels \a data contains
62 * If \a channels is greater than 1, each channel contains \a frames / \a channels frames of data
64 inline ChannelCount const & channels() const { return _channels; }
65 inline ChannelCount & channels() { return _channels; }
67 /// Returns the amount of frames per channel
68 inline nframes_t frames_per_channel() const { return _frames / _channels; }
72 inline bool has_flag (Flag flag) const { return _flags.has (flag); }
73 inline void set_flag (Flag flag) const { _flags.set (flag); }
74 inline void remove_flag (Flag flag) const { _flags.remove (flag); }
75 inline FlagField const & flags () const { return _flags; }
80 ChannelCount _channels;
82 mutable FlagField _flags;
85 /// A process context that allocates and owns it's data buffer
87 struct AllocatingProcessContext : public ProcessContext<T>
89 /// Allocates uninitialized memory
90 AllocatingProcessContext (nframes_t frames, ChannelCount channels)
91 : ProcessContext<T> (new T[frames], frames, channels) {}
93 /// Copy constructor, copies data from other ProcessContext
94 AllocatingProcessContext (ProcessContext<T> const & other)
95 : ProcessContext<T> (other, new T[other._frames])
96 { memcpy (ProcessContext<T>::_data, other._data, other._channels * other._frames * sizeof (T)); }
98 /// "Copy constructor" with uninitialized data, unique frame and channel count, but copies flags
100 AllocatingProcessContext (ProcessContext<Y> const & other, nframes_t frames, ChannelCount channels)
101 : ProcessContext<T> (other, new T[frames], frames, channels) {}
103 /// "Copy constructor" with uninitialized data, unique frame count, but copies channel count and flags
105 AllocatingProcessContext (ProcessContext<Y> const & other, nframes_t frames)
106 : ProcessContext<T> (other, new T[frames], frames, other.channels()) {}
108 /// "Copy constructor" uninitialized data, that copies frame and channel count + flags
110 AllocatingProcessContext (ProcessContext<Y> const & other)
111 : ProcessContext<T> (other, new T[other._frames]) {}
113 ~AllocatingProcessContext () { delete [] ProcessContext<T>::_data; }
116 /// A wrapper for a const ProcesContext which can be created from const data
117 template <typename T>
118 class ConstProcessContext
121 /// Basic constructor with data, frame and channel count
122 ConstProcessContext (T const * data, nframes_t frames, ChannelCount channels)
123 : context (const_cast<T *>(data), frames, channels) {}
125 /// Copy constructor from const ProcessContext
126 ConstProcessContext (ProcessContext<T> const & other)
127 : context (const_cast<ProcessContext<T> &> (other)) {}
129 /// "Copy constructor", with unique data, frame and channel count, but copies flags
130 template<typename ProcessContext>
131 ConstProcessContext (ProcessContext const & other, T const * data, nframes_t frames, ChannelCount channels)
132 : context (other, const_cast<T *>(data), frames, channels) {}
134 /// "Copy constructor", with unique data and frame count, but copies channel count and flags
135 template<typename ProcessContext>
136 ConstProcessContext (ProcessContext const & other, T const * data, nframes_t frames)
137 : context (other, const_cast<T *>(data), frames) {}
139 /// "Copy constructor", with unique data, but copies frame and channel count + flags
140 template<typename ProcessContext>
141 ConstProcessContext (ProcessContext const & other, T const * data)
142 : context (other, const_cast<T *>(data)) {}
144 inline operator ProcessContext<T> const & () { return context; }
145 inline ProcessContext<T> const & operator() () { return context; }
146 inline ProcessContext<T> const * operator& () { return &context; }
149 ProcessContext<T> const context;
154 #endif // AUDIOGRAPHER_PROCESS_CONTEXT_H