Skip to content

Commit 1102733

Browse files
committed
Add vector<string>
1 parent ebd1f22 commit 1102733

File tree

6 files changed

+1524
-29
lines changed

6 files changed

+1524
-29
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ swig_fortran_add_module(flc_string)
176176
target_link_libraries(flc_string flc)
177177

178178
swig_fortran_add_module(flc_vector)
179-
target_link_libraries(flc_vector flc)
179+
target_link_libraries(flc_vector flc flc_string)
180180

181181
#---------------------------------------------------------------------------#
182182
# INSTALLATION

doc/modules/vector.rst

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
Vector
99
******
1010

11-
Vectors are resizeable arrays of elements. All vectors support the following
12-
basic operations.
11+
Vectors are resizeable arrays of elements. The ``flc_vector`` module
12+
instantiates vectors of ``integer(4)``, ``integer(8)``, ``real(8)``, and
13+
``type(String)``.
14+
15+
Common functionality
16+
====================
17+
18+
All vector types support the following basic operations.
1319

1420
Construction and destruction
1521
----------------------------
@@ -77,6 +83,14 @@ The size of a vector is returned by the bound function ``size``; ``get``
7783
returns the value at an index; and ``front`` and ``back`` are aliases for
7884
``get(1)`` and ``get(v%size())``, respectively.
7985

86+
Additionally, ``front_ref``, ``back_ref``, and ``get_ref`` return pointers to
87+
the elements of the array.
88+
89+
.. warning:: Array element pointers are valid **only** as long as the vector's
90+
size is not changed. Calling ``erase``, ``push_back``, and so forth will
91+
invalidate the pointer; accessing it at that point results in undefined
92+
behavior.
93+
8094
Numeric vectors
8195
===============
8296

@@ -100,6 +114,8 @@ an array pointer::
100114
v = Vector(iarr)
101115
write(0,*) "Size should be 4:", v%size()
102116

117+
The ``assign`` bound method acts like a constructor but for an existing vector.
118+
103119
View as an array pointer
104120
------------------------
105121

@@ -124,7 +140,20 @@ underlying memory as the C++ object::
124140
String vectors
125141
==============
126142

127-
String vectors are not yet implemented.
143+
String vectors' native "element" type is a ``character(len=:)``. Vector
144+
operations that accept an input will take any native character string; and
145+
returned values will be allocatable character arrays.
146+
147+
The ``front_ref``, ``back_ref``, and ``get_ref`` functions allow the underlying
148+
``std::string`` class to be accessed with the ``String`` Fortran derived type
149+
wrapper. Note that unlike for intrinsic types, where these functions return a
150+
``integer, pointer``, the vector of strings returns just ``type(String)``.
151+
However, as with native pointers described above, these references are
152+
*invalid* once the string changes size. They should be cleared with the
153+
``%release()`` bound method.
154+
155+
An additional ``set_ref`` function allows vector elements to be assigned from
156+
vector classes.
128157

129158
.. ############################################################################
130159
.. end of doc/modules/vector.rst

src/flc_vector.i

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,18 @@ namespace std {
6868
%template(VectorInt4) std::vector<int32_t>;
6969
%template(VectorInt8) std::vector<int64_t>;
7070
%template(VectorReal8) std::vector<double>;
71+
72+
/* ------------------------------------------------------------------------- */
73+
74+
%extend std::vector<std::string> {
75+
void set_ref(size_type index, std::string& str) {
76+
SWIG_check_range(index, $self->size(),
77+
"std::vector<std::string>::set_ref",
78+
return);
79+
(*$self)[index] = str;
80+
}
81+
}
82+
83+
%include <std_string.i>
84+
%import "flc_string.i"
85+
%template(VectorString) std::vector<std::string>;

0 commit comments

Comments
 (0)