list of primitive types

This commit is contained in:
Willem Melching
2020-05-26 14:58:42 -07:00
parent 34f96f2574
commit 1e53bf7d7d
3 changed files with 37 additions and 2 deletions

View File

@@ -15,3 +15,7 @@ T ReaderFromBytes(char * dat, size_t sz){
auto msg_reader = new capnp::FlatArrayMessageReader(kj::ArrayPtr<capnp::word>(buf.begin(), size));
return msg_reader->getRoot<typename T::Reads>();
}
template<typename T>
using List = typename capnp::List<T>::Reader;

View File

@@ -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

View File

@@ -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))