Skip to content

Support conversion between vector<Any> and vector<typename T::value_type> #873

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dyackzan
Copy link

@dyackzan dyackzan commented Sep 24, 2024

These changes make way for us to do general vector operations like iterate over, index, append, and remove using BT.CPP. Using vector<Any> objects on the blackboard allows us to create these operations as singletons, rather than requiring each operation to be templated for each vector<T>

The 2 primary components to this PR:

  • In setOutput, convert vector<T> objects to vector<Any> before placing them on the blackboard
  • In getInput, attempt to unwrap and convert vector<Any> to vector<T> if a vector<T> is expected

Don't check port type alignment for vector<Any>
@dyackzan dyackzan marked this pull request as ready for review September 24, 2024 17:44
@dyackzan dyackzan force-pushed the support-vec-any-to-vec-type-conversion branch from 3e23456 to d55f69e Compare October 3, 2024 17:33
@dyackzan dyackzan changed the title Support vector<Any> -> vector<typename T::value_type> conversion Support conversion between vector<Any> and vector<typename T::value_type> Oct 3, 2024
@dyackzan dyackzan force-pushed the support-vec-any-to-vec-type-conversion branch from d55f69e to fbb6583 Compare October 7, 2024 15:40
@dyackzan dyackzan force-pushed the support-vec-any-to-vec-type-conversion branch 5 times, most recently from 29c90be to 7d0219e Compare October 23, 2024 22:47
Also update checks to allow mismatch when a port was declared as a
vector<T> and we have an input port that takes it in as a vector<Any>
@dyackzan dyackzan force-pushed the support-vec-any-to-vec-type-conversion branch from 7d0219e to f8493e5 Compare October 23, 2024 22:48
Copy link

@nbbrooks nbbrooks left a comment

Choose a reason for hiding this comment

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

Could you document the gcc and clang demangled output for the std::vector<std::string> for reference here in the PR if it isn't possible to add a unit test establishing the demangled output passes the regex max? Ideally we'd have a unit test for partial specialization as well.

@dyackzan
Copy link
Author

I checked clang and gcc with the type demangler and get the the same output for the demangled typeid of a std::vector<string> for both:

std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >

@nbbrooks
Copy link

Oops, when I hit approve I lost track of my tabs and thought I was approving a submodule update to point to this PR branch 🤦

@facontidavide
Copy link
Collaborator

looks like a good addition. Can you please:

  • use pre-commit to fix the formatting?
  • add a simple unit test too?

@dyackzan dyackzan force-pushed the support-vec-any-to-vec-type-conversion branch from 55a9f5a to 6607968 Compare November 6, 2024 15:39
@dyackzan
Copy link
Author

dyackzan commented Nov 6, 2024

How does that look @facontidavide ?

@dyackzan
Copy link
Author

dyackzan commented Dec 2, 2024

Tagging @facontidavide , could you give me another review on this?

@dyackzan
Copy link
Author

Friendly ping @facontidavide , please let me know if there's anything else you'd like me to change here. We want to keep our fork as up to date with your base as possible

@facontidavide
Copy link
Collaborator

facontidavide commented Apr 19, 2025

Ok, I am finally looking into this.

  std::string xml_txt_bad = R"(
    <root BTCPP_format="4" >
      <BehaviorTree>
        <Sequence name="root_sequence">
          <OutputVectorStringNode/>
          <InputVectorDoubleNode double_vector="{string_vector}"/>
        </Sequence>
      </BehaviorTree>
    </root>)";
    
ASSERT_NO_THROW(tree = factory.createTreeFromText(xml_txt_bad));

In my opinion this is SUPPOSED to throw. You are basically bypassing the entire type safety mechanism, unless you rely on the fact that string is the usual "wildcard" that has conversion to numbers.

@facontidavide
Copy link
Collaborator

Another problem I see is that this will not play nicely with this:

https://github.com/BehaviorTree/BehaviorTree.CPP/blob/master/examples/t13_access_by_ref.cpp

If I try to access any vector by reference, I will find a vector<Any>. This will break other people code.

@facontidavide facontidavide self-assigned this Apr 19, 2025
@facontidavide
Copy link
Collaborator

Summary:

  • you basically bypass the entire type safety checks for any vector<T>.
  • accessing the blackboard by reference will return an unexpected type.
  • the unit test doesn't seem meaningful to me, in terms of desired functionality. What you want to show is a generic node that can access an existing port as vector<Any>

I understand your problem and the annoyance of templates, and I would like to find a solution, but I am not sure that this is the one I want to merge.

I was thinking about doing the conversion to vector<Any> lazily, for instance you have your input port AnyVector and it should be able to connect and read from vector<string>. The problem is when writing back into the blackboard, because we don't want to change the original type.

A potential solution might be to store a to/from vector<Any> "converters" and run them only when requested, to avoid altering the original type in the blackboard.

On the other hand, what the "right" type of the blackboard is is based on some "fuzzy" criteria, i.e. the order of the XMl parsing...

I am not sure how to proceed. Probably creating a better test/use case first and then discussing that.

@facontidavide
Copy link
Collaborator

If you want, we can arrange a meeting, and iterate faster to find alternatives. You can contact me at [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants