1 #ifndef AUDIOGRAPHER_TYPE_UTILS_H
2 #define AUDIOGRAPHER_TYPE_UTILS_H
4 #include "audiographer/types.h"
5 #include <boost/static_assert.hpp>
6 #include <boost/type_traits.hpp>
11 namespace AudioGrapher
14 /// Non-template base class for TypeUtils
19 template<typename T, bool b>
20 static void do_zero_fill(T * buffer, framecnt_t frames, const boost::integral_constant<bool, b>&)
21 { std::uninitialized_fill_n (buffer, frames, T()); }
24 static void do_zero_fill(T * buffer, framecnt_t frames, const boost::true_type&)
25 { memset (buffer, 0, frames * sizeof(T)); }
28 /// Utilities for initializing, copying, moving, etc. data
29 template<typename T = DefaultSampleType>
30 class TypeUtils : private TypeUtilsBase
32 BOOST_STATIC_ASSERT (boost::has_trivial_destructor<T>::value);
34 typedef boost::integral_constant<bool,
35 boost::is_floating_point<T>::value ||
36 boost::is_signed<T>::value> zero_fillable;
38 /** Fill buffer with a zero value
39 * The value used for filling is either 0 or the value of T()
40 * if T is not a floating point or signed integer type
43 inline static void zero_fill (T * buffer, framecnt_t frames)
44 { do_zero_fill(buffer, frames, zero_fillable()); }
46 /** Copies \a frames frames of data from \a source to \a destination
47 * The source and destination may NOT overlap.
50 inline static void copy (T const * source, T * destination, framecnt_t frames)
51 { std::uninitialized_copy (source, &source[frames], destination); }
53 /** Moves \a frames frames of data from \a source to \a destination
54 * The source and destination may overlap in any way.
57 inline static void move (T const * source, T * destination, framecnt_t frames)
59 if (destination < source) {
60 std::copy (source, &source[frames], destination);
61 } else if (destination > source) {
62 std::copy_backward (source, &source[frames], destination + frames);
70 #endif // AUDIOGRAPHER_TYPE_UTILS_H