Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Julia support for the StdDequeIterator #431

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f06efcd
use `cppsize` when constructing `String` from `StdString`
kleinschmidt Oct 2, 2023
e84508c
whoops
kleinschmidt Oct 2, 2023
326d9f8
Use length argument in conversion to StdString
barche Oct 2, 2023
ecd5279
Always use ncodeunits as size argument when converting to StdString
barche Oct 4, 2023
a1e26aa
Stop `@cxxdereference` from messing up linenum information
omus Oct 18, 2023
81864a2
Handle UTF-8 code points in `StdString` (#381)
omus Oct 21, 2023
9c0055c
Bump version to v0.14.1 (#383)
barche Oct 21, 2023
05548c7
Parameterized constructors for `StdVector` (#385)
omus Oct 24, 2023
2e5c2eb
Bump version to v0.14.2 (#387)
barche Oct 25, 2023
952a584
Fix README test badge (#389)
omus Oct 25, 2023
a80510a
convert RefArray: add new RefArray type signature (#390)
benlorenz Nov 1, 2023
bbddc3f
Add method to Base.between to fix string indexing (#392)
barche Nov 16, 2023
3e02e69
docstring support for functions
melven Dec 20, 2023
4d0c193
Remove debug println
barche Jan 11, 2024
e2227db
extra_arg: working arg-names and kw-args
melven Jan 8, 2024
328ad85
extra_arg: implement default args
melven Jan 8, 2024
e245afb
extra_arg: improved default arguments
melven Jan 8, 2024
2086708
extra_arg: update README
melven Jan 13, 2024
b98380b
extra_arg: fix compatibility with different julia versions
melven Jan 16, 2024
2cdfcb3
Fix failing test on Julia 1.6
barche Jan 16, 2024
447cca2
Set version to v0.15.0
barche Jan 17, 2024
166996c
Properly deal with const template parameters
barche Feb 8, 2024
63daec0
Fix tests
barche Mar 7, 2024
eb8c2e6
Bump required libcxxwrap_julia version
barche Mar 7, 2024
cd76296
feat: add basic queue support
PraneethJain Feb 24, 2024
4503f75
test: queue
PraneethJain Feb 24, 2024
c8da17b
testing against correct version of libcxxwrap
PraneethJain Mar 4, 2024
47b1d22
fix: input from body
PraneethJain Mar 4, 2024
3e0d881
fix splitting
PraneethJain Mar 4, 2024
d551892
temp
PraneethJain Mar 4, 2024
a310b0a
feat: separate testing workflows
PraneethJain Mar 4, 2024
11e1408
fix: typo
PraneethJain Mar 4, 2024
197a4b7
test: add nightly
PraneethJain Mar 4, 2024
985c845
feat: change PR body format (#413)
PraneethJain Mar 8, 2024
4896072
Set continue-on-error for build step
barche Mar 8, 2024
ca90833
Bump required libcxxwrap-julia version
barche Mar 8, 2024
3f1a013
Document memory management for return values (#406)
sloede Mar 8, 2024
e9ca431
Remove test badge (#415)
barche Mar 8, 2024
c445909
Add breaking changes for v0.15 (#417)
barche Mar 9, 2024
9d06993
Add StdFill (#420)
PraneethJain Mar 31, 2024
bc6eeff
Add support for dynamic casting on the Julia side (#427)
barche Apr 27, 2024
5925baf
Test conditionally on ranges (#428)
barche Apr 28, 2024
00e8512
Set version to v0.15.1 (#429)
barche Apr 30, 2024
a31cbde
Added the StdDequeIterator and its testset
abdoei May 4, 2024
364718a
Merge branch 'testjll-stditerator' into main
abdoei May 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/test-linux-mac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: test-linux-mac
on:
push:
branches-ignore:
- prerelease
pull_request:
branches-ignore:
- prerelease

defaults:
run:
shell: bash

jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- "1.6"
- "1.10"
- "nightly"
os:
- macos-13
- ubuntu-latest
arch:
- x64
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- name: Build libcxxwrap
continue-on-error: true
run: |
mkdir libcxxwrap && cd libcxxwrap

body="${{github.event.pull_request.body}}"
first_line=$(echo "$body" | sed -n '1p')
if [[ "$first_line" == *"#"* ]]; then
IFS='#' read -r left right <<< "$first_line"

repo_url=$(echo "$left" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
branch=$(echo "$right" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
else
repo_url="$first_line"
branch="main"
fi
echo "repo_url: '$repo_url'"
echo "branch: '$branch'"
git clone --branch "$branch" --single-branch "$repo_url" .
echo "done cloning"

if [[ "$OSTYPE" != "darwin"* ]]; then
rm -f /opt/hostedtoolcache/julia/1.6*/x64/lib/julia/libstdc++.so.6
fi
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=$HOME/install -DAPPEND_OVERRIDES_TOML=ON -DOVERRIDE_VERSION_TO_JLL=ON -DCMAKE_BUILD_TYPE=Debug ..
VERBOSE=ON cmake --build . --config Debug --target install
cd ../..
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
76 changes: 76 additions & 0 deletions .github/workflows/test-win.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: test-win
on:
push:
branches-ignore:
- prerelease
pull_request:
branches-ignore:
- prerelease

defaults:
run:
shell: bash

jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- "1.7"
- "1.10"
- "nightly"
os:
- windows-latest
arch:
- x64
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- name: Clone libcxxwrap
continue-on-error: true
run: |
mkdir libcxxwrap && cd libcxxwrap

body="${{github.event.pull_request.body}}"
first_line=$(echo "$body" | sed -n '1p')
if [[ "$first_line" == *"#"* ]]; then
IFS='#' read -r left right <<< "$first_line"

repo_url=$(echo "$left" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
branch=$(echo "$right" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
else
repo_url="$first_line"
branch="main"
fi
echo "repo_url: '$repo_url'"
echo "branch: '$branch'"
git clone --branch "$branch" --single-branch "$repo_url" .
echo "done cloning"
- name: Config 32bit
continue-on-error: true
if: ${{ matrix.arch == 'x86'}}
run: |
cd libcxxwrap && mkdir build && cd build
cmake -G "Visual Studio 17 2022" -A Win32 -DOVERRIDES_PATH=$HOMEDRIVE/$HOMEPATH/.julia/artifacts/Overrides.toml -DOVERRIDE_ROOT=./ -DAPPEND_OVERRIDES_TOML=ON ..
- name: Config 64bit
continue-on-error: true
if: ${{ matrix.arch == 'x64'}}
run: |
cd libcxxwrap && mkdir build && cd build
cmake -G "Visual Studio 17 2022" -A x64 -DOVERRIDES_PATH=$HOMEDRIVE/$HOMEPATH/.julia/artifacts/Overrides.toml -DOVERRIDE_ROOT=./ -DAPPEND_OVERRIDES_TOML=ON ..
- name: Build libcxxwrap
continue-on-error: true
run: |
cd libcxxwrap/build
cmake --build . --config Release

- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
40 changes: 0 additions & 40 deletions .github/workflows/test.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CxxWrap"
uuid = "1f15a43c-97ca-5a2a-ae31-89f07a497df4"
authors = ["Bart Janssens <[email protected]>"]
version = "0.14.0"
version = "0.15.1"

[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand All @@ -11,7 +11,7 @@ libcxxwrap_julia_jll = "3eaa8342-bff7-56a5-9981-c04077f7cee7"
[compat]
MacroTools = "0.5.9"
julia = "1.6"
libcxxwrap_julia_jll = "0.11.0"
libcxxwrap_julia_jll = "0.12.3"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
51 changes: 44 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# CxxWrap

![test](https://github.com/JuliaInterop/CxxWrap.jl/workflows/test/badge.svg)

This package aims to provide a Boost.Python-like wrapping for C++ types and functions to Julia.
The idea is to write the code for the Julia wrapper in C++, and then use a one-liner on the Julia side to make the wrapped C++ library available there.

Expand Down Expand Up @@ -179,11 +177,11 @@ CppTypes.set(w, "hello")
```

The manually added constructor using the `constructor` function also creates a finalizer.
This can be disabled by adding the argument `false`:
This can be disabled by adding the argument `jlcxx::finalize_policy::no`:

```c++
types.add_type<World>("World")
.constructor<const std::string&>(false);
.constructor<const std::string&>(jlcxx::finalize_policy::no);
```

The `add_type` function actually builds two Julia types related to `World`.
Expand Down Expand Up @@ -296,6 +294,29 @@ auto multi_vector_base = mod.add_type<Parametric<TypeVar<1>>>("MultiVectorBase")
auto vector_base = mod.add_type<Parametric<TypeVar<1>>>("VectorBase", multi_vector_base.dt());
```

### Conversion

Conversion to the base type happens automatically, or can be forced by calling convert, e.g.

```julia
convert(A,b)
```

Where we have `b::B` and `B <: A`

For the equivalent of a C++ `dynamic_cast`, we need to use pointers because the conversion may fail, i.e:

```julia
convert(CxxPtr{B},CxxPtr(a))
```

This is equivalent to the C++ code:
```c++
dynamic_cast<B*>(&a);
```

Use `isnull` on the result to check if the conversion was successful or not.

See the test at [`examples/inheritance.cpp`](https://github.com/JuliaInterop/libcxxwrap-julia/tree/master/examples/inheritance.cpp) and [`test/inheritance.jl`](test/inheritance.jl).

## Enum types
Expand Down Expand Up @@ -385,7 +406,8 @@ There is also an `apply_combination` method to make applying all combinations of

Full example and test including non-type parameters at: [`examples/parametric.cpp`](https://github.com/JuliaInterop/libcxxwrap-julia/tree/master/examples/parametric.cpp) and [`test/parametric.jl`](test/parametric.jl).

## Constructors and destructors
## Memory management
### Constructors and destructors

The default constructor and any manually added constructor using the `constructor` function will automatically create a Julia object that has a finalizer attached that calls delete to free the memory.
To write a C++ function that returns a new object that can be garbage-collected in Julia, use the `jlcxx::create` function:
Expand All @@ -405,6 +427,13 @@ wvec = cpp_function_returning_vector()
julia_array = copy.(wvec)
```

### Return values
If a wrapped C++ function returns an object by value, the wrapped object gets a finalizer
and is owned by Julia. The same holds if a smart pointer such as `shared_ptr` (automatically
wrapped in a `SharedPtr`) is returned by value. In contrast to that, if a reference or raw
pointer is returned from C++, then the default assumption is that the pointed-to object
lifetime is managed by C++.

## Call operator overload

Since Julia supports overloading the function call operator `()`, this can be used to wrap `operator()` by just omitting the method name:
Expand Down Expand Up @@ -648,10 +677,10 @@ The behavior of the macro can be customized by adding methods to `CxxWrap.refere
## Exceptions

When directly adding a regular free C++ function as a method, it will be called directly using `ccall` and any exception will abort the Julia program.
To avoid this, you can force wrapping it in an `std::function` to intercept the exception automatically by setting the `force_convert` argument to `method` to true:
To avoid this, you can force wrapping it in an `std::function` to intercept the exception automatically by setting the `jlcxx::calling_policy` argument to `std_function`:

```c++
mod.method("test_exception", test_exception, true);
mod.method("test_exception", test_exception, jlcxx::calling_policy::std_function);
```

Member functions and lambdas are automatically wrapped in an `std::function` and so any exceptions thrown there are always intercepted and converted to a Julia exception.
Expand Down Expand Up @@ -902,6 +931,10 @@ mod.method("getSecondaryWorldVector", [](const World* p)->const std::vector<Worl

Linking wrappers using STL support requires adding `JlCxx::cxxwrap_julia_stl` to the `target_link_libraries` command in `CMakeLists.txt`.

### Working with `StdString`

The `StdString` implements the Julia string interface and interprets `std::string` data as UTF-8 data. Since C++ strings do not require the use of the null-character to denote the end of a string the `StdString` constructors usually rely on the `ncodeunits` to determin the size of the string. When constructing a `StdString` from a `Cstring`, `Base.CodeUnits`, or `Vector{UInt8}` the first null-character present will denote the end of the string.

## Release procedure

Often, new releases of `CxxWrap` also require a new release of the C++ component `libcxxwrap-julia`, and a rebuild of its JLL package. To make sure everything is tested properly, the following procedure should be followed for each release that requires changing both the Julia and the C++ component:
Expand Down Expand Up @@ -964,6 +997,10 @@ Often, new releases of `CxxWrap` also require a new release of the C++ component
## Breaking changes in v0.13
* Automatic dereferencing of smart pointers was removed, so some code may require adding the dereferencing operator `[]` explicitly. See [PR #338](https://github.com/JuliaInterop/CxxWrap.jl/pull/338).

## Breaking changes in v0.15
* This release is based on `libcxxwrap-julia` 0.12, which is binary incompatible with previous versions, so JLLs should be rebuilt to use CxxWrap 0.15
* The `constructor` method now takes a `jlcxx::finalize_policy` instead of a `bool`, e.g. `.constructor<Foo>(false)` becomes `.constructor<Foo>(jlcxx::finalize_policy::no)`

## References
* [JuliaCon 2020 Talk: Julia and C++: a technical overview of CxxWrap.jl](https://www.youtube.com/watch?v=u7IaXwKSUU0)
* [JuliaCon 2020 Workshop: Wrapping a C++ library using CxxWrap.jl](https://www.youtube.com/watch?v=VoXmXtqLhdo)
Loading
Loading