dropbox / djinni

A tool for generating cross-language type declarations and interface bindings.
Apache License 2.0
2.88k stars 488 forks source link

std::shared_ptr<T> and std::function<void <void* ptr , size_t size> > #425

Closed XingXingDeTomato closed 5 years ago

XingXingDeTomato commented 5 years ago

i am a beginner i used std::shared_ptr and std::function<void < void*ptr ,size_t size >> in C++ interface , like this:

( 1 ) void setCallback(std::function<void(void ptr, size_t size)> callback); ( 2 ) std::function<void(void ptr, size_t size)> getCallback; ( 3 ) std::shared_ptr<std::unordered_map<std::string, std::string> > getMap();

now , i want to generate interface code ( java ,oc, c++) , how should i write the Djinni file ? thanks you

adamtait commented 5 years ago

I'm a little confused by your question, but it sounds like you might be trying to write your interface definition. That is what Djinni does for you (you need to write the implementation).

Another note from your question - I see (void *) in use. This would be invalid in Djinni. Djinni requires you to specify data types for all your interface definitions. See the data types section of the README.

XingXingDeTomato commented 5 years ago

I'm a little confused by your question, but it sounds like you might be trying to write your interface definition. That is what Djinni does for you (you need to write the implementation).

Another note from your question - I see (void *) in use. This would be invalid in Djinni. Djinni requires you to specify data types for all your interface definitions. See the data types section of the README.

first of all , thanks for your answer . i want to generate C++ code above , but i don't know how to write djinni file or does Djinni not support generating the C++ interface above , i think it might be the latter but I'm not sure ,because my english is not good .

if Djinni doesn't support generate it ( std::shared_ptr std::function<...> ),can you give me some advices without changing the existing interface , thanks .

artwyman commented 5 years ago

std::function isn't a supported datatype in Djinni. In general you can meet the same need using an interface with as single method like call(). Assuming the ptr & size you're passing is a memory buffer, that could be passed as a list of bytes, though be aware that Djinni copies data across the language boundary, so there's some cost involved. Djinni passes maps by value, not by pointer. With those caveats, the intent of your interface seems like it can be met by Djinni. In general, you shouldn't try to get Djinni to match a precise pre-existing interface in C++, since that's not what it's for. You should use Djinni to declare what data you want to pass, and let it generate the interface for you in all languages.

I think your requested interface might be something like this:

callback = interface +j +o {
  call(list<i8>);
}

my_thing = interface +c {
  set_callback(callback c);
  callback get_callback();
  map<string, string> get_map();
}

You should try it out and see what is generated, and write glue code to convert types in each language as necessary. If you want to get more help from experienced users, I'd suggest you join the Slack community mentioned in the README and discuss there.

XingXingDeTomato commented 5 years ago

std::function isn't a supported datatype in Djinni. In general you can meet the same need using an interface with as single method like call(). Assuming the ptr & size you're passing is a memory buffer, that could be passed as a list of bytes, though be aware that Djinni copies data across the language boundary, so there's some cost involved. Djinni passes maps by value, not by pointer. With those caveats, the intent of your interface seems like it can be met by Djinni. In general, you shouldn't try to get Djinni to match a precise pre-existing interface in C++, since that's not what it's for. You should use Djinni to declare what data you want to pass, and let it generate the interface for you in all languages.

I think your requested interface might be something like this:

callback = interface +j +o {
  call(list<i8>);
}

my_thing = interface +c {
  set_callback(callback c);
  callback get_callback();
  map<string, string> get_map();
}

You should try it out and see what is generated, and write glue code to convert types in each language as necessary. If you want to get more help from experienced users, I'd suggest you join the Slack community mentioned in the README and discuss there.

Thank you very much for your help , which is very helpful to me , thank you again.