fix breakage for optimized build caused by semantically critical statement inside...
[ardour.git] / libs / audiographer / audiographer / type_utils.h
1 #ifndef AUDIOGRAPHER_TYPE_UTILS_H
2 #define AUDIOGRAPHER_TYPE_UTILS_H
3
4 #include "audiographer/types.h"
5 #include <boost/static_assert.hpp>
6 #include <boost/type_traits.hpp>
7 #include <memory>
8 #include <algorithm>
9 #include <cstring>
10
11 namespace AudioGrapher
12 {
13
14 /// Non-template base class for TypeUtils
15 class TypeUtilsBase
16 {
17   protected:
18         
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()); }
22
23         template<typename T>
24         static void do_zero_fill(T * buffer, framecnt_t frames, const boost::true_type&)
25                 { memset (buffer, 0, frames * sizeof(T)); }
26 };
27
28 /// Utilities for initializing, copying, moving, etc. data
29 template<typename T = DefaultSampleType>
30 class TypeUtils : private TypeUtilsBase
31 {
32         BOOST_STATIC_ASSERT (boost::has_trivial_destructor<T>::value);
33         
34         typedef boost::integral_constant<bool, 
35                         boost::is_floating_point<T>::value ||
36                         boost::is_signed<T>::value> zero_fillable;
37   public:
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
41           * \n RT safe
42           */
43         inline static void zero_fill (T * buffer, framecnt_t frames)
44                 { do_zero_fill(buffer, frames, zero_fillable()); }
45         
46         /** Copies \a frames frames of data from \a source to \a destination
47           * The source and destination may NOT overlap.
48           * \n RT safe
49           */
50         inline static void copy (T const * source, T * destination, framecnt_t frames)
51                 { std::uninitialized_copy (source, &source[frames], destination); }
52         
53         /** Moves \a frames frames of data from \a source to \a destination
54           * The source and destination may overlap in any way.
55           * \n RT safe
56           */
57         inline static void move (T const * source, T * destination, framecnt_t frames)
58         {
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);
63                 }
64         }
65 };
66
67
68 } // namespace
69
70 #endif // AUDIOGRAPHER_TYPE_UTILS_H