-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type_caster for ntcore raw values
- std::vector<uint8_t> is only used in raw contexts, so the type caster intercepts and converts the value automatically to/from bytes - Fixes #45 - There may be other places we could use this technique?
- Loading branch information
Showing
4 changed files
with
48 additions
and
0 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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
--- | ||
|
||
extra_includes: | ||
- src/nt_type_caster.h | ||
|
||
classes: | ||
GenericSubscriber: | ||
methods: | ||
|
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
--- | ||
|
||
extra_includes: | ||
- src/nt_type_caster.h | ||
|
||
classes: | ||
RawSubscriber: | ||
methods: | ||
|
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
--- | ||
|
||
extra_includes: | ||
- src/nt_type_caster.h | ||
|
||
defaults: | ||
ignore: true | ||
report_ignored_missing: false | ||
|
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,39 @@ | ||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace pybind11 { | ||
namespace detail { | ||
|
||
// ntcore uses std::vector<uint8_t> anytime there is a raw value, so | ||
// add this specialization to convert to/from bytes directly | ||
|
||
template<> | ||
struct type_caster<std::vector<uint8_t>> { | ||
using vector_type = std::vector<uint8_t>; | ||
PYBIND11_TYPE_CASTER(vector_type, const_name("bytes")); | ||
|
||
bool load(handle src, bool convert) { | ||
if (!isinstance<buffer>(src)) { | ||
return false; | ||
} | ||
auto buf = reinterpret_borrow<buffer>(src); | ||
auto req = buf.request(); | ||
if (req.ndim != 1) { | ||
return false; | ||
} | ||
|
||
auto begin = (const uint8_t*)req.ptr; | ||
auto end = begin + req.size*req.itemsize; | ||
|
||
value = std::vector<uint8_t>(begin, end); | ||
return true; | ||
} | ||
|
||
static handle cast(const std::vector<uint8_t> &src, return_value_policy policy, handle parent) { | ||
return py::bytes((char*)src.data(), src.size()).release(); | ||
} | ||
}; | ||
|
||
} | ||
} |