-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "nested.h" | ||
|
||
Time::Time(unsigned long v) : timestamp(v){}; | ||
Time::operator unsigned long() const { return timestamp; }; | ||
|
||
DateTime::DateTime(Time v) : time(v){}; | ||
DateTime::operator Time &() { return time; }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Time { | ||
unsigned long timestamp; | ||
|
||
public: | ||
Time(unsigned long v); | ||
operator unsigned long() const; | ||
}; | ||
|
||
class DateTime { | ||
public: | ||
Time time; | ||
DateTime(Time v); | ||
operator Time &(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <fixtures/nested.h> | ||
|
||
#include <nobind.h> | ||
|
||
NOBIND_MODULE(nested, m) { | ||
m.def<Time>("Time").cons<unsigned long>().def < &Time::operator unsigned long>("get"); | ||
|
||
m.def<DateTime>("DateTime") | ||
.cons<Time>() | ||
.cons<unsigned long>() | ||
// Getters of object members automatically return nested references | ||
.def<&DateTime::time>("time") | ||
// Explicitly return a nested reference | ||
.def<&DateTime::operator Time &, Nobind::ReturnNested>("get"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { assert } = require('chai'); | ||
|
||
describe('nested objects', () => { | ||
it('constructors', () => { | ||
const time = new dll.Time(100); | ||
assert.instanceOf(time, dll.Time); | ||
|
||
const dt1 = new dll.DateTime(100); | ||
assert.instanceOf(dt1, dll.DateTime); | ||
|
||
const dt2 = new dll.DateTime(time); | ||
assert.instanceOf(dt2, dll.DateTime); | ||
}); | ||
|
||
it('operators', () => { | ||
const time = new dll.Time(100); | ||
assert.isNumber(time.get()); | ||
assert.strictEqual(time.get(), 100); | ||
|
||
const dt1 = new dll.DateTime(101); | ||
assert.instanceOf(dt1, dll.DateTime); | ||
assert.strictEqual(dt1.get().get(), 101); | ||
|
||
const dt2 = new dll.DateTime(time); | ||
assert.instanceOf(dt2, dll.DateTime); | ||
assert.strictEqual(dt2.get().get(), 100); | ||
}); | ||
|
||
it('explicit nested references', () => { | ||
const dt = new dll.DateTime(new dll.Time(111)); | ||
assert.instanceOf(dt, dll.DateTime); | ||
assert.strictEqual(dt.get().__nobind_parent_reference, dt); | ||
assert.strictEqual(dt.get().get(), 111); | ||
}); | ||
|
||
it('implicit nested references', () => { | ||
const dt = new dll.DateTime(new dll.Time(117)); | ||
assert.instanceOf(dt, dll.DateTime); | ||
assert.strictEqual(dt.time.__nobind_parent_reference, dt); | ||
assert.strictEqual(dt.get().get(), 117); | ||
}); | ||
}); |