Skip to content

Commit

Permalink
nph
Browse files Browse the repository at this point in the history
  • Loading branch information
Clonkk committed Sep 16, 2024
1 parent 3048be5 commit 3b07120
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 310 deletions.
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
178 changes: 135 additions & 43 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
16 changes: 7 additions & 9 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,7 +27,7 @@ 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<#>(@)".}
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 @@ -90,8 +90,6 @@ proc `>=`*[T1, T2](lhs, rhs: CppPair[T1, T2]): bool =
else:
result = true



#-----------
# Some sugar
#-----------
Expand All @@ -110,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
42 changes: 24 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,48 +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 @@ -124,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

0 comments on commit 3b07120

Please sign in to comment.