Nugget
function.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
5 #ifndef EASTL_FUNCTION_H
6 #define EASTL_FUNCTION_H
7 
8 #include <EASTL/internal/config.h>
9 
10 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
11  #pragma once
12 #endif
13 
14 #include <EASTL/internal/function_detail.h>
15 
16 namespace eastl
17 {
18 
23  #ifndef EASTL_FUNCTION_DEFAULT_CAPTURE_SSO_SIZE
24  #define EASTL_FUNCTION_DEFAULT_CAPTURE_SSO_SIZE (2 * sizeof(void*))
25  #endif
26 
27  static_assert(EASTL_FUNCTION_DEFAULT_CAPTURE_SSO_SIZE >= sizeof(void*), "functor storage must be able to hold at least a pointer!");
28 
29  template <typename>
30  class function;
31 
32  template <typename R, typename... Args>
33  class function<R(Args...)> : public internal::function_detail<EASTL_FUNCTION_DEFAULT_CAPTURE_SSO_SIZE, R(Args...)>
34  {
35  private:
36  using Base = internal::function_detail<EASTL_FUNCTION_DEFAULT_CAPTURE_SSO_SIZE, R(Args...)>;
37  public:
38  using typename Base::result_type;
39 
40  function() EA_NOEXCEPT = default;
41  function(std::nullptr_t p) EA_NOEXCEPT
42  : Base(p)
43  {
44  }
45 
46  function(const function& other)
47  : Base(other)
48  {
49  }
50 
51  function(function&& other)
52  : Base(eastl::move(other))
53  {
54  }
55 
56  template <typename Functor, typename = EASTL_INTERNAL_FUNCTION_VALID_FUNCTION_ARGS(Functor, R, Args..., Base, function)>
57  function(Functor functor)
58  : Base(eastl::move(functor))
59  {
60  }
61 
62  ~function() EA_NOEXCEPT = default;
63 
64  function& operator=(const function& other)
65  {
66  Base::operator=(other);
67  return *this;
68  }
69 
70  function& operator=(function&& other)
71  {
72  Base::operator=(eastl::move(other));
73  return *this;
74  }
75 
76  function& operator=(std::nullptr_t p) EA_NOEXCEPT
77  {
78  Base::operator=(p);
79  return *this;
80  }
81 
82  template <typename Functor, typename = EASTL_INTERNAL_FUNCTION_VALID_FUNCTION_ARGS(Functor, R, Args..., Base, function)>
83  function& operator=(Functor&& functor)
84  {
85  Base::operator=(eastl::forward<Functor>(functor));
86  return *this;
87  }
88 
89  template <typename Functor>
90  function& operator=(eastl::reference_wrapper<Functor> f) EA_NOEXCEPT
91  {
92  Base::operator=(f);
93  return *this;
94  }
95 
96  void swap(function& other) EA_NOEXCEPT
97  {
98  Base::swap(other);
99  }
100 
101  explicit operator bool() const EA_NOEXCEPT
102  {
103  return Base::operator bool();
104  }
105 
106  R operator ()(Args... args) const
107  {
108  return Base::operator ()(eastl::forward<Args>(args)...);
109  }
110 
111  #if EASTL_RTTI_ENABLED
112  const std::type_info& target_type() const EA_NOEXCEPT
113  {
114  return Base::target_type();
115  }
116 
117  template <typename Functor>
118  Functor* target() EA_NOEXCEPT
119  {
120  return Base::target();
121  }
122 
123  template <typename Functor>
124  const Functor* target() const EA_NOEXCEPT
125  {
126  return Base::target();
127  }
128  #endif // EASTL_RTTI_ENABLED
129  };
130 
131  template <typename R, typename... Args>
132  bool operator==(const function<R(Args...)>& f, std::nullptr_t) EA_NOEXCEPT
133  {
134  return !f;
135  }
136 
137  template <typename R, typename... Args>
138  bool operator==(std::nullptr_t, const function<R(Args...)>& f) EA_NOEXCEPT
139  {
140  return !f;
141  }
142 
143  template <typename R, typename... Args>
144  bool operator!=(const function<R(Args...)>& f, std::nullptr_t) EA_NOEXCEPT
145  {
146  return !!f;
147  }
148 
149  template <typename R, typename... Args>
150  bool operator!=(std::nullptr_t, const function<R(Args...)>& f) EA_NOEXCEPT
151  {
152  return !!f;
153  }
154 
155  template <typename R, typename... Args>
156  void swap(function<R(Args...)>& lhs, function<R(Args...)>& rhs)
157  {
158  lhs.swap(rhs);
159  }
160 
161 } // namespace eastl
162 
163 #endif // EASTL_FUNCTION_H
Definition: function.h:30
Definition: function_detail.h:416
reference_wrapper
Definition: functional_base.h:221
EA Standard Template Library.
Definition: algorithm.h:288
OutputIterator move(InputIterator first, InputIterator last, OutputIterator result)
Definition: copy_help.h:170