Embedded Template Library  1.0
functional.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_FUNCTIONAL_INCLUDED
32 #define ETL_FUNCTIONAL_INCLUDED
33 
34 #include "platform.h"
35 
38 
41 
42 namespace etl
43 {
44  //***************************************************************************
47  //***************************************************************************
48  template <typename T>
50  {
51  public:
52 
53  typedef T type;
54 
55  ETL_CONSTEXPR20 explicit reference_wrapper(T& t_) ETL_NOEXCEPT
56  : t(&t_)
57  {
58  }
59 
60  ETL_CONSTEXPR20 reference_wrapper<T>& operator = (const reference_wrapper& rhs) ETL_NOEXCEPT
61  {
62  t = rhs.t;
63  return *this;
64  }
65 
66  ETL_CONSTEXPR20 T& get() const ETL_NOEXCEPT
67  {
68  return *t;
69  }
70 
71  ETL_CONSTEXPR20 operator T&() const ETL_NOEXCEPT
72  {
73  return *t;
74  }
75 
76  private:
77 
78  T* t;
79  };
80 
81  //***************************************************************************
82  template <typename T>
83  reference_wrapper<T> ref(T& t)
84  {
85  return reference_wrapper<T>(t);
86  }
87 
88  //***************************************************************************
89  template <typename T>
90  reference_wrapper<T> ref(reference_wrapper<T> t)
91  {
92  return reference_wrapper<T>(t.get());
93  }
94 
95  //***************************************************************************
96  template <typename T>
97  reference_wrapper<const T> cref(const T& t)
98  {
99  return reference_wrapper<const T>(t);
100  }
101 
102  //***************************************************************************
103  template <typename T>
104  reference_wrapper<const T> cref(reference_wrapper<T> t)
105  {
106  return reference_wrapper<const T>(t.get());
107  }
108 
109  //***************************************************************************
110  template <typename T = void>
111  struct less
112  {
113  typedef T value_type;
114 
115  ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
116  {
117  return lhs < rhs;
118  }
119  };
120 
121  //***************************************************************************
122  template <typename T = void>
123  struct greater
124  {
125  typedef T value_type;
126 
127  ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
128  {
129  return lhs > rhs;
130  }
131  };
132 
133  //***************************************************************************
134  template <typename T = void>
135  struct equal_to
136  {
137  typedef T value_type;
138 
139  ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
140  {
141  return lhs == rhs;
142  }
143  };
144 
145  //***************************************************************************
146  template <typename T = void>
148  {
149  typedef T value_type;
150 
151  ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
152  {
153  return lhs != rhs;
154  }
155  };
156 
157  //***************************************************************************
158 
159  template <typename TArgumentType, typename TResultType>
161  {
162  typedef TArgumentType argument_type;
163  typedef TResultType result_type;
164  };
165 
166  //***************************************************************************
167 
168  template <typename TFirstArgumentType, typename TSecondArgumentType, typename TResultType>
170  {
171  typedef TFirstArgumentType first_argument_type;
172  typedef TSecondArgumentType second_argument_type;
173  typedef TResultType result_type;
174  };
175 
176  //***************************************************************************
177 
178  template <typename TFunction>
179  class binder1st : public etl::unary_function<typename TFunction::second_argument_type, typename TFunction::result_type>
180  {
181  protected:
182 
183  TFunction operation;
184  typename TFunction::first_argument_type value;
185 
186  public:
187 
188  binder1st(const TFunction& f, const typename TFunction::first_argument_type& v)
189  : operation(f), value(v)
190  {
191  }
192 
193  typename TFunction::result_type operator()(typename TFunction::second_argument_type& x) const
194  {
195  return operation(value, x);
196  }
197 
198  typename TFunction::result_type operator()(const typename TFunction::second_argument_type& x) const
199  {
200  return operation(value, x);
201  }
202  };
203 
204  template <typename F, typename T>
205  binder1st<F> bind1st(const F& f, const T& x)
206  {
207  return binder1st<F>(f, x);
208  }
209 
210  //***************************************************************************
211 
212  template <typename TFunction >
213  class binder2nd : public etl::unary_function<typename TFunction::first_argument_type, typename TFunction::result_type>
214  {
215  protected:
216  TFunction operation;
217  typename TFunction::second_argument_type value;
218  public:
219  binder2nd(const TFunction& f, const typename TFunction::second_argument_type& v)
220  : operation(f), value(v)
221  {
222  }
223 
224  typename TFunction::result_type operator()(typename TFunction::first_argument_type& x) const
225  {
226  return operation(x, value);
227  }
228 
229  typename TFunction::result_type operator()(const typename TFunction::first_argument_type& x) const
230  {
231  return operation(x, value);
232  }
233  };
234 
235  template <typename F, typename T>
236  binder2nd<F> bind2nd(const F& f, const T& x)
237  {
238  return binder2nd<F>(f, x);
239  }
240 }
241 
242 #endif
243 
Definition: functional.h:180
Definition: functional.h:214
Definition: functional.h:50
Definition: absolute.h:37
Definition: functional.h:170
Definition: functional.h:136
Definition: functional.h:124
Definition: functional.h:112
Definition: functional.h:148
Definition: functional.h:161