Math/Distribution.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2015 Jan Haller
00005 // 
00006 // This software is provided 'as-is', without any express or implied
00007 // warranty. In no event will the authors be held liable for any damages
00008 // arising from the use of this software.
00009 // 
00010 // Permission is granted to anyone to use this software for any purpose,
00011 // including commercial applications, and to alter it and redistribute it
00012 // freely, subject to the following restrictions:
00013 // 
00014 // 1. The origin of this software must not be misrepresented; you must not
00015 //    claim that you wrote the original software. If you use this software
00016 //    in a product, an acknowledgment in the product documentation would be
00017 //    appreciated but is not required.
00018 // 
00019 // 2. Altered source versions must be plainly marked as such, and must not be
00020 //    misrepresented as being the original software.
00021 // 
00022 // 3. This notice may not be removed or altered from any source distribution.
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     // Functor that returns always the same value (don't use lambda expression because of Clang compiler bug)
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     // Metafunction for SFINAE and reasonable compiler errors
00065     template <typename Fn, typename T>
00066     struct IsCompatibleFunction
00067     {
00068         // General case: Fn is a functor/function -> if it's not convertible to T (and thus not a constant), accept it
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         // If Fn is another Distribution<U>, accept it iff U is convertible to T (like all functors, but clearer error message)
00076         static const bool value = std::is_convertible<U, T>::value;
00077     };
00078 
00079 } // namespace detail
00080     
00081 // ---------------------------------------------------------------------------------------------------------------------------
00082     
00083     
00086 
00099 template <typename T>
00100 class Distribution
00101 {
00102     // ---------------------------------------------------------------------------------------------------------------------------
00103     // Private types
00104     private:
00105         typedef std::function<T()> FactoryFn;
00106     
00107         
00108     // ---------------------------------------------------------------------------------------------------------------------------
00109     // Public member functions
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     // Private variables
00140     private:
00141         FactoryFn                   mFactory;   
00142 };
00143 
00145 
00146 } // namespace thor
00147 
00148 #endif // THOR_DISTRIBUTION_HPP