looking for help

i am working on a native executable using parcel. the executable is in c++ and will send the parcel to a java function, the parcel in java will use parcel.writetypedlist however writetypedlist is not available in c++ parcel so im stuck. i found this link https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md, in with it states "Consider an AIDL type in @nullable List<String> bar. This type indicates that the remote caller may pass in a list of strings, and that both the list and any string in the list may be null. This type will map to a C++ type unique_ptr<vector<unique_ptr<String16>>>* bar". however im looking for examples on how to initialize a vector of type unique_ptr<vector<unique_ptr<String16>>>* with 6 values in it 4 of which are null. any help or especialy any example would be greatly appreciated
Last edited on
1
2
3
4
5
6
auto vector = std::make_unique<std::vector<std::unique_ptr<String16>>>();
//std::unique_ptr<String16> some_String16;
//std::unique_ptr<String16> some_other_String16;
vector->resize(6);
vector[0] = std::move(some_String16);
vector[1] = std::move(some_other_String16);
thank you very much @helios,
i tried that and got

type 'std::__1::unique_ptr<std::__1::vector<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> >, std::__1::allocator<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> > > >, std::__1::default_delete<std::__1::vector<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> >, std::__1::allocator<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> > > > > >' does not provide a subscript operator
vector[0] = std::move(String16("310"));

i should point out im new to c++


NOTE: the vector is being sent to a function requiring const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val, however the page i link to says i need to use unique_ptr<vector<unique_ptr<String16>>>* bar


qoute from page linked to "Note that by default, the generated C++ passes a const reference to the value of a parameter and rejects null references with a NullPointerException sent back the caller. Parameters marked with @nullable are passed by pointer, allowing native services to explicitly control whether they allow method overloading via null parameters. Java stubs and proxies currently do nothing with the @nullable annotation."
Last edited on
vector[0] = std::move(String16("310"));
The correct way to write this (assuming String16("310") is correct) would be
 
vector[0] = std::make_unique<String16>("310");
Last edited on
Yes String16("310") is correct it maps to String in android java im away from pc now i will test shortly but thank you in advance
still not working
error: type 'std::__1::unique_ptr<std::__1::vector<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> >, std::__1::allocator<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> > > >, std::__1::default_delete<std::__1::vector<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> >, std::__1::allocator<std::__1::unique_ptr<android::String16, std::__1::default_delete<android::String16> > > > > >' does not provide a subscript operator
vector[0] = std::make_unique<String16>("310");

truely appreciate your effort
Last edited on
Also for info on String16 it is defined in utils/String16.h
https://android.googlesource.com/platform/frameworks/native/+/android-4.2.2_r1/include/utils/String16.h

The vector im trying to make will be written to android::Parcel using android::Parcel.writeString16vector defined here
https://android.googlesource.com/platform/frameworks/native/+/master/libs/binder/include/binder/Parcel.h
Last edited on
Oops. My bad.
 
(*vector)[0] = std::make_unique<String16>("310");
That worked as (*vector)[0] = std::make_unique<String16>(String16("310"));

How i was of the understanding that unique_ptr<vector<unique_ptr<String16>>>*
Allowed for setting values of null or 0 (not "0")
As this does not seem to allow null
Last edited on
If you're using my original snippet, elements 2, 3, 4, and 5 of the vector are null, because the vector was resized to size 6 and only the first two elements were set, so the other elements retain their values set by the default constructor of std::unique_ptr<String16> (which is null). To explicitly set an element to null:
1
2
3
4
5
6
(*vector)[whatever] = std::unique_ptr<String16>();
//or:
(*vector)[whatever].reset();

//or, to push a nullptr at the back of the vector:
vector->emplace_back();
Last edited on
I see now you are correct resize to 6 set values of 0 and 1 and 2, 3, 4 and 5 are not set leaving them null. Perfect you are awesome truely truely greatful

Last edited on
Seems i was confused and needed to be multi domenional vector of same type

I beleave would be as
(*vector)[0] [0]= std::make_unique<String16>(String16"310"));
(*vector)[0] [1]= std::make_unique<String16>(String16"310"));

(*vector)[1] [0]= std::make_unique<String16>(String16"310"));
(*vector)[1] [1]= std::make_unique<String16>(String16"310"));

Not positive that is correct or how to declare the vector as 2d or 3d and so on
Last edited on
Topic archived. No new replies allowed.