about summary refs log tree commit diff
path: root/std_string_view.i
blob: 585281f80268be394ed26bd6c1b440a8963c3506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* -----------------------------------------------------------------------------
 * std_string_view.i
 *
 * std::string_view typemaps for Guile
 * ----------------------------------------------------------------------------- */

%{
#include <string>
#include <string_view>
%}

namespace std {

  %naturalvar string_view;

  %typemap(in) string_view (std::string temp) {
    char *ptr = scm_to_stringn($input, NULL, "utf8",
                               SCM_FAILED_CONVERSION_ERROR);
    temp.assign(ptr);
    SWIG_free(ptr);
    $1 = std::string_view(temp);
  }

  %typemap(out) string_view %{
    $result = scm_from_stringn($1.data(), $1.size(), "utf8",
                               SCM_FAILED_CONVERSION_ERROR);
    %}

  %typemap(in) const string_view& (std::string temp, $*1_ltype view) {
    char *ptr = scm_to_stringn($input, NULL, "utf8",
                               SCM_FAILED_CONVERSION_ERROR);
    temp.assign(ptr);
    SWIG_free(ptr);
    view = std::string_view(temp);
    $1 = &view;
  }

  %typemap(out) const string_view& %{
    $result = scm_from_stringn($1->data(), $1->size(), "utf8",
                               SCM_FAILED_CONVERSION_ERROR);
    %}

  %typemap(varout) string_view %{
    $result = scm_from_stringn($1.data(), $1.size(), "utf8",
                               SCM_FAILED_CONVERSION_ERROR);
    %}

  // for throwing of any kind of string_view, string_view ref's and
  // string_view pointers we convert all to Guile strings
  %typemap(throws) string_view, string_view&, const string_view& %{
    scm_throw(scm_from_locale_symbol("swig-exception"),
              scm_list_1(scm_from_stringn($1.data(), $1.size(), "utf8",
                                          SCM_FAILED_CONVERSION_ERROR)));
    %}

  %typemap(throws) string_view*, const string_view* %{
    scm_throw(scm_from_locale_symbol("swig-exception"),
              scm_list_1(scm_from_stringn($1->data(), $1->size(), "utf8",
                                          SCM_FAILED_CONVERSION_ERROR)));
    %}

  %typemap(typecheck,precedence=SWIG_TYPECHECK_STRINGVIEW) string_view, const string_view& {
    $1 = scm_is_string($input);
  }

  class string_view;

}