Ardour  8.12
process_context.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_PROCESS_CONTEXT_H
2 #define AUDIOGRAPHER_PROCESS_CONTEXT_H
3 
4 #include <boost/static_assert.hpp>
5 #include <boost/format.hpp>
6 
8 #include "exception.h"
9 #include "debug_utils.h"
10 #include "types.h"
11 #include "flag_field.h"
12 #include "throwing.h"
13 #include "type_utils.h"
14 
15 namespace AudioGrapher
16 {
17 
18 
23 template <typename T = DefaultSampleType>
24 class /*LIBAUDIOGRAPHER_API*/ ProcessContext
25  : public Throwing<>
26 {
27  // Support older compilers that don't support template base class initialization without template parameters
28  // This will need to be modified if if it's modified above
30 
31  BOOST_STATIC_ASSERT (boost::has_trivial_destructor<T>::value);
32 
33 public:
34 
36 
37  enum Flags {
38  EndOfInput = 0
39  };
40 
41 public:
42 
46  { validate_data(); }
47 
50  : Throwing<throwLevel>(), _data (other._data), _samples (other._samples), _channels (other._channels), _flags (other._flags)
51  { /* No need to validate data */ }
52 
54  template<typename Y>
57  { validate_data(); }
58 
60  template<typename Y>
62  : Throwing<throwLevel>(), _data (data), _samples (samples), _channels (other.channels()), _flags (other.flags())
63  { validate_data(); }
64 
66  template<typename Y>
67  ProcessContext (ProcessContext<Y> const & other, T * data)
68  : Throwing<throwLevel>(), _data (data), _samples (other.samples()), _channels (other.channels()), _flags (other.flags())
69  { /* No need to validate data */ }
70 
73  {
75  throw Exception (*this, boost::str (boost::format
76  ("Trying to use too many samples of %1% for a new Context: %2% instead of %3%")
78  }
79  validate_data ();
80 
81  return ProcessContext (*this, _data, samples);
82  }
83 
84  virtual ~ProcessContext () {}
85 
87  inline T const * data() const { return _data; }
88  inline T * data() { return _data; }
89 
91  inline samplecnt_t const & samples() const { return _samples; }
92 
96  inline ChannelCount const & channels() const { return _channels; }
97 
99  inline samplecnt_t samples_per_channel() const { return _samples / _channels; }
100 
101  /* Flags */
102 
103  inline bool has_flag (Flag flag) const { return _flags.has (flag); }
104  inline void set_flag (Flag flag) const { _flags.set (flag); }
105  inline void remove_flag (Flag flag) const { _flags.remove (flag); }
106  inline FlagField const & flags () const { return _flags; }
107 
108 protected:
109  T * const _data;
112 
113  mutable FlagField _flags;
114 
115  private:
116  inline void validate_data()
117  {
118  if (throw_level (ThrowProcess) && (_samples % _channels != 0)) {
119  throw Exception (*this, boost::str (boost::format
120  ("Number of samples given to %1% was not a multiple of channels: %2% samples with %3% channels")
122  }
123  }
124 };
125 
127 template <typename T = DefaultSampleType>
128 class /*LIBAUDIOGRAPHER_API*/ AllocatingProcessContext : public ProcessContext<T>
129 {
130 public:
133  : ProcessContext<T> (new T[samples], samples, channels) {}
134 
137  : ProcessContext<T> (new T[samples], samples, channels)
139 
142  : ProcessContext<T> (other, new T[other._samples])
144 
146  template<typename Y>
148  : ProcessContext<T> (other, new T[samples], samples, channels) {}
149 
151  template<typename Y>
153  : ProcessContext<T> (other, new T[samples], samples, other.channels()) {}
154 
156  template<typename Y>
158  : ProcessContext<T> (other, new T[other._samples]) {}
159 
161 };
162 
164 template <typename T = DefaultSampleType>
165 class /*LIBAUDIOGRAPHER_API*/ ConstProcessContext
166 {
167  public:
169  ConstProcessContext (T const * data, samplecnt_t samples, ChannelCount channels)
170  : context (const_cast<T *>(data), samples, channels) {}
171 
174  : context (const_cast<ProcessContext<T> &> (other)) {}
175 
177  template<typename ProcessContext>
178  ConstProcessContext (ProcessContext const & other, T const * data, samplecnt_t samples, ChannelCount channels)
179  : context (other, const_cast<T *>(data), samples, channels) {}
180 
182  template<typename ProcessContext>
183  ConstProcessContext (ProcessContext const & other, T const * data, samplecnt_t samples)
184  : context (other, const_cast<T *>(data), samples) {}
185 
187  template<typename ProcessContext>
188  ConstProcessContext (ProcessContext const & other, T const * data)
189  : context (other, const_cast<T *>(data)) {}
190 
191  inline operator ProcessContext<T> const & () { return context; }
192  inline ProcessContext<T> const & operator() () { return context; }
193  inline ProcessContext<T> const * operator& () { return &context; }
194 
195  private:
197 };
198 
199 } // namespace
200 
201 #endif // AUDIOGRAPHER_PROCESS_CONTEXT_H
A process context that allocates and owns it's data buffer.
AllocatingProcessContext(ProcessContext< Y > const &other, samplecnt_t samples)
"Copy constructor" with uninitialized data, unique sample count, but copies channel count and flags
AllocatingProcessContext(ProcessContext< Y > const &other)
"Copy constructor" uninitialized data, that copies sample and channel count + flags
AllocatingProcessContext(ProcessContext< Y > const &other, samplecnt_t samples, ChannelCount channels)
"Copy constructor" with uninitialized data, unique sample and channel count, but copies flags
AllocatingProcessContext(ProcessContext< T > const &other)
Copy constructor, copies data from other ProcessContext.
AllocatingProcessContext(samplecnt_t samples, ChannelCount channels)
Allocates uninitialized memory.
AllocatingProcessContext(T const *data, samplecnt_t samples, ChannelCount channels)
Allocates and copies data from raw buffer.
A wrapper for a const ProcesContext which can be created from const data.
ConstProcessContext(ProcessContext const &other, T const *data, samplecnt_t samples)
"Copy constructor", with unique data and sample count, but copies channel count and flags
ConstProcessContext(ProcessContext const &other, T const *data)
"Copy constructor", with unique data, but copies sample and channel count + flags
ConstProcessContext(ProcessContext const &other, T const *data, samplecnt_t samples, ChannelCount channels)
"Copy constructor", with unique data, sample and channel count, but copies flags
ProcessContext< T > const context
ProcessContext< T > const & operator()()
ConstProcessContext(T const *data, samplecnt_t samples, ChannelCount channels)
Basic constructor with data, sample and channel count.
ConstProcessContext(ProcessContext< T > const &other)
Copy constructor from const ProcessContext.
ProcessContext< T > const * operator&()
void set(Flag flag)
Definition: flag_field.h:76
bool has(Flag flag) const
Definition: flag_field.h:73
void remove(Flag flag)
Definition: flag_field.h:77
ChannelCount const & channels() const
bool has_flag(Flag flag) const
T const * data() const
data points to the array of data to process
ProcessContext beginning(samplecnt_t samples)
Make new Context out of the beginning of this context.
ProcessContext(T *data, samplecnt_t samples, ChannelCount channels)
Basic constructor with data, sample and channel count.
BOOST_STATIC_ASSERT(boost::has_trivial_destructor< T >::value)
ProcessContext(ProcessContext< Y > const &other, T *data)
"Copy constructor" with unique data, but copies sample and channel count + flags
static const ThrowLevel throwLevel
samplecnt_t samples_per_channel() const
Returns the amount of samples per channel.
ProcessContext(ProcessContext< T > const &other)
Normal copy constructor.
FlagField const & flags() const
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
void set_flag(Flag flag) const
ProcessContext(ProcessContext< Y > const &other, T *data, samplecnt_t samples)
"Copy constructor" with unique data and sample count, but copies channel count and flags
void remove_flag(Flag flag) const
ProcessContext(ProcessContext< Y > const &other, T *data, samplecnt_t samples, ChannelCount channels)
"Copy constructor" with unique data, sample and channel count, but copies flags
bool throw_level(ThrowLevel level)
Definition: throwing.h:47
static void copy(T const *source, T *destination, samplecnt_t samples)
Definition: type_utils.h:52
@ ThrowProcess
Process cycle level stuff.
Definition: throwing.h:23
static std::string demangled_name(T const &obj)
Returns the demangled name of the object passed as the parameter.
Definition: debug_utils.h:24
#define DEFAULT_THROW_LEVEL
Definition: throwing.h:5