Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pybind11 dependency from cpp library #40

Merged
merged 3 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions lib/ros_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,6 @@ std::vector<RosValue::Pointer> RosValue::getValues() const {
return ros_value_pointers;
}

pybind11::buffer_info RosValue::getPrimitiveArrayBufferInfo() {
if (type_ != Embag::RosValue::Type::primitive_array) {
throw std::runtime_error("Only primitive arrays can be represented as buffers!");
}

if (primitive_array_info_.element_type == Embag::RosValue::Type::string) {
throw std::runtime_error("In order to be represented as a buffer, an array's elements must not be strings!");
}

const size_t size_of_elements = Embag::RosValue::primitiveTypeToSize(primitive_array_info_.element_type);
return pybind11::buffer_info(
(void*) &at(0)->getPrimitive<uint8_t>(),
size_of_elements,
Embag::RosValue::primitiveTypeToFormat(primitive_array_info_.element_type),
1,
{ size() },
{ size_of_elements },
true
);
}

std::string RosValue::toString(const std::string &path) const {
switch (type_) {
case Type::ros_bool: {
Expand Down
5 changes: 4 additions & 1 deletion lib/ros_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
#include <cstdint>
#include <cstring>
#include <memory>
#include <pybind11/buffer_info.h>
#include <string>
#include <unordered_map>
#include <vector>

#include "span.hpp"
#include "util.h"

namespace pybind11 {
struct buffer_info;
}

Copy link

@jaredjxyz jaredjxyz Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm forward declarations can be a dangerous hole to go down - Our style guide says to avoid them whenever possible https://google.github.io/styleguide/cppguide.html#Forward_Declarations

Why do you need the forward declaration here? Can you structure this in a way that doesn't need it?

Copy link

@kashish-jain kashish-jain Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! Since this is a 3rd party library, it might just be better to not have a forward declaration. Do you need RosValue::getPrimitiveArrayBufferInfo() declaration in this file @liamembark? If not, then we can delete this forward declaration, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya so the underlying need for this is that the buffer_info object needs access to the underlying memory buffer, but this is a private member.
As a result, whatever is going to access this memory buffer needs to be declared along with the RosValue.
AFAIK this is always going to require forward declaration (if not let me know), but one alternative to forward declaring a piece of a third party library, I could instead forward declare a friend function that will be defined in the python library to provide access!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Liam I'll DM you to discuss this a bit, I think I understand the problem but want to dig in just a little more

namespace Embag {

class RosValue {
Expand Down
22 changes: 22 additions & 0 deletions python/adapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,26 @@ template<>
const py::object RosValue::const_iterator<py::object, size_t>::operator*() const {
return castValue(value_.at(index_));
}

pybind11::buffer_info RosValue::getPrimitiveArrayBufferInfo() {
if (type_ != Embag::RosValue::Type::primitive_array) {
throw std::runtime_error("Only primitive arrays can be represented as buffers!");
}

if (primitive_array_info_.element_type == Embag::RosValue::Type::string) {
throw std::runtime_error("In order to be represented as a buffer, an array's elements must not be strings!");
}

const size_t size_of_elements = Embag::RosValue::primitiveTypeToSize(primitive_array_info_.element_type);
return pybind11::buffer_info(
(void*) &at(0)->getPrimitive<uint8_t>(),
size_of_elements,
Embag::RosValue::primitiveTypeToFormat(primitive_array_info_.element_type),
1,
{ size() },
{ size_of_elements },
true
);
}

}