ChaiScript / ChaiScript
1
// This file is distributed under the BSD License.
2
// See "license.txt" for details.
3
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
5
// http://www.chaiscript.com
6

7
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9

10

11
#ifndef CHAISCRIPT_HANDLE_RETURN_HPP_
12
#define CHAISCRIPT_HANDLE_RETURN_HPP_
13

14
#include <functional>
15
#include <memory>
16
#include <type_traits>
17

18
#include "boxed_number.hpp"
19
#include "boxed_value.hpp"
20

21
namespace chaiscript {
22
class Boxed_Number;
23
}  // namespace chaiscript
24

25
namespace chaiscript
26
{
27
  namespace dispatch
28
  {
29
    template<class T, class U> class Proxy_Function_Callable_Impl;
30
    template<class T> class Assignable_Proxy_Function_Impl;
31

32
    namespace detail
33
    {
34
      /// Used internally for handling a return value from a Proxy_Function call
35
      template<typename Ret>
36
        struct Handle_Return
37
        {
38
          template<typename T,
39
                   typename = std::enable_if_t<std::is_pod_v<std::decay_t<T>>>>
40 1
          static Boxed_Value handle(T r)
41
          {
42 1
            return Boxed_Value(std::move(r), true);
43
          }
44

45
          template<typename T,
46
                   typename = std::enable_if_t<!std::is_pod_v<std::decay_t<T>>>>
47 1
          static Boxed_Value handle(T &&r)
48
          {
49 1
            return Boxed_Value(std::make_shared<T>(std::forward<T>(r)), true);
50
          }
51
        };
52

53
      template<typename Ret>
54
        struct Handle_Return<const std::function<Ret> &>
55
        {
56 0
          static Boxed_Value handle(const std::function<Ret> &f) {
57
            return Boxed_Value(
58
                chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
59 0
              );
60
          }
61
        };
62

63
      template<typename Ret>
64
        struct Handle_Return<std::function<Ret>> : Handle_Return<const std::function<Ret> &>
65
        {
66
        };
67

68
      template<typename Ret>
69
        struct Handle_Return<const std::shared_ptr<std::function<Ret>>>
70
        {
71
          static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
72
            return Boxed_Value(
73
                chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function_Impl<Ret>>(std::ref(*f),f)
74
                );
75
          }
76
        };
77

78
      template<typename Ret>
79
        struct Handle_Return<const std::shared_ptr<std::function<Ret>> &> : Handle_Return<const std::shared_ptr<std::function<Ret>>>
80
        {
81
        };
82

83
      template<typename Ret>
84
        struct Handle_Return<std::shared_ptr<std::function<Ret>>> : Handle_Return<const std::shared_ptr<std::function<Ret>>>
85
        {
86
        };
87

88
      template<typename Ret>
89
        struct Handle_Return<std::function<Ret> &>
90
        {
91 1
          static Boxed_Value handle(std::function<Ret> &f) {
92
            return Boxed_Value(
93 1
                chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function_Impl<Ret>>(std::ref(f),
94
                  std::shared_ptr<std::function<Ret>>())
95 1
              );
96
          }
97

98
          static Boxed_Value handle(const std::function<Ret> &f) {
99
            return Boxed_Value(
100
                chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
101
              );
102
          }
103
        };
104

105
      template<typename Ret>
106
        struct Handle_Return<Ret *&>
107
        {
108 1
          static Boxed_Value handle(Ret *p)
109
          {
110 1
            return Boxed_Value(p, true);
111
          }
112
        };
113

114
      template<typename Ret>
115
        struct Handle_Return<const Ret *&>
116
        {
117
          static Boxed_Value handle(const Ret *p)
118
          {
119
            return Boxed_Value(p, true);
120
          }
121
        };
122

123
      template<typename Ret>
124
        struct Handle_Return<Ret *>
125
        {
126 1
          static Boxed_Value handle(Ret *p)
127
          {
128 1
            return Boxed_Value(p, true);
129
          }
130
        };
131

132
      template<typename Ret>
133
        struct Handle_Return<const Ret *>
134
        {
135 1
          static Boxed_Value handle(const Ret *p)
136
          {
137 1
            return Boxed_Value(p, true);
138
          }
139
        };
140

141
      template<typename Ret>
142
        struct Handle_Return<std::shared_ptr<Ret> &>
143
        {
144 1
          static Boxed_Value handle(const std::shared_ptr<Ret> &r)
145
          {
146 1
            return Boxed_Value(r, true);
147
          }
148
        };
149

150
      template<typename Ret>
151
        struct Handle_Return<std::shared_ptr<Ret>> : Handle_Return<std::shared_ptr<Ret> &>
152
        {
153
        };
154

155
      template<typename Ret>
156
        struct Handle_Return<const std::shared_ptr<Ret> &> : Handle_Return<std::shared_ptr<Ret> &>
157
        {
158
        };
159

160

161
      template<typename Ret>
162
        struct Handle_Return<std::unique_ptr<Ret>> : Handle_Return<std::unique_ptr<Ret> &>
163
        {
164 1
          static Boxed_Value handle(std::unique_ptr<Ret> &&r)
165
          {
166 1
            return Boxed_Value(std::move(r), true);
167
          }
168
        };
169

170
      template<typename Ret, bool Ptr>
171
        struct Handle_Return_Ref
172
        {
173
          template<typename T>
174 1
          static Boxed_Value handle(T &&r)
175
          {
176 1
            return Boxed_Value(std::cref(r), true);
177
          }
178
        };
179

180
      template<typename Ret>
181
        struct Handle_Return_Ref<Ret, true>
182
        {
183
          template<typename T>
184 1
          static Boxed_Value handle(T &&r)
185
          {
186 1
            return Boxed_Value(typename std::remove_reference<decltype(r)>::type{r}, true);
187
          }
188
        };
189

190
      template<typename Ret>
191
        struct Handle_Return<const Ret &> : Handle_Return_Ref<const Ret &, std::is_pointer<typename std::remove_reference<const Ret &>::type>::value>
192
        {
193

194
        };
195

196

197
      template<typename Ret>
198
        struct Handle_Return<const Ret>
199
        {
200 1
          static Boxed_Value handle(Ret r)
201
          {
202 1
            return Boxed_Value(std::move(r));
203
          }
204
        };
205

206
      template<typename Ret>
207
        struct Handle_Return<Ret &>
208
        {
209 1
          static Boxed_Value handle(Ret &r)
210
          {
211 1
            return Boxed_Value(std::ref(r));
212
          }
213
        };
214

215
      template<>
216
        struct Handle_Return<Boxed_Value>
217
        {
218 1
          static Boxed_Value handle(const Boxed_Value &r) noexcept
219
          {
220 1
            return r;
221
          }
222
        };
223

224
      template<>
225
        struct Handle_Return<const Boxed_Value> : Handle_Return<Boxed_Value>
226
        {
227
        };
228

229
      template<>
230
        struct Handle_Return<Boxed_Value &> : Handle_Return<Boxed_Value>
231
        {
232
        };
233

234
      template<>
235
        struct Handle_Return<const Boxed_Value &> : Handle_Return<Boxed_Value>
236
        {
237
        };
238

239
      /**
240
       * Used internally for handling a return value from a Proxy_Function call
241
       */
242
      template<>
243
        struct Handle_Return<Boxed_Number>
244
        {
245 1
          static Boxed_Value handle(const Boxed_Number &r) noexcept
246
          {
247 1
            return r.bv;
248
          }
249
        };
250

251
      template<>
252
        struct Handle_Return<const Boxed_Number> : Handle_Return<Boxed_Number>
253
        {
254
        };
255

256

257
      /**
258
       * Used internally for handling a return value from a Proxy_Function call
259
       */
260
      template<>
261
        struct Handle_Return<void>
262
        {
263
          static Boxed_Value handle()
264
          {
265
            return void_var();
266
          }
267
        };
268
    }
269
  }
270
}
271

272
#endif

Read our documentation on viewing source code .

Loading