-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
30: add nanobind struct example, other refactoring
- Loading branch information
Showing
10 changed files
with
74 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,10 @@ _site/ | |
.jekyll-cache/ | ||
|
||
.DS_Store | ||
~$* | ||
~$* | ||
|
||
__pycache__/ | ||
.vscode/ | ||
*.o | ||
*.so | ||
build/ |
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
File renamed without changes.
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
6 changes: 2 additions & 4 deletions
6
...anobind-example/src/examplepkg/compute.py → ...nobind-example/src/example_pkg/compute.py
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
35 changes: 35 additions & 0 deletions
35
30_CCQ/nanobind-example/src/example_pkg/struct_example_module.cpp
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,35 @@ | ||
#include <iostream> | ||
#include <sstream> | ||
|
||
#include <nanobind/nanobind.h> | ||
#include <nanobind/ndarray.h> | ||
#include <nanobind/stl/string.h> | ||
|
||
namespace nb = nanobind; | ||
|
||
struct S { | ||
int i; | ||
S(int i):i{i}{} | ||
int m() const { return i+2;} | ||
}; | ||
|
||
// A function using S | ||
int f(S const & s){ return s.i;} | ||
|
||
// make S printable in C++ | ||
std::ostream & operator<<(std::ostream &out, S const & s) { | ||
return out << "S struct with i=" << s.i << '\n'; | ||
} | ||
|
||
NB_MODULE(struct_example_module, m) { | ||
nb::class_<S>(m, "S") | ||
.def_rw("i", &S::i) | ||
.def(nb::init<int>()) | ||
.def("m", &S::m) | ||
.def("__repr__", [](const S& s) { | ||
std::stringstream stream; | ||
stream << s; | ||
return stream.str(); | ||
}); | ||
m.def("f", &f); | ||
} |
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,13 @@ | ||
from . import struct_example_module | ||
|
||
def main(): | ||
s = struct_example_module.S(3) | ||
print() | ||
print(f'{s = }') | ||
print(f'{s.i = }') | ||
print(f'{s.m() = }') | ||
print(f'{struct_example_module.f(s) = }') | ||
print() | ||
|
||
if __name__ == '__main__': | ||
main() |