diff --git a/cython/capnp_wrapper.h b/cython/capnp_wrapper.h index 0caaa49..c242137 100644 --- a/cython/capnp_wrapper.h +++ b/cython/capnp_wrapper.h @@ -15,3 +15,7 @@ T ReaderFromBytes(char * dat, size_t sz){ auto msg_reader = new capnp::FlatArrayMessageReader(kj::ArrayPtr(buf.begin(), size)); return msg_reader->getRoot(); } + + +template +using List = typename capnp::List::Reader; diff --git a/cython/generator.py b/cython/generator.py index 980511c..9c9c326 100755 --- a/cython/generator.py +++ b/cython/generator.py @@ -8,6 +8,9 @@ from libc.stdint cimport * cdef extern from "capnp_wrapper.h": cdef T ReaderFromBytes[T](char* dat, size_t sz) + cdef cppclass List[T]: + T operator[](int) + int size() """ @@ -134,10 +137,29 @@ def gen_code(definition, node, name=None): field_tp = field.proto.slot.type._which_str() - if field_tp in ['list', 'text', 'data']: + if field_tp in ['text', 'data']: continue + elif field_tp == 'list': + print(field) + list_tp = field.proto.slot.type.list.elementType._which_str() - if field_tp == 'struct': + if list_tp in ['struct', 'enum']: + continue + + if list_tp in ['text', 'data']: + continue + + list_tp = TYPE_LOOKUP[list_tp] + + pxd += 8 * " " + f"List[{list_tp}] get{name_cap}()\n" + + pyx += 4 * " " + f"@property\n" + pyx += 4 * " " + f"def {name}(self):\n" + pyx += 8 * " " + f"cdef List[{list_tp}] l = self.reader.get{name_cap}()\n" + pyx += 8 * " " + f"return [l[i] for i in range(l.size())]\n\n" + + print(list_tp) + elif field_tp == 'struct': if struct_full_name is None: continue diff --git a/cython/test/test_struct.py b/cython/test/test_struct.py index 153f015..6aced57 100644 --- a/cython/test/test_struct.py +++ b/cython/test/test_struct.py @@ -45,3 +45,12 @@ class TestStruct(unittest.TestCase): self.assertEqual(l_cython.which(), l.which()) self.assertEqual(l_cython.thermal.cpu0, l.thermal.cpu0) + + + def test_list_of_primitive(self): + cs = car.CarState.new_message() + cs.canMonoTimes = [0, 1, 2, 3, 4] + + b = cs.to_bytes() + cs_cython = cython_log.CarState(b) + self.assertEqual(cs_cython.canMonoTimes, list(cs.canMonoTimes))