Skip to content

Commit

Permalink
Merge pull request #35 from serjepatoff/fix-nim-2.0.4
Browse files Browse the repository at this point in the history
Fix for Nim 2.0.0 - 2.0.4
  • Loading branch information
Clonkk authored Sep 16, 2024
2 parents dff4752 + c3e72ff commit 268dff3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ requires "cppstl"

## Limitations

``cppstl`` currently wraps :
#### ``cppstl`` currently wraps :

* ``std::string``
* ``std::basic_string``
Expand All @@ -39,6 +39,26 @@ requires "cppstl"
* ``std::unique_ptr``
* ``std::shared_ptr``

#### Avoid using wrapped STL objects in top-level Nim scope.
Most of the times it works on Nim 1.x but leads to both compile-time and runtime errors on 2.x.
So instantiate them in subroutines only to ensure portability between 1.x and 2.x.

I.e this usecase is not recommended:
```
when isMainModule:
var vec = initCppVector[int]()
vec.pushBack(20)
```
Use this one instead:
```
when isMainModule:
proc foo =
var vec = initCppVector[int]()
vec.pushBack(20)
foo()
```


## Contributions

All contributions are welcome!
Expand Down
10 changes: 9 additions & 1 deletion cppstl/std_string.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proc initCppString*(s: cstring, n: csize_t): CppString {.constructor, importcpp:
proc initCppString*(first, last: CppStrConstIterator): CppString {.constructor, importcpp: "std::string(@)".}

# Modifiers
proc `+=`*(self: var CppString, str: cstring) {.importcpp: "# += #".}
proc `+=`*(self: var CppString, str: cstring) {.importcpp: "(# += #)".}

proc append*(self: var CppString, str: cstring) {.importcpp: "append".}
proc append*(self: var CppString, str: cstring, n: csize_t) {.importcpp: "append".}
Expand Down Expand Up @@ -70,6 +70,14 @@ proc compare*(self: CppString, pos, l: csize_t, str: cstring, subpos, subl: csiz
# Converter: CppStrIterator -> StrConstIterator
converter CppStrIteratorToStrConstIterator*(s: CppStrIterator): CppStrConstIterator {.importcpp: "#".}

# Relational operators
proc `==`*(a: CppString, b: CppString): bool {.importcpp: "(# == #)".}
proc `!=`*(a: CppString, b: CppString): bool {.importcpp: "(# != #)".}
proc `<`*(a: CppString, b: CppString): bool {.importcpp: "(# < #)".}
proc `<=`*(a: CppString, b: CppString): bool {.importcpp: "(# <= #)".}
proc `>`*(a: CppString, b: CppString): bool {.importcpp: "(# > #)".}
proc `>=`*(a: CppString, b: CppString): bool {.importcpp: "(# >= #)".}

{.pop.}

{.push inline.}
Expand Down
3 changes: 3 additions & 0 deletions tests/tcomplex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import complex
import std/math
import cppstl/std_complex


proc main() =
suite "CppComplex":
test "constructors":
Expand All @@ -28,6 +29,7 @@ proc main() =
refres = refa + refb
res = a + b
check almostEqual(refres, toComplex(res))

block:
var
a = initCppComplex[float64](141.571, 124.412)
Expand Down Expand Up @@ -94,3 +96,4 @@ proc main() =

when isMainModule:
main()

2 changes: 2 additions & 0 deletions tests/tpair.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,7 @@ proc main() =
check tup == (first: f, second: s)
check makePair(tup) == pair


when isMainModule:
main()

3 changes: 3 additions & 0 deletions tests/tstring.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import cppstl/std_string


proc main() =
suite "CppString":
test "constructors and iterators":
Expand Down Expand Up @@ -416,5 +417,7 @@ proc main() =
c = 'b'
check s1.toString == "bbbbbbbbb"


when isMainModule:
main()

5 changes: 4 additions & 1 deletion tests/tvector.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import std/[unittest, strformat, sequtils]
import cppstl/std_vector


proc main() =
suite "CppVector":
test "constructor, size/len, empty":
Expand Down Expand Up @@ -445,4 +446,6 @@ proc main() =
check v1 == @['w', 'x', 'y', 'z'].toCppVector()
check v2 == @['a', 'b', 'c'].toCppVector()

main()
when isMainModule:
main()

0 comments on commit 268dff3

Please sign in to comment.