Skip to content

Commit

Permalink
Merge pull request #32 from maihd/main
Browse files Browse the repository at this point in the history
Pointers of child class implicit cast
  • Loading branch information
SomeRanDev committed Sep 5, 2023
2 parents 72d23a7 + ded1b8c commit dfc58e1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions std/cxx/Ptr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ extern abstract Ptr<T>(T) from T to T from Value<T> {

@:nativeFunctionCode("(*{this})")
public function toValue(): cxx.Value<T>;

@:from
@:nativeFunctionCode("{arg0}")
static function fromSubType<T, U: T>(other: Ptr<U>) : Ptr<T>;
}
4 changes: 4 additions & 0 deletions std/cxx/SharedPtr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ extern abstract SharedPtr<T>(T) from T to T {
@:typeArgNotNullable
@:nativeFunctionCode("std::make_shared<{type0}>({arg0})")
public static extern function make<T>(obj: T): SharedPtr<T>;

@:from
@:nativeFunctionCode("{arg0}")
static function fromSubType<T, U: T>(other: SharedPtr<U>) : SharedPtr<T>;
}
4 changes: 4 additions & 0 deletions std/cxx/UniquePtr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ extern abstract UniquePtr<T>(T) from T to T {

@:nativeFunctionCode("({this}.reset({arg1}))")
public overload function reset(other: cxx.Ptr<T>): Void;

@:from
@:nativeFunctionCode("{arg0}")
static function fromSubType<T, U: T>(other: UniquePtr<U>) : UniquePtr<T>;
}
2 changes: 2 additions & 0 deletions test/unit_testing/tests/ChildClassPointers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
out
38 changes: 38 additions & 0 deletions test/unit_testing/tests/ChildClassPointers/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package;

var exitCode = 0;
function assert(v: Bool) {
if(!v) {
exitCode = 1;
}
}

class A {
public var val: Int;
}

class B extends A {
public function new() {
val = 0;
}
}

class C {
public var val: Int;
public function new() {
val = 100;
}
}

function main() {
var a: cxx.Ptr<A> = cxx.Ptr.Null;
var b: cxx.Ptr<B> = new B();

// Valid, B is child of A
a = b;

var c: cxx.Ptr<C> = new C();

// Invalid, C is not child A (uncomment to test)
// a = c;
}
6 changes: 6 additions & 0 deletions test/unit_testing/tests/ChildClassPointers/Test.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following are added automatically:
# -lib reflaxe.cpp
# -D cpp-output=out

-D mainClass=Main
-main Main

0 comments on commit dfc58e1

Please sign in to comment.