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
/// \file
12
/// This file contains utility functions for registration of STL container
13
/// classes. The methodology used is based on the SGI STL concepts.
14
/// http://www.sgi.com/tech/stl/table_of_contents.html
15

16

17
#ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_
18
#define CHAISCRIPT_BOOTSTRAP_STL_HPP_
19

20
#include <functional>
21
#include <memory>
22
#include <stdexcept>
23
#include <typeinfo>
24
#include <vector>
25

26
#include "bootstrap.hpp"
27
#include "boxed_value.hpp"
28
#include "dispatchkit.hpp"
29
#include "operators.hpp"
30
#include "proxy_constructors.hpp"
31
#include "register_function.hpp"
32
#include "type_info.hpp"
33

34
namespace chaiscript 
35
{
36
  namespace bootstrap
37
  {
38
    namespace standard_library
39
    {
40

41
      /// Bidir_Range, based on the D concept of ranges.
42
      /// \todo Update the Range code to base its capabilities on
43
      ///       the user_typetraits of the iterator passed in
44
      template<typename Container, typename IterType>
45
        struct Bidir_Range
46
        {
47
          using container_type = Container;
48

49 1
          constexpr Bidir_Range(Container &c)
50 1
            : m_begin(c.begin()), m_end(c.end())
51
          {
52
          }
53

54 1
          constexpr bool empty() const noexcept
55
          {
56 1
            return m_begin == m_end;
57
          }
58

59 1
          constexpr void pop_front()
60
          {
61 1
            if (empty())
62
            {
63 0
              throw std::range_error("Range empty");
64
            }
65 1
            ++m_begin;
66
          }
67

68 1
          constexpr void pop_back()
69
          {
70 1
            if (empty())
71
            {
72 0
              throw std::range_error("Range empty");
73
            }
74 1
            --m_end;
75
          }
76

77 1
          constexpr decltype(auto) front() const
78
          {
79 1
            if (empty())
80
            {
81 0
              throw std::range_error("Range empty");
82
            }
83 1
            return (*m_begin);
84
          }
85

86 1
          constexpr decltype(auto) back() const
87
          {
88 1
            if (empty())
89
            {
90 0
              throw std::range_error("Range empty");
91
            }
92 1
            auto pos = m_end;
93 1
            --pos;
94 1
            return (*(pos));
95
          }
96

97
          IterType m_begin;
98
          IterType m_end;
99
        };
100

101
      namespace detail {
102

103
        template<typename T>
104 1
        size_t count(const T &t_target, const typename T::key_type &t_key)
105
        {
106 1
          return t_target.count(t_key);
107
        }
108

109
        template<typename T>
110 1
          void insert(T &t_target, const T &t_other)
111
          {
112 1
            t_target.insert(t_other.begin(), t_other.end());
113
          }
114

115
        template<typename T>
116 1
          void insert_ref(T &t_target, const typename T::value_type &t_val)
117
          {
118 1
            t_target.insert(t_val);
119
          }
120

121

122

123
        /// Add Bidir_Range support for the given ContainerType
124
        template<typename Bidir_Type>
125 1
          void input_range_type_impl(const std::string &type, Module& m)
126
          {
127 1
            m.add(user_type<Bidir_Type>(), type + "_Range");
128

129 1
            copy_constructor<Bidir_Type>(type + "_Range", m);
130

131 1
            m.add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal");
132

133 1
            m.add(fun(&Bidir_Type::empty), "empty");
134 1
            m.add(fun(&Bidir_Type::pop_front), "pop_front");
135 1
            m.add(fun(&Bidir_Type::front), "front");
136 1
            m.add(fun(&Bidir_Type::pop_back), "pop_back");
137 1
            m.add(fun(&Bidir_Type::back), "back");
138
          }
139

140

141
        /// Algorithm for inserting at a specific position into a container
142
        template<typename Type>
143 1
          void insert_at(Type &container, int pos, const typename Type::value_type &v)
144
          {
145 1
            auto itr = container.begin();
146 1
            auto end = container.end();
147

148 1
            if (pos < 0 || std::distance(itr, end) < pos)
149
            {
150 1
              throw std::range_error("Cannot insert past end of range");
151
            }
152

153 1
            std::advance(itr, pos);
154 1
            container.insert(itr, v);
155
          }
156

157

158
        /// Algorithm for erasing a specific position from a container
159
        template<typename Type>
160 1
          void erase_at(Type &container, int pos)
161
          {
162 1
            auto itr = container.begin();
163 1
            auto end = container.end();
164

165 0
            if (pos < 0 || std::distance(itr, end) < (pos-1))
166
            {
167 0
              throw std::range_error("Cannot erase past end of range");
168
            }
169

170 1
            std::advance(itr, pos);
171 1
            container.erase(itr);
172
          }
173
      }
174

175
      template<typename ContainerType>
176 1
        void input_range_type(const std::string &type, Module& m)
177
        {
178 1
          detail::input_range_type_impl<Bidir_Range<ContainerType, typename ContainerType::iterator> >(type,m);
179 1
          detail::input_range_type_impl<Bidir_Range<const ContainerType, typename ContainerType::const_iterator> >("Const_" + type,m);
180
        }
181

182

183
      /// Add random_access_container concept to the given ContainerType
184
      /// http://www.sgi.com/tech/stl/RandomAccessContainer.html
185
      template<typename ContainerType>
186 1
        void random_access_container_type(const std::string &/*type*/, Module& m)
187
        {
188
          //In the interest of runtime safety for the m, we prefer the at() method for [] access,
189
          //to throw an exception in an out of bounds condition.
190 1
          m.add(
191
              fun(
192
                [](ContainerType &c, int index) -> typename ContainerType::reference {
193
                  /// \todo we are preferring to keep the key as 'int' to avoid runtime conversions
194
                  /// during dispatch. reevaluate
195
                  return c.at(static_cast<typename ContainerType::size_type>(index));
196
                }), "[]");
197

198 1
          m.add(
199
              fun(
200
                [](const ContainerType &c, int index) -> typename ContainerType::const_reference {
201
                  /// \todo we are preferring to keep the key as 'int' to avoid runtime conversions
202
                  /// during dispatch. reevaluate
203
                  return c.at(static_cast<typename ContainerType::size_type>(index));
204
                }), "[]");
205
        }
206

207

208
      /// Add assignable concept to the given ContainerType
209
      /// http://www.sgi.com/tech/stl/Assignable.html
210
      template<typename ContainerType>
211 1
        void assignable_type(const std::string &type, Module& m)
212
        {
213 1
          copy_constructor<ContainerType>(type, m);
214 1
          operators::assign<ContainerType>(m);
215
        }
216

217
      /// Add container resize concept to the given ContainerType
218
      /// http://www.cplusplus.com/reference/stl/
219
      template<typename ContainerType>
220 1
        void resizable_type(const std::string &/*type*/, Module& m)
221
        {
222 1
          m.add(fun([](ContainerType *a, typename ContainerType::size_type n, const typename ContainerType::value_type& val) { return a->resize(n, val); } ), "resize");
223 1
          m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->resize(n); } ), "resize");
224
        }
225

226
      /// Add container reserve concept to the given ContainerType
227
      /// http://www.cplusplus.com/reference/stl/
228
      template<typename ContainerType>
229 1
        void reservable_type(const std::string &/*type*/, Module& m)
230
        {
231 1
          m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->reserve(n); } ), "reserve");
232 1
          m.add(fun([](const ContainerType *a) { return a->capacity(); } ), "capacity");
233
        }
234

235
      /// Add container concept to the given ContainerType
236
      /// http://www.sgi.com/tech/stl/Container.html
237
      template<typename ContainerType>
238 1
        void container_type(const std::string &/*type*/, Module& m)
239
        {
240 1
          m.add(fun([](const ContainerType *a) { return a->size(); } ), "size");
241 1
          m.add(fun([](const ContainerType *a) { return a->empty(); } ), "empty");
242 1
          m.add(fun([](ContainerType *a) { a->clear(); } ), "clear");
243
        }
244

245
      /// Add default constructable concept to the given Type
246
      /// http://www.sgi.com/tech/stl/DefaultConstructible.html
247
      template<typename Type>
248 1
        void default_constructible_type(const std::string &type, Module& m)
249
        {
250 1
          m.add(constructor<Type ()>(), type);
251
        }
252

253
      /// Add sequence concept to the given ContainerType
254
      /// http://www.sgi.com/tech/stl/Sequence.html
255
      template<typename ContainerType>
256 1
        void sequence_type(const std::string &/*type*/, Module& m)
257
        {
258 1
          m.add(fun(&detail::insert_at<ContainerType>),
259 1
              []()->std::string{
260 1
                if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
261
                  return "insert_ref_at";
262
                } else {
263
                  return "insert_at";
264
                }
265
              }());
266

267 1
          m.add(fun(&detail::erase_at<ContainerType>), "erase_at");
268
        }
269

270
      /// Add back insertion sequence concept to the given ContainerType
271
      /// http://www.sgi.com/tech/stl/BackInsertionSequence.html
272
      template<typename ContainerType>
273 1
        void back_insertion_sequence_type(const std::string &type, Module& m)
274
        {
275 1
          m.add(fun([](ContainerType &container)->decltype(auto){ 
276
                      if (container.empty()) {
277 0
                        throw std::range_error("Container empty");
278
                      } else {
279
                        return (container.back());
280
                      }
281
                    }
282
                  )
283
                , "back");
284 1
          m.add(fun([](const ContainerType &container)->decltype(auto){ 
285
                      if (container.empty()) {
286
                        throw std::range_error("Container empty");
287
                      } else {
288 0
                        return (container.back());
289
                      }
290
                    }
291
                  )
292
                , "back");
293

294

295
          using push_back = void (ContainerType::*)(const typename ContainerType::value_type &);
296 1
          m.add(fun(static_cast<push_back>(&ContainerType::push_back)),
297 1
              [&]()->std::string{
298 1
              if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
299
                m.eval(
300
                    "# Pushes the second value onto the container while making a clone of the value\n"
301
                    "def push_back(" + type + " container, x)\n"
302
                    "{ \n"
303
                    "  if (x.is_var_return_value()) {\n"
304
                    "    x.reset_var_return_value() \n"
305
                    "    container.push_back_ref(x) \n"
306
                    "  } else { \n"
307
                    "    container.push_back_ref(clone(x)); \n"
308
                    "  }\n"
309
                    "} \n"
310
                    );
311

312
                  return "push_back_ref";
313
                } else {
314
                  return "push_back";
315
                }
316
              }());
317

318 1
          m.add(fun(&ContainerType::pop_back), "pop_back");
319
        }
320

321

322
      /// Front insertion sequence
323
      /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
324
      template<typename ContainerType>
325 1
        void front_insertion_sequence_type(const std::string &type, Module& m)
326
        {
327
          using push_ptr = void (ContainerType::*)(typename ContainerType::const_reference);
328
          using pop_ptr = void (ContainerType::*)();
329

330 1
          m.add(fun([](ContainerType &container)->decltype(auto){ 
331 1
                      if (container.empty()) {
332 0
                        throw std::range_error("Container empty");
333
                      } else {
334 1
                        return (container.front());
335
                      }
336
                    }
337
                  )
338
                , "front");
339

340
          m.add(fun([](const ContainerType &container)->decltype(auto){ 
341 0
                      if (container.empty()) {
342 0
                        throw std::range_error("Container empty");
343
                      } else {
344 0
                        return (container.front());
345
                      }
346
                    }
347
                  )
348
                , "front");
349

350

351 1
          m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
352 1
              [&]()->std::string{
353 1
                if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
354 1
                  m.eval(
355
                      "# Pushes the second value onto the front of container while making a clone of the value\n"
356
                      "def push_front(" + type + " container, x)\n"
357
                      "{ \n"
358
                      "  if (x.is_var_return_value()) {\n"
359
                      "    x.reset_var_return_value() \n"
360
                      "    container.push_front_ref(x) \n"
361
                      "  } else { \n"
362
                      "    container.push_front_ref(clone(x)); \n"
363
                      "  }\n"
364
                      "} \n"
365
                      );
366 1
                  return "push_front_ref";
367
                } else {
368 0
                  return "push_front";
369
                }
370 1
              }());
371

372 1
          m.add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front");
373
        }
374

375
      /// bootstrap a given PairType
376
      /// http://www.sgi.com/tech/stl/pair.html
377
      template<typename PairType>
378 1
        void pair_type(const std::string &type, Module& m)
379
        {
380 1
          m.add(user_type<PairType>(), type);
381

382 1
          m.add(fun(&PairType::first), "first");
383 1
          m.add(fun(&PairType::second), "second");
384

385 1
          basic_constructors<PairType>(type, m);
386 1
          m.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
387
        }
388

389

390
      /// Add pair associative container concept to the given ContainerType
391
      /// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
392

393
      template<typename ContainerType>
394 1
        void pair_associative_container_type(const std::string &type, Module& m)
395
        {
396 1
          pair_type<typename ContainerType::value_type>(type + "_Pair", m);
397
        }
398

399
      /// Add unique associative container concept to the given ContainerType
400
      /// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
401
      template<typename ContainerType>
402 1
        void unique_associative_container_type(const std::string &/*type*/, Module& m)
403
        {
404 1
          m.add(fun(detail::count<ContainerType>), "count");
405

406
          using erase_ptr = size_t (ContainerType::*)(const typename ContainerType::key_type &);
407

408 1
          m.add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase");
409

410 1
          m.add(fun(&detail::insert<ContainerType>), "insert");
411

412 1
          m.add(fun(&detail::insert_ref<ContainerType>),
413 1
              []()->std::string{
414 1
                if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
415 1
                  return "insert_ref";
416
                } else {
417 0
                  return "insert";
418
                }
419
              }());
420
        }
421

422
      /// Add a MapType container
423
      /// http://www.sgi.com/tech/stl/Map.html
424
      template<typename MapType>
425 1
        void map_type(const std::string &type, Module& m)
426
        {
427 1
          m.add(user_type<MapType>(), type);
428

429
          using elem_access = typename MapType::mapped_type &(MapType::*)(const typename MapType::key_type &);
430
          using const_elem_access = const typename MapType::mapped_type &(MapType::*)(const typename MapType::key_type &) const;
431

432 1
          m.add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
433

434 1
          m.add(fun(static_cast<elem_access>(&MapType::at)), "at");
435 1
          m.add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
436

437 1
          if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>))
438
          {
439 1
            m.eval(R"(
440
                    def Map::`==`(Map rhs) {
441
                       if ( rhs.size() != this.size() ) {
442
                         return false;
443
                       } else {
444
                         auto r1 = range(this);
445
                         auto r2 = range(rhs);
446
                         while (!r1.empty())
447
                         {
448
                           if (!eq(r1.front().first, r2.front().first) || !eq(r1.front().second, r2.front().second))
449
                           {
450
                             return false;
451
                           }
452
                           r1.pop_front();
453
                           r2.pop_front();
454
                         }
455
                         true;
456
                       }
457
                   } )"
458
                 );
459
          } 
460

461 1
          container_type<MapType>(type, m);
462 1
          default_constructible_type<MapType>(type, m);
463 1
          assignable_type<MapType>(type, m);
464 1
          unique_associative_container_type<MapType>(type, m);
465 1
          pair_associative_container_type<MapType>(type, m);
466 1
          input_range_type<MapType>(type, m);
467
        }
468

469
      /// http://www.sgi.com/tech/stl/List.html
470
      template<typename ListType>
471 1
        void list_type(const std::string &type, Module& m)
472
        {
473 1
          m.add(user_type<ListType>(), type);
474

475 1
          front_insertion_sequence_type<ListType>(type, m);
476 1
          back_insertion_sequence_type<ListType>(type, m);
477 1
          sequence_type<ListType>(type, m);
478 1
          resizable_type<ListType>(type, m);
479 1
          container_type<ListType>(type, m);
480 1
          default_constructible_type<ListType>(type, m);
481 1
          assignable_type<ListType>(type, m);
482 1
          input_range_type<ListType>(type, m);
483
        }
484

485
      /// Create a vector type with associated concepts
486
      /// http://www.sgi.com/tech/stl/Vector.html
487
      template<typename VectorType>
488 1
        void vector_type(const std::string &type, Module& m)
489
        {
490 1
          m.add(user_type<VectorType>(), type);
491

492 1
          m.add(fun([](VectorType &container)->decltype(auto){ 
493
                      if (container.empty()) {
494 0
                        throw std::range_error("Container empty");
495
                      } else {
496
                        return (container.front());
497
                      }
498
                    }
499
                  )
500
                , "front");
501

502 1
          m.add(fun([](const VectorType &container)->decltype(auto){ 
503 0
                      if (container.empty()) {
504 0
                        throw std::range_error("Container empty");
505
                      } else {
506 0
                        return (container.front());
507
                      }
508
                    }
509
                  )
510
                , "front");
511

512

513

514

515 1
          back_insertion_sequence_type<VectorType>(type, m);
516 1
          sequence_type<VectorType>(type, m);
517 1
          random_access_container_type<VectorType>(type, m);
518 1
          resizable_type<VectorType>(type, m);
519 1
          reservable_type<VectorType>(type, m);
520 1
          container_type<VectorType>(type, m);
521 1
          default_constructible_type<VectorType>(type, m);
522 1
          assignable_type<VectorType>(type, m);
523 1
          input_range_type<VectorType>(type, m);
524

525 1
          if (typeid(VectorType) == typeid(std::vector<Boxed_Value>))
526
          {
527 1
            m.eval(R"(
528
                    def Vector::`==`(Vector rhs) {
529
                       if ( rhs.size() != this.size() ) {
530
                         return false;
531
                       } else {
532
                         auto r1 = range(this);
533
                         auto r2 = range(rhs);
534
                         while (!r1.empty())
535
                         {
536
                           if (!eq(r1.front(), r2.front()))
537
                           {
538
                             return false;
539
                           }
540
                           r1.pop_front();
541
                           r2.pop_front();
542
                         }
543
                         true;
544
                       }
545
                   } )"
546
                 );
547
          } 
548
        }
549

550
      /// Add a String container
551
      /// http://www.sgi.com/tech/stl/basic_string.html
552
      template<typename String>
553 1
        void string_type(const std::string &type, Module& m)
554
        {
555 1
          m.add(user_type<String>(), type);
556 1
          operators::addition<String>(m);
557 1
          operators::assign_sum<String>(m);
558 1
          opers_comparison<String>(m);
559 1
          random_access_container_type<String>(type, m);
560 1
          sequence_type<String>(type, m);
561 1
          default_constructible_type<String>(type, m);
562
          // container_type<String>(type, m);
563 1
          assignable_type<String>(type, m);
564 1
          input_range_type<String>(type, m);
565

566
          //Special case: add push_back to string (which doesn't support other back_insertion operations
567 1
          m.add(fun(&String::push_back),
568 1
              []()->std::string{
569 1
                if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
570 0
                  return "push_back_ref";
571
                } else {
572 1
                  return "push_back";
573
                }
574
              }());
575

576

577 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find");
578 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind");
579 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of");
580 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of");
581 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
582 1
          m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
583
        
584 1
          m.add(fun([](String *s, typename String::value_type c) -> decltype(auto) { return (*s += c); } ), "+=");
585

586 1
          m.add(fun([](String *s) { s->clear(); } ), "clear");
587 1
          m.add(fun([](const String *s) { return s->empty(); } ), "empty");
588 1
          m.add(fun([](const String *s) { return s->size(); } ), "size");
589

590 1
          m.add(fun([](const String *s) { return s->c_str(); } ), "c_str");
591 1
          m.add(fun([](const String *s) { return s->data(); } ), "data");
592 1
          m.add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
593
        }
594

595

596
      /// Add a MapType container
597
      /// http://www.sgi.com/tech/stl/Map.html
598
      template<typename FutureType>
599 1
        void future_type(const std::string &type, Module& m)
600
        {
601 1
          m.add(user_type<FutureType>(), type);
602

603 1
          m.add(fun([](const FutureType &t) { return t.valid(); }), "valid");
604 1
          m.add(fun([](FutureType &t) { return t.get(); }), "get");
605 1
          m.add(fun(&FutureType::wait), "wait");
606
        }
607
    }
608
  }
609
}
610

611

612
#endif
613

614

Read our documentation on viewing source code .

Loading