1 #ifndef AUDIOGRAPHER_TYPE_UTILS_H
2 #define AUDIOGRAPHER_TYPE_UTILS_H
4 #include <boost/static_assert.hpp>
5 #include <boost/type_traits.hpp>
10 #include "audiographer/visibility.h"
11 #include "audiographer/types.h"
13 namespace AudioGrapher
16 /// Non-template base class for TypeUtils
17 class LIBAUDIOGRAPHER_API TypeUtilsBase
21 template<typename T, bool b>
22 static void do_zero_fill(T * buffer, samplecnt_t samples, const boost::integral_constant<bool, b>&)
23 { std::uninitialized_fill_n (buffer, samples, T()); }
26 static void do_zero_fill(T * buffer, samplecnt_t samples, const boost::true_type&)
27 { memset (buffer, 0, samples * sizeof(T)); }
30 /// Utilities for initializing, copying, moving, etc. data
31 template<typename T = DefaultSampleType>
32 class /*LIBAUDIOGRAPHER_API*/ TypeUtils : private TypeUtilsBase
34 BOOST_STATIC_ASSERT (boost::has_trivial_destructor<T>::value);
36 typedef boost::integral_constant<bool,
37 boost::is_floating_point<T>::value ||
38 boost::is_signed<T>::value> zero_fillable;
40 /** Fill buffer with a zero value
41 * The value used for filling is either 0 or the value of T()
42 * if T is not a floating point or signed integer type
45 inline static void zero_fill (T * buffer, samplecnt_t samples)
46 { do_zero_fill(buffer, samples, zero_fillable()); }
48 /** Copies \a samples frames of data from \a source to \a destination
49 * The source and destination may NOT overlap.
52 inline static void copy (T const * source, T * destination, samplecnt_t samples)
53 { std::uninitialized_copy (source, &source[samples], destination); }
55 /** Moves \a samples frames of data from \a source to \a destination
56 * The source and destination may overlap in any way.
59 inline static void move (T const * source, T * destination, samplecnt_t samples)
61 if (destination < source) {
62 std::copy (source, &source[samples], destination);
63 } else if (destination > source) {
64 std::copy_backward (source, &source[samples], destination + samples);
72 #endif // AUDIOGRAPHER_TYPE_UTILS_H