diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2023-12-02 01:45:21 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2023-12-02 02:42:50 +0300 |
commit | 9c43d58f75cf086b744cf4fe2ae180e8f37e4a0c (patch) | |
tree | 9f88a486917d371d099cd712efd91b4c122d209d /contrib/tools/swig/Lib/go | |
parent | 32fb6dda1feb24f9ab69ece5df0cb9ec238ca5e6 (diff) | |
download | ydb-9c43d58f75cf086b744cf4fe2ae180e8f37e4a0c.tar.gz |
Intermediate changes
Diffstat (limited to 'contrib/tools/swig/Lib/go')
-rw-r--r-- | contrib/tools/swig/Lib/go/exception.i | 7 | ||||
-rw-r--r-- | contrib/tools/swig/Lib/go/std_common.i | 4 | ||||
-rw-r--r-- | contrib/tools/swig/Lib/go/std_except.i | 31 | ||||
-rw-r--r-- | contrib/tools/swig/Lib/go/std_string.i | 162 | ||||
-rw-r--r-- | contrib/tools/swig/Lib/go/std_vector.i | 92 |
5 files changed, 296 insertions, 0 deletions
diff --git a/contrib/tools/swig/Lib/go/exception.i b/contrib/tools/swig/Lib/go/exception.i new file mode 100644 index 0000000000..5abd306a4e --- /dev/null +++ b/contrib/tools/swig/Lib/go/exception.i @@ -0,0 +1,7 @@ +%typemap(throws,noblock=1) (...) { + SWIG_exception(SWIG_RuntimeError,"unknown exception"); +} + +%insert("runtime") %{ +#define SWIG_exception(code, msg) _swig_gopanic(msg) +%} diff --git a/contrib/tools/swig/Lib/go/std_common.i b/contrib/tools/swig/Lib/go/std_common.i new file mode 100644 index 0000000000..c010facacd --- /dev/null +++ b/contrib/tools/swig/Lib/go/std_common.i @@ -0,0 +1,4 @@ +%include <std_except.i> + +%apply size_t { std::size_t }; +%apply const size_t& { const std::size_t& }; diff --git a/contrib/tools/swig/Lib/go/std_except.i b/contrib/tools/swig/Lib/go/std_except.i new file mode 100644 index 0000000000..4f021a1264 --- /dev/null +++ b/contrib/tools/swig/Lib/go/std_except.i @@ -0,0 +1,31 @@ +/* ----------------------------------------------------------------------------- + * std_except.i + * + * Typemaps used by the STL wrappers that throw exceptions. + * These typemaps are used when methods are declared with an STL exception specification, such as + * size_t at() const throw (std::out_of_range); + * ----------------------------------------------------------------------------- */ + +%{ +#include <typeinfo> +#include <stdexcept> +%} + +namespace std +{ + %ignore exception; + struct exception {}; +} + +%typemap(throws) std::bad_cast %{_swig_gopanic($1.what());%} +%typemap(throws) std::bad_exception %{_swig_gopanic($1.what());%} +%typemap(throws) std::domain_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::exception %{_swig_gopanic($1.what());%} +%typemap(throws) std::invalid_argument %{_swig_gopanic($1.what());%} +%typemap(throws) std::length_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::logic_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::out_of_range %{_swig_gopanic($1.what());%} +%typemap(throws) std::overflow_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::range_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::runtime_error %{_swig_gopanic($1.what());%} +%typemap(throws) std::underflow_error %{_swig_gopanic($1.what());%} diff --git a/contrib/tools/swig/Lib/go/std_string.i b/contrib/tools/swig/Lib/go/std_string.i new file mode 100644 index 0000000000..35b4a5e46b --- /dev/null +++ b/contrib/tools/swig/Lib/go/std_string.i @@ -0,0 +1,162 @@ +/* ----------------------------------------------------------------------------- + * std_string.i + * + * Typemaps for std::string and const std::string& + * These are mapped to a Go string and are passed around by value. + * + * To use non-const std::string references use the following %apply. Note + * that they are passed by value. + * %apply const std::string & {std::string &}; + * ----------------------------------------------------------------------------- */ + +%{ +#include <string> +%} + +namespace std { + +%naturalvar string; + +class string; + +%typemap(gotype) string, const string & "string" + +%typemap(in) string +%{ $1.assign($input.p, $input.n); %} + +%typemap(godirectorout) string +%{ + { + p := Swig_malloc(len($input)) + s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)] + copy(s, $input) + $result = *(*string)(unsafe.Pointer(&s)) + } +%} + +%typemap(directorout) string +%{ + $result.assign($input.p, $input.n); + free($input.p); +%} + +%typemap(out,fragment="AllocateString") string +%{ $result = Swig_AllocateString($1.data(), $1.length()); %} + +%typemap(goout,fragment="CopyString") string +%{ $result = swigCopyString($1) %} + +%typemap(directorin,fragment="AllocateString") string +%{ $input = Swig_AllocateString($1.data(), $1.length()); %} + +%typemap(godirectorin,fragment="CopyString") string +%{ $result = swigCopyString($input) %} + +%typemap(throws) string +%{ _swig_gopanic($1.c_str()); %} + +%typemap(in) const string & +%{ + $*1_ltype $1_str($input.p, $input.n); + $1 = &$1_str; +%} + +%typemap(godirectorout) const string & +%{ + { + p := Swig_malloc(len($input)) + s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)] + copy(s, $input) + $result = *(*string)(unsafe.Pointer(&s)) + } +%} + +%typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string & +%{ + static $*1_ltype $1_str; + $1_str.assign($input.p, $input.n); + free($input.p); + $result = &$1_str; +%} + +%typemap(out,fragment="AllocateString") const string & +%{ $result = Swig_AllocateString((*$1).data(), (*$1).length()); %} + +%typemap(goout,fragment="CopyString") const string & +%{ $result = swigCopyString($1) %} + +%typemap(directorin,fragment="AllocateString") const string & +%{ $input = Swig_AllocateString($1.data(), $1.length()); %} + +%typemap(godirectorin,fragment="CopyString") const string & +%{ $result = swigCopyString($input) %} + +%typemap(throws) const string & +%{ _swig_gopanic($1.c_str()); %} + + +%typemap(gotype) string * "*string" + +%typemap(in) string * (string temp) +%{ + if ($input) { + temp.assign($input->p, $input->n); + $1 = &temp; + } else + $1 = 0; +%} + +%typemap(godirectorout) string * +%{ + if $input != nil { + p := Swig_malloc(len(*$input)) + s := (*[1<<30]byte)(unsafe.Pointer(p))[:len(*$input)] + copy(s, *$input) + $result = (*string)(unsafe.Pointer(&s)) + } else { + $result = nil + } +%} + +%typemap(directorout) string * (string temp) +%{ + temp.assign($input->p, $input->n); + $result = &temp; + free($input.p); +%} + +%typemap(out,fragment="AllocateString") string * (_gostring_ temp) +%{ + temp = Swig_AllocateString($1->data(), $1->length()); + $result = &temp; +%} + +%typemap(goout,fragment="CopyString") string * +%{ *$result = swigCopyString(*$1) %} + +%typemap(directorin,fragment="AllocateString") string * (_gostring_ temp) +%{ + if ($1) { + temp = Swig_AllocateString($1->data(), $1->length()); + $input = &temp; + } else + $input = 0; +%} + +%typemap(godirectorin,fragment="CopyString") string * +%{ *$result = swigCopyString(*$input); %} + +%typemap(argout,fragment="AllocateString") string * +%{ + if ($1) + *$input = Swig_AllocateString($1->data(), $1->length()); +%} + +%typemap(goargout,fragment="CopyString") string * +%{ + if $input != nil { + *$1 = swigCopyString(*$input) + } +%} + +} diff --git a/contrib/tools/swig/Lib/go/std_vector.i b/contrib/tools/swig/Lib/go/std_vector.i new file mode 100644 index 0000000000..679c707596 --- /dev/null +++ b/contrib/tools/swig/Lib/go/std_vector.i @@ -0,0 +1,92 @@ +/* ----------------------------------------------------------------------------- + * std_vector.i + * ----------------------------------------------------------------------------- */ + +%{ +#include <vector> +#include <stdexcept> +%} + +namespace std { + + template<class T> class vector { + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + + vector(); + vector(size_type n); + vector(const vector& other); + + size_type size() const; + size_type capacity() const; + void reserve(size_type n); + %rename(isEmpty) empty; + bool empty() const; + void clear(); + %rename(add) push_back; + void push_back(const value_type& x); + %extend { + const_reference get(int i) throw (std::out_of_range) { + int size = int(self->size()); + if (i>=0 && i<size) + return (*self)[i]; + else + throw std::out_of_range("vector index out of range"); + } + void set(int i, const value_type& val) throw (std::out_of_range) { + int size = int(self->size()); + if (i>=0 && i<size) + (*self)[i] = val; + else + throw std::out_of_range("vector index out of range"); + } + } + }; + + // bool specialization + template<> class vector<bool> { + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef bool value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef bool const_reference; + + vector(); + vector(size_type n); + vector(const vector& other); + + size_type size() const; + size_type capacity() const; + void reserve(size_type n); + %rename(isEmpty) empty; + bool empty() const; + void clear(); + %rename(add) push_back; + void push_back(const value_type& x); + %extend { + bool get(int i) throw (std::out_of_range) { + int size = int(self->size()); + if (i>=0 && i<size) + return (*self)[i]; + else + throw std::out_of_range("vector index out of range"); + } + void set(int i, const value_type& val) throw (std::out_of_range) { + int size = int(self->size()); + if (i>=0 && i<size) + (*self)[i] = val; + else + throw std::out_of_range("vector index out of range"); + } + } + }; +} |