|
| 1 | +/*! |
| 2 | + * \file flc_string.i |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC. |
| 5 | + * Distributed under an MIT open source license: see LICENSE for details. |
| 6 | + */ |
| 7 | + |
| 8 | +%module "flc_string" |
| 9 | +%include "import_flc.i" |
| 10 | + |
| 11 | +// SWIG always represents std::string as native strings. We load its typemaps |
| 12 | +// but will explicitly create the class. |
| 13 | +%include <std_string.i> |
| 14 | + |
| 15 | +// Include typemaps for integer offsets and native integer types |
| 16 | +%include <std_common.i> |
| 17 | + |
| 18 | +// Typemap to convert positions from npos -> 0 and 1-offset otherwise |
| 19 | +%apply int FORTRAN_INT { std::size_t POSITION }; |
| 20 | +%typemap(out, noblock=1) std::size_t POSITION { |
| 21 | + $result = ($1 == std::string::npos ? 0 : $1 + 1); |
| 22 | +} |
| 23 | + |
| 24 | +namespace std { |
| 25 | +class string { |
| 26 | + public: |
| 27 | + // >>> TYPES |
| 28 | + typedef size_t size_type; |
| 29 | + typedef ptrdiff_t difference_type; |
| 30 | + typedef char value_type; |
| 31 | + typedef const char& const_reference; |
| 32 | + |
| 33 | + // Typemaps for making std::vector feel more like native Fortran: |
| 34 | + // - Use Fortran 1-offset indexing |
| 35 | + %apply int FORTRAN_INDEX {size_type pos, |
| 36 | + size_type index, |
| 37 | + size_type start_index, |
| 38 | + size_type stop_index}; |
| 39 | + // - Use native Fortran integers in proxy code |
| 40 | + %apply int FORTRAN_INT {size_type}; |
| 41 | + |
| 42 | + // - Use fortran indexing (and 0 for not found) for search |
| 43 | + %apply std::size_t POSITION {size_type find}; |
| 44 | + |
| 45 | + // - Allow access as an array view |
| 46 | + %apply SWIGTYPE& { string& view }; |
| 47 | + %fortran_array_pointer(char, string& view); |
| 48 | + %typemap(out, noblock=1) string& view { |
| 49 | + $result.data = ($1->empty() ? NULL : const_cast<char*>($1->data())); |
| 50 | + $result.size = $1->size(); |
| 51 | + } |
| 52 | + |
| 53 | + // - Allow interaction with other string objects |
| 54 | + %apply SWIGTYPE& {const string& OTHER}; |
| 55 | + |
| 56 | + public: |
| 57 | + // >>> MEMBER FUNCTIONS |
| 58 | + |
| 59 | + // TODO: add more constructors |
| 60 | + string(); |
| 61 | + string(size_type count, value_type ch); |
| 62 | + string(const std::string& s); |
| 63 | + |
| 64 | + // Accessors |
| 65 | + size_type size() const; |
| 66 | + bool empty() const; |
| 67 | + |
| 68 | + const_reference front() const; |
| 69 | + const_reference back() const; |
| 70 | + |
| 71 | + // Modify |
| 72 | + void resize(size_type count); |
| 73 | + void resize(size_type count, value_type v); |
| 74 | + void assign(const string& s); |
| 75 | + void push_back(value_type v); |
| 76 | + void pop_back(); |
| 77 | + void clear(); |
| 78 | + |
| 79 | + // String operations |
| 80 | + size_type find(const string& s, size_type pos = 0); |
| 81 | + void append(const string& s); |
| 82 | + int compare(const string& OTHER); |
| 83 | + |
| 84 | + // >>> EXTENSIONS |
| 85 | + // (TODO: add the same erase/insert extensions as std::vector) |
| 86 | + |
| 87 | + %extend { |
| 88 | + %fragment("SWIG_check_range"); |
| 89 | + |
| 90 | + void set(size_type index, value_type v) { |
| 91 | + SWIG_check_range(index, $self->size(), |
| 92 | + "std::string::set", |
| 93 | + return); |
| 94 | + (*$self)[index] = v; |
| 95 | + } |
| 96 | + |
| 97 | + value_type get(size_type index) { |
| 98 | + SWIG_check_range(index, $self->size(), |
| 99 | + "std::string::get", |
| 100 | + return $self->front()); |
| 101 | + return (*$self)[index]; |
| 102 | + } |
| 103 | + |
| 104 | + // Get a character array view |
| 105 | + string& view() { return *$self; } |
| 106 | + |
| 107 | + // Get a copy as a native Fortran string |
| 108 | + const string& str() { return *$self; } |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +%exception { |
| 113 | + SWIG_check_unhandled_exception(); |
| 114 | + try { |
| 115 | + $action |
| 116 | + } |
| 117 | + catch (const std::invalid_argument& e) { |
| 118 | + SWIG_exception(SWIG_ValueError, e.what()); |
| 119 | + } |
| 120 | + catch (const std::out_of_range& e) { |
| 121 | + SWIG_exception(SWIG_OverflowError, e.what()); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// String conversion |
| 126 | +int stoi(const std::string& s); |
| 127 | +long stol(const std::string& s); |
| 128 | +long long stoll(const std::string& s); |
| 129 | +float stof(const std::string& s); |
| 130 | +double stod(const std::string& s); |
| 131 | + |
| 132 | +} // namespace std |
0 commit comments