Distribution.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_DISTRIBUTION_HPP
30 #define THOR_DISTRIBUTION_HPP
31 
32 #include <Aurora/Meta/Templates.hpp>
33 
34 #include <functional>
35 #include <type_traits>
36 
37 
38 namespace thor
39 {
40 
41 template <typename T>
43 
44 namespace detail
45 {
46 
47  // Functor that returns always the same value (don't use lambda expression because of Clang compiler bug)
48  template <typename T>
49  struct Constant
50  {
51  explicit Constant(T value)
52  : value(value)
53  {
54  }
55 
56  T operator() () const
57  {
58  return value;
59  }
60 
61  T value;
62  };
63 
64  // Metafunction for SFINAE and reasonable compiler errors
65  template <typename Fn, typename T>
66  struct IsCompatibleFunction
67  {
68  // General case: Fn is a functor/function -> if it's not convertible to T (and thus not a constant), accept it
69  static const bool value = !std::is_convertible<Fn, T>::value;
70  };
71 
72  template <typename U, typename T>
73  struct IsCompatibleFunction<Distribution<U>, T>
74  {
75  // If Fn is another Distribution<U>, accept it iff U is convertible to T (like all functors, but clearer error message)
76  static const bool value = std::is_convertible<U, T>::value;
77  };
78 
79 } // namespace detail
80 
81 // ---------------------------------------------------------------------------------------------------------------------------
82 
83 
86 
99 template <typename T>
100 class Distribution
101 {
102  // ---------------------------------------------------------------------------------------------------------------------------
103  // Private types
104  private:
105  typedef std::function<T()> FactoryFn;
106 
107 
108  // ---------------------------------------------------------------------------------------------------------------------------
109  // Public member functions
110  public:
113  template <typename U>
114  Distribution(U constant
115  AURORA_ENABLE_IF(std::is_convertible<U, T>::value))
116  : mFactory(detail::Constant<T>(constant))
117  {
118  }
119 
123  template <typename Fn>
124  Distribution(Fn function
125  AURORA_ENABLE_IF(detail::IsCompatibleFunction<Fn, T>::value))
126  : mFactory(function)
127  {
128  }
129 
132  T operator() () const
133  {
134  return mFactory();
135  }
136 
137 
138  // ---------------------------------------------------------------------------------------------------------------------------
139  // Private variables
140  private:
141  FactoryFn mFactory;
142 };
143 
145 
146 } // namespace thor
147 
148 #endif // THOR_DISTRIBUTION_HPP
Distribution(Fn function)
Construct from distribution function.
Definition: Distribution.hpp:124
Class holding a rule to create values with predefined properties.
Definition: Distribution.hpp:42
T operator()() const
Returns a value according to the distribution.
Definition: Distribution.hpp:132
Definition: Animator.hpp:42
Distribution(U constant)
Construct from constant.
Definition: Distribution.hpp:114