diff --git a/README.md b/README.md index 404ee21..eba406e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ requires "cppstl" ## Limitations -``cppstl`` currently wraps : +#### ``cppstl`` currently wraps : * ``std::string`` * ``std::basic_string`` @@ -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! diff --git a/cppstl/std_string.nim b/cppstl/std_string.nim index ae408d2..bc34685 100644 --- a/cppstl/std_string.nim +++ b/cppstl/std_string.nim @@ -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".} @@ -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.} diff --git a/tests/tcomplex.nim b/tests/tcomplex.nim index b98c029..4555e4a 100644 --- a/tests/tcomplex.nim +++ b/tests/tcomplex.nim @@ -4,6 +4,7 @@ import complex import std/math import cppstl/std_complex + proc main() = suite "CppComplex": test "constructors": @@ -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) @@ -94,3 +96,4 @@ proc main() = when isMainModule: main() + diff --git a/tests/tpair.nim b/tests/tpair.nim index 3c73394..4496974 100644 --- a/tests/tpair.nim +++ b/tests/tpair.nim @@ -116,5 +116,7 @@ proc main() = check tup == (first: f, second: s) check makePair(tup) == pair + when isMainModule: main() + diff --git a/tests/tstring.nim b/tests/tstring.nim index ee94cac..ad433d9 100644 --- a/tests/tstring.nim +++ b/tests/tstring.nim @@ -2,6 +2,7 @@ import unittest import cppstl/std_string + proc main() = suite "CppString": test "constructors and iterators": @@ -416,5 +417,7 @@ proc main() = c = 'b' check s1.toString == "bbbbbbbbb" + when isMainModule: main() + diff --git a/tests/tvector.nim b/tests/tvector.nim index ba13d2d..b3fdd34 100644 --- a/tests/tvector.nim +++ b/tests/tvector.nim @@ -2,6 +2,7 @@ import std/[unittest, strformat, sequtils] import cppstl/std_vector + proc main() = suite "CppVector": test "constructor, size/len, empty": @@ -445,4 +446,6 @@ proc main() = check v1 == @['w', 'x', 'y', 'z'].toCppVector() check v2 == @['a', 'b', 'c'].toCppVector() -main() +when isMainModule: + main() +