dropbox / djinni

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

[Question]Include functions in record #383

Closed sarkarchandan closed 6 years ago

sarkarchandan commented 6 years ago

Hi Everyone,

I am a student developer and I am trying to learn Djinni. I have a question regarding the records in the interface builder file. I understand that they get translated to value type, namely struct in C++. For simple use cases that works seamlessly.

Now I am trying to create a Graph with a value type Node that would be used to model a simple Graph node. And I want to use std::unordered_map<Node , std::vector > construct to create an adjacency list representation. In the unordered_map my key will be a Node and value will be a std::vector of Node.

To be able to use Node as a key in unordered_map, I need to make it uniquely identifiable. At the end I want to represent the Node in this way. This is just an example.

struct Node { std::string identifier; Node(std::string _identifier) :identifier(std::move(_identifier)) {} Node(){} bool operator==(const Node& otherNode) const { return identifier == otherNode.identifier; } std::string getInfo() { return identifier; } };

struct NodeHasher { std::size_t operator()(const Node& keyNode) const { return std::hash()(keyNode.identifier); } };

Is there any way I could write my record in the IDL file which makes it possible for me to achieve some thing like this ?

I tried different ways to write my Node record in the .djinni file, all resulting in errors. I tried to use interface instead which seemed possible at first but later created other complications ?

Could you please give me some advise if I am trying things in wrong way ?

Thanks in advance, Chandan.

j4cbo commented 6 years ago

I think the feature you'd need for this is #154. As a workaround, you could perhaps use the node's identifier (string) as the map key. Is 'Node' just a wrapper type around string, or will it gain more functionality?

sarkarchandan commented 6 years ago

@j4cbo , Thanks a lot for the response. For now, yes Node is just a wrapper type. In the example above I have used a string but in essence, I want to make it a wrapper of some generic type for now.

And yes, I can use the String as fallback strategy. Meanwhile, I have discovered +c syntax also applicable for records. It is supposed to generate some base struct type which we can extend perhaps. Before using String key as my last resort, I would probably give it a try as well.

sarkarchandan commented 6 years ago

I have worked with the extension to the record construct in the .djinni IDL file and could deal with the challenge I was having. Extension(+c) to record created a base structure for me to extend and include the stuff I wanted it to have. Finally I could create a user defined type which is hash-able and could be used as a key in the unordered_map.