Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00025
00028
00029 #ifndef THOR_DISTRIBUTION_HPP
00030 #define THOR_DISTRIBUTION_HPP
00031
00032 #include <Aurora/Meta/Templates.hpp>
00033
00034 #include <functional>
00035 #include <type_traits>
00036
00037
00038 namespace thor
00039 {
00040
00041 template <typename T>
00042 class Distribution;
00043
00044 namespace detail
00045 {
00046
00047
00048 template <typename T>
00049 struct Constant
00050 {
00051 explicit Constant(T value)
00052 : value(value)
00053 {
00054 }
00055
00056 T operator() () const
00057 {
00058 return value;
00059 }
00060
00061 T value;
00062 };
00063
00064
00065 template <typename Fn, typename T>
00066 struct IsCompatibleFunction
00067 {
00068
00069 static const bool value = !std::is_convertible<Fn, T>::value;
00070 };
00071
00072 template <typename U, typename T>
00073 struct IsCompatibleFunction<Distribution<U>, T>
00074 {
00075
00076 static const bool value = std::is_convertible<U, T>::value;
00077 };
00078
00079 }
00080
00081
00082
00083
00086
00099 template <typename T>
00100 class Distribution
00101 {
00102
00103
00104 private:
00105 typedef std::function<T()> FactoryFn;
00106
00107
00108
00109
00110 public:
00113 template <typename U>
00114 Distribution(U constant
00115 AURORA_ENABLE_IF(std::is_convertible<U, T>::value))
00116 : mFactory(detail::Constant<T>(constant))
00117 {
00118 }
00119
00123 template <typename Fn>
00124 Distribution(Fn function
00125 AURORA_ENABLE_IF(detail::IsCompatibleFunction<Fn, T>::value))
00126 : mFactory(function)
00127 {
00128 }
00129
00132 T operator() () const
00133 {
00134 return mFactory();
00135 }
00136
00137
00138
00139
00140 private:
00141 FactoryFn mFactory;
00142 };
00143
00145
00146 }
00147
00148 #endif // THOR_DISTRIBUTION_HPP