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