Debian Codegraph @

tensilelite/rocisa/docs/how-to-add-a-class.md

raw
1 # How to Add a Class in rocIsa
2
3 1. Write a class or structure as usual.
4
5 ```c++
6 struct Test
7 {
8 int testInt;
9 };
10 ```
11
12 2. If yourclass or structure inherits structures such as ``Item``, ``Instruction``. you'll have to add something more
13
14 ```c++
15 struct Test : public Item
16 {
17 // 1. A copy constructor
18 Test(const Test& other)
19 {
20 // your code here, make sure you deepcopy any pointer.
21 }
22
23 // 2. You need to overwrite this in order to make deepcopy work properly.
24 std::shared_ptr<Item> clone() const
25 {
26 return std::make_shared<Test>(*this);
27 }
28 };
29
30 ```
31
32 3. Create a new cpp or use an existing cpp if your class is related to one of the files.
33 4. Inside the cpp add the following code
34
35 ```c++
36 #include <nanobind/nanobind.h>
37 #include <nanobind/stl/shared_ptr.h> // Need to add this if you are exposing shared_ptr
38
39 namespace nb = nanobind;
40
41 void init_test(nb::module_ m)
42 {
43 nb::class_<Test>(m, "Test")
44 .def(nb::init<>())
45 .def("__deepcopy__", [](const rocisa::Test& self, nb::dict&) { return new rocisa::Test(self); });
46 }
47 ```