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

Fix various Nim 2.0 issue (unary operator, global scope, $ templating) #32

Merged
merged 22 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 20 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ on:
- master

jobs:
tests:
runs-on: ubuntu-latest
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
nim:
- '2.0.x'
- 'stable'
- devel
os:
- ubuntu-latest
# - windows-latest
- macOS-latest
name: '${{ matrix.nim }} (${{ matrix.os }})'
steps:
- uses: actions/checkout@v2
- name: "install_nim"
id: install_nim
uses: iffy/install-nim@v3
- uses: actions/checkout@v4
- name: Setup nim
uses: jiro4989/setup-nim-action@v2
with:
use-nightlies: true
nim-version: ${{ matrix.nim }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
- run: nimble install -y
- run: testament p "tests/t*.nim"
- run: nimble test
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ jobs:
- uses: actions/checkout@v2
with:
ref: "gh-pages"
- name: "install_nim"
id: install_nim
uses: iffy/install-nim@v3

- uses: jiro4989/setup-nim-action@v2
with:
# TODO use 2.2.0 when it's released - gendoc is broken om 2.0.8
nim-version: devel
use-nightlies: true
repo-token: ${{ secrets.GITHUB_TOKEN }}
- run: nimble install -y
- name: Set CI config github
run: |
Expand Down
3 changes: 3 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ when not compiles(nimVersion):
when nimVersion >= (1, 3, 3):
# https://github.com/nim-lang/Nim/commit/9502e39b634eea8e04f07ddc110b466387f42322
switch("backend", "cpp")

when defined(macosx):
switch("cc", "gcc")
4 changes: 2 additions & 2 deletions cppstl.nimble
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Package

version = "0.6.2"
version = "0.7.0"
author = "Clonkk"
description = "Bindings for the C++ Standard Template Library (STL)"
license = "MIT"


# Dependencies

requires "nim >= 1.0.0"
requires "nim >= 2.0.0"

backend = "cpp"

Expand Down
7 changes: 3 additions & 4 deletions cppstl/private/iterators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ type
CppIterator*[ValueType] {.importc.} = object
CppConstIterator*[ValueType] {.importc.} = object

type
SomeCppIterator = CppIterator or CppConstIterator
type SomeCppIterator = CppIterator or CppConstIterator

proc `+`*[I: SomeCppIterator](it: I, offset: int): I {.importcpp: "# + #"}
proc `-`*[I: SomeCppIterator](it: I, offset: int): I {.importcpp: "# - #"}
proc `+`*[I: SomeCppIterator](it: I, offset: int): I {.importcpp: "# + #".}
proc `-`*[I: SomeCppIterator](it: I, offset: int): I {.importcpp: "# - #".}

proc inc*(it: SomeCppIterator) {.importcpp: "(void)(++#)".}
proc inc*(it: SomeCppIterator, offset: int) {.importcpp: "(void)(# += #)".}
Expand Down
192 changes: 142 additions & 50 deletions cppstl/std_basicstring.nim

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions cppstl/std_complex.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# std::complex
# -----------------------------------------------------------------------
{.push header: "<complex>".}
type
CppComplex*[T: SomeFloat] {.importcpp: "std::complex".} = object
type CppComplex*[T: SomeFloat] {.importcpp: "std::complex".} = object

func initCppComplex*[T: SomeFloat](re, im: T): CppComplex[T] {.constructor, importcpp: "std::complex<'*0>(@)".}
func polar*[T: SomeFloat](r, theta: T): CppComplex[T] {.importcpp: "std::polar<'*0>(@)".}
Expand Down
3 changes: 1 addition & 2 deletions cppstl/std_exception.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ when (NimMajor, NimMinor, NimPatch) < (1, 4, 0):
# IndexDefect was introduced in 1.4.0
type IndexDefect* = IndexError

type
OutOfRangeException* {.importcpp: "std::out_of_range", header: "stdexcept".} = object of ValueError
type OutOfRangeException* {.importcpp: "std::out_of_range", header: "stdexcept".} = object of ValueError
28 changes: 14 additions & 14 deletions cppstl/std_pair.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import std/strformat
when not defined(cpp):
{.error: "C++ backend required to use STL wrapper".}

{.push header:"<utility>".}
{.push header: "<utility>".}
# https://cplusplus.com/reference/utility/pair/

type
CppPair*[F,S] {.importcpp:"std::pair <'0,'1>"} = object
type CppPair*[F, S] {.importcpp: "std::pair <'0,'1>".} = object

# procs
proc first*[T1, T2](this: CppPair[T1, T2]): T1 {.importcpp: "#.first".}
Expand All @@ -28,17 +27,17 @@ proc swap*[T1, T2](this, other: var CppPair[T1, T2]) {.importcpp: "#.swap(@)".}

# Non-member functions
# https://en.cppreference.com/w/cpp/utility/pair/make_pair
proc makePair*[F,S](a:F; b:S):CppPair[F,S] {.importcpp:"std::make_pair(@)" .}
proc makePair*[F, S](a: F, b: S): CppPair[F, S] {.importcpp: "std::make_pair(@)".}

proc getImpl[T1, T2](p: CppPair[T1, T2], T: typedesc[T1 or T2]): T {.importcpp: "std::get<'0>(@)".}
proc getImpl[T1, T2](n: int, p: CppPair[T1, T2], T: typedesc[T1 or T2]): T {.importcpp: "std::get<#>(@)".}

# proc `==`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# == #".}
# proc `!=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# != #".}
# proc `<`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# < #".}
# proc `<=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# <= #".}
# proc `>`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# > #".}
# proc `>=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "# >= #".}
# proc `==`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# == #)".}
# proc `!=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# != #)".}
# proc `<=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# <= #)".}
# proc `>=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# >= #)".}
# proc `<`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# < #)".}
# proc `>`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool {.importcpp: "(# > #)".}

{.pop.} # header

Expand Down Expand Up @@ -71,6 +70,7 @@ proc `<`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool =
return true
else:
return false

#
proc `<=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool =
## 4) !(rhs < lhs)
Expand All @@ -89,7 +89,7 @@ proc `>=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool =
result = false
else:
result = true
#

#-----------
# Some sugar
#-----------
Expand All @@ -108,15 +108,15 @@ proc get*[T1, T2](n: static int, p: CppPair[T1, T2]): auto =
{.error: "index in pair must be 0 or 1".}
getImpl(n, p, ResultType)

proc `$`*[F,S](val:CppPair[F,S]):string =
proc `$`*[F, S](val: CppPair[F, S]): string =
# provides stdout for CppPair
&"CppPair(first: {val.first}, second: {val.second})"

proc toTuple*[F,S](val:CppPair[F,S]):tuple[first:F, second:S] =
proc toTuple*[F, S](val: CppPair[F, S]): tuple[first: F, second: S] =
## converts a CppPair into a Nim's tuple
(val.first, val.second)

proc makePair*[F, S](t: tuple[first: F, second: S]) : CppPair[F, S] =
proc makePair*[F, S](t: tuple[first: F, second: S]): CppPair[F, S] =
result = initCppPair[F, S]()
result.first = t.first
result.second = t.second
35 changes: 9 additions & 26 deletions cppstl/std_smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ when not defined(cpp):
# -----------------------------------------------------------------------
{.push header: "<memory>".}

type
CppSharedPtr*[T]{.importcpp: "std::shared_ptr", bycopy.} = object
type CppSharedPtr*[T] {.importcpp: "std::shared_ptr", bycopy.} = object

proc newCppSharedPtr*[T](p: ptr T): CppSharedPtr[T] {.constructor,
importcpp: "std::shared_ptr<'*0>(#)".}
proc newCppSharedPtr*[T](p: ptr T): CppSharedPtr[T] {.constructor, importcpp: "std::shared_ptr<'*0>(#)".}

func makeShared*(T: typedesc): CppSharedPtr[T] {.importcpp: "std::make_shared<'*0>()".}

func makeShared*[T](p: CppSharedPtr[T]): CppSharedPtr[T] {.importcpp: "std::make_shared<'*0>(#)".}

proc `=copy`*[T](p: var CppSharedPtr[T], o: CppSharedPtr[T]) {.noInit, importcpp: "# = #".}
proc `=sink`*[T](dst: var CppSharedPtr[T], src: CppSharedPtr[T]){.importcpp: "# = std::move(#)".}
proc `=sink`*[T](dst: var CppSharedPtr[T], src: CppSharedPtr[T]) {.importcpp: "# = std::move(#)".}

# std::unique_ptr<T>
# -----------------------------------------------------------------------
type
CppUniquePtr*[T]{.importcpp: "std::unique_ptr", header: "<memory>", bycopy.} = object
type CppUniquePtr*[T] {.importcpp: "std::unique_ptr", header: "<memory>", bycopy.} = object

func makeUnique*(T: typedesc): CppUniquePtr[T] {.importcpp: "std::make_unique<'*0>()".}

proc `=copy`*[T](dst: var CppUniquePtr[T], src: CppUniquePtr[T]) {.error: "A unique ptr cannot be copied".}
proc `=sink`*[T](dst: var CppUniquePtr[T], src: CppUniquePtr[T]){.importcpp: "# = std::move(#)".}
proc `=sink`*[T](dst: var CppUniquePtr[T], src: CppUniquePtr[T]) {.importcpp: "# = std::move(#)".}

{.pop.}

# Let C++ destructor do their things
proc `=destroy`[T](dst: var CppUniquePtr[T]) =
discard

proc `=destroy`[T](dst: var CppSharedPtr[T]) =
discard

Expand All @@ -53,29 +51,14 @@ func `$`*[T](p: CppSharedPtr[T]): string =
result = "CppShared " & repr(get(p))

macro `.()`*[T](p: CppUniquePtr[T] or CppSharedPtr[T], fieldOrFunc: untyped, args: varargs[untyped]): untyped =
result = nnkCall.newTree(
nnkDotExpr.newTree(
newCall(bindSym"deref", p),
fieldOrFunc
)
)
result = nnkCall.newTree(nnkDotExpr.newTree(newCall(bindSym"deref", p), fieldOrFunc))
copyChildrenTo(args, result)
# echo result.repr

macro `.`*[T](p: CppUniquePtr[T] or CppSharedPtr[T], fieldOrFunc: untyped): untyped =
result = nnkDotExpr.newTree(
newCall(bindSym"deref", p),
fieldOrFunc
)
result = nnkDotExpr.newTree(newCall(bindSym"deref", p), fieldOrFunc)
# echo result.repr

macro `.=`*[T](p: CppUniquePtr[T] or CppSharedPtr[T], fieldOrFunc: untyped, args: untyped): untyped =

result = newAssignment(
nnkDotExpr.newTree(
newCall(bindSym"deref", p),
fieldOrFunc
),
args
)
result = newAssignment(nnkDotExpr.newTree(newCall(bindSym"deref", p), fieldOrFunc), args)
# echo result.repr
43 changes: 25 additions & 18 deletions cppstl/std_string.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ proc insert*(self: var CppString, pos: csize_t, s: cstring, n: csize_t) {.import
proc replace*(self: var CppString, pos, l: csize_t, s: cstring) {.importcpp: "replace".}
proc replace*(self: var CppString, i1, i2: CppStrConstIterator, s: cstring) {.importcpp: "replace".}
proc replace*(self: var CppString, pos, l: csize_t, s: cstring, n: csize_t) {.importcpp: "replace".}
proc replace*(self: var CppString, i1, i2: CppStrConstIterator, s: cstring, n: csize_t) {.
importcpp: "replace".}
proc replace*(self: var CppString, i1, i2: CppStrConstIterator, s: cstring, n: csize_t) {.importcpp: "replace".}

# CppString operations
# Avoid const char* vs char* issues
Expand Down Expand Up @@ -73,47 +72,54 @@ converter CppStrIteratorToStrConstIterator*(s: CppStrIterator): CppStrConstItera

{.pop.}


{.push inline.}

proc initCppString*(s: string): CppString =
initCppString(s.cstring)

proc `+`*(a: CppString, b: string|cstring): CppString =
proc `+`*(a: CppString, b: string | cstring): CppString =
result = (a + initCppString(b))
proc `+`*(a: string|cstring, b: CppString): CppString =

proc `+`*(a: string | cstring, b: CppString): CppString =
let a = initCppString(a)
result = (a + b)

proc `==`*(a: CppString, b: string|cstring): bool =
proc `==`*(a: CppString, b: string | cstring): bool =
let b = initCppString(b)
result = (a == b)
proc `==`*(a: string|cstring, b: CppString): bool =

proc `==`*(a: string | cstring, b: CppString): bool =
let a = initCppString(a)
result = (a == b)

proc `!=`*(a: CppString, b: string|cstring): bool =
proc `!=`*(a: CppString, b: string | cstring): bool =
result = (a != initCppString(b))
proc `!=`*(a: string|cstring, b: CppString): bool =

proc `!=`*(a: string | cstring, b: CppString): bool =
result = (initCppString(a) != b)

proc `<`*(a: CppString, b: string|cstring): bool =
proc `<`*(a: CppString, b: string | cstring): bool =
result = (a < initCppString(b))
proc `<`*(a: string|cstring, b: CppString): bool =

proc `<`*(a: string | cstring, b: CppString): bool =
result = (initCppString(a) < b)

proc `<=`*(a: CppString, b: string|cstring): bool =
proc `<=`*(a: CppString, b: string | cstring): bool =
result = (a <= initCppString(b))
proc `<=`*(a: string|cstring, b: CppString): bool =

proc `<=`*(a: string | cstring, b: CppString): bool =
result = (initCppString(a) <= b)

proc `>`*(a: CppString, b: string|cstring): bool =
proc `>`*(a: CppString, b: string | cstring): bool =
result = (a > initCppString(b))
proc `>`*(a: string|cstring, b: CppString): bool =

proc `>`*(a: string | cstring, b: CppString): bool =
result = (initCppString(a) > b)

proc `>=`*(a: CppString, b: string|cstring): bool =
proc `>=`*(a: CppString, b: string | cstring): bool =
result = (a >= initCppString(b))
proc `>=`*(a: string|cstring, b: CppString): bool =

proc `>=`*(a: string | cstring, b: CppString): bool =
result = (initCppString(a) >= b)

{.pop.}
Expand All @@ -123,7 +129,8 @@ proc `>=`*(a: string|cstring, b: CppString): bool =
proc toCppString*(s: string): CppString {.inline.} =
initCppString(cstring(s), len(s).csize_t)

proc toString*(s: CppString): string = $(s.cStr())
proc toString*(s: CppString): string =
$(s.cStr())

# Display CppString
proc `$`*(s: CppString): string {.noinit.} =
Expand Down
Loading
Loading