Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / audiographer / process_context.h
1 #ifndef AUDIOGRAPHER_PROCESS_CONTEXT_H
2 #define AUDIOGRAPHER_PROCESS_CONTEXT_H
3
4 #include "types.h"
5
6 #include <cstring>
7
8 namespace AudioGrapher
9 {
10
11 /**
12  * Processing context. Constness only applies to data, not flags
13  */
14
15 template <typename T>
16 class ProcessContext  {
17         
18 public:
19
20         typedef FlagField::Flag Flag;
21
22         enum Flags {
23                 EndOfInput = 0
24         };
25
26 public:
27
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) {}
31         
32         /// Normal copy constructor
33         ProcessContext (ProcessContext<T> const & other)
34                 : _data (other._data), _frames (other._frames), _channels (other._channels), _flags (other._flags) {}
35         
36         /// "Copy constructor" with unique data, frame and channel count, but copies flags
37         template<typename Y>
38         ProcessContext (ProcessContext<Y> const & other, T * data, nframes_t frames, ChannelCount channels)
39                 : _data (data), _frames (frames), _channels (channels), _flags (other.flags()) {}
40         
41         /// "Copy constructor" with unique data and frame count, but copies channel count and flags
42         template<typename Y>
43         ProcessContext (ProcessContext<Y> const & other, T * data, nframes_t frames)
44                 : _data (data), _frames (frames), _channels (other.channels()), _flags (other.flags()) {}
45         
46         /// "Copy constructor" with unique data, but copies frame and channel count + flags
47         template<typename Y>
48         ProcessContext (ProcessContext<Y> const & other, T * data)
49                 : _data (data), _frames (other.frames()), _channels (other.channels()), _flags (other.flags()) {}
50         
51         virtual ~ProcessContext () {}
52         
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; }
56         
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; }
60         
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
63           */
64         inline ChannelCount const & channels() const { return _channels; }
65         inline ChannelCount &       channels()       { return _channels; }
66         
67         /// Returns the amount of frames per channel
68         inline nframes_t            frames_per_channel() const { return _frames / _channels; }
69
70         /* Flags */
71         
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; }
76         
77 protected:
78         T * const              _data;
79         nframes_t              _frames;
80         ChannelCount           _channels;
81         
82         mutable FlagField      _flags;
83 };
84
85 /// A process context that allocates and owns it's data buffer
86 template <typename T>
87 struct AllocatingProcessContext : public ProcessContext<T>
88 {
89         /// Allocates uninitialized memory
90         AllocatingProcessContext (nframes_t frames, ChannelCount channels)
91                 : ProcessContext<T> (new T[frames], frames, channels) {}
92         
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)); }
97         
98         /// "Copy constructor" with uninitialized data, unique frame and channel count, but copies flags
99         template<typename Y>
100         AllocatingProcessContext (ProcessContext<Y> const & other, nframes_t frames, ChannelCount channels)
101                 : ProcessContext<T> (other, new T[frames], frames, channels) {}
102         
103         /// "Copy constructor" with uninitialized data, unique frame count, but copies channel count and flags
104         template<typename Y>
105         AllocatingProcessContext (ProcessContext<Y> const & other, nframes_t frames)
106                 : ProcessContext<T> (other, new T[frames], frames, other.channels()) {}
107         
108         /// "Copy constructor" uninitialized data, that copies frame and channel count + flags
109         template<typename Y>
110         AllocatingProcessContext (ProcessContext<Y> const & other)
111                 : ProcessContext<T> (other, new T[other._frames]) {}
112         
113         ~AllocatingProcessContext () { delete [] ProcessContext<T>::_data; }
114 };
115
116 /// A wrapper for a const ProcesContext which can be created from const data
117 template <typename T>
118 class ConstProcessContext
119 {
120   public:
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) {}
124         
125         /// Copy constructor from const ProcessContext
126         ConstProcessContext (ProcessContext<T> const & other)
127           : context (const_cast<ProcessContext<T> &> (other)) {}
128         
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) {}
133         
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) {}
138         
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)) {}
143
144         inline operator ProcessContext<T> const & () { return context; }
145         inline ProcessContext<T> const & operator() () { return context; }
146         inline ProcessContext<T> const * operator& () { return &context; }
147
148   private:
149           ProcessContext<T> const context;
150 };
151
152 } // namespace
153
154 #endif // AUDIOGRAPHER_PROCESS_CONTEXT_H