Skip to content

Commit

Permalink
Avoid optional
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed May 31, 2024
1 parent e3248b3 commit 63cae05
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions python/event_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
#include "../udp.hpp"
#include <numpy/arrayobject.h>
#include "npy_2_compat.h"
#include <optional>

/// description represents a named type with an offset.
struct description {
std::string name;
std::optional<std::string> title;
std::string title;
NPY_TYPES type;
};

Expand All @@ -21,30 +20,30 @@ template <sepia::type event_stream_type>
std::vector<description> get_descriptions();
template <>
std::vector<description> get_descriptions<sepia::type::generic>() {
return {{"t", {}, NPY_UINT64}, {"bytes", {}, NPY_OBJECT}};
return {{"t", "", NPY_UINT64}, {"bytes", "", NPY_OBJECT}};
}
template <>
std::vector<description> get_descriptions<sepia::type::dvs>() {
return {{"t", {}, NPY_UINT64}, {"x", {}, NPY_UINT16}, {"y", {}, NPY_UINT16}, {"on", "p", NPY_BOOL}};
return {{"t", "", NPY_UINT64}, {"x", "", NPY_UINT16}, {"y", "", NPY_UINT16}, {"on", "p", NPY_BOOL}};
}
template <>
std::vector<description> get_descriptions<sepia::type::atis>() {
return {
{"t", {}, NPY_UINT64},
{"x", {}, NPY_UINT16},
{"y", {}, NPY_UINT16},
{"t", "", NPY_UINT64},
{"x", "", NPY_UINT16},
{"y", "", NPY_UINT16},
{"exposure", "e", NPY_BOOL},
{"polarity", "p", NPY_BOOL}};
}
template <>
std::vector<description> get_descriptions<sepia::type::color>() {
return {
{"t", {}, NPY_UINT64},
{"x", {}, NPY_UINT16},
{"y", {}, NPY_UINT16},
{"r", {}, NPY_UINT8},
{"g", {}, NPY_UINT8},
{"b", {}, NPY_UINT8}};
{"t", "", NPY_UINT64},
{"x", "", NPY_UINT16},
{"y", "", NPY_UINT16},
{"r", "", NPY_UINT8},
{"g", "", NPY_UINT8},
{"b", "", NPY_UINT8}};
}

/// offsets calculates the packed offsets from the description types.
Expand Down Expand Up @@ -78,23 +77,15 @@ static PyArray_Descr* event_type_to_dtype() {
const auto descriptions = get_descriptions<event_stream_type>();
auto python_names_and_types = PyList_New(static_cast<Py_ssize_t>(descriptions.size()));
for (Py_ssize_t index = 0; index < static_cast<Py_ssize_t>(descriptions.size()); ++index) {

const auto name = PyUnicode_FromString(descriptions[index].name.c_str());
auto name_and_title = name;
if (descriptions[index].title) {
name_and_title = PyTuple_Pack(
2,
name,
PyUnicode_FromString(descriptions[index].title.value().c_str())
);
if (!descriptions[index].title.empty()) {
name_and_title = PyTuple_Pack(2, name, PyUnicode_FromString(descriptions[index].title.c_str()));
}
if (PyList_SetItem(
python_names_and_types,
index,
PyTuple_Pack(
2,
name_and_title,
PyArray_TypeObjectFromType(descriptions[index].type)))
PyTuple_Pack(2, name_and_title, PyArray_TypeObjectFromType(descriptions[index].type)))
< 0) {
throw std::logic_error("PyList_SetItem failed");
}
Expand Down

0 comments on commit 63cae05

Please sign in to comment.