Ref, ConstRef, StringRef and other adapters #339
-
Why are these necessary? I couldn't figure it out from the code. I tried removing the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes, you can pass either a reference to something you own: T* or pass ownership of the object directly: T One thing I observed:
Ref is an adapter. It represents either a reference to an external object, or an object it owns. This way, developers can pass either T or T*. StringRef are like Ref, but with the added capability to convert std::wstring <=> std::string automatically. This helped me switching from an API using UTF-16 std::wstring toward an API using UTF-8 std::string, without breaking legacy users. Does this help? |
Beta Was this translation helpful? Give feedback.
-
It is essential for the component to be able not to "own" the data. For instance, let say you to implement a tab bar, displaying multiple subcomponent, depending on the value selected. std::vector<std::string> tab_values{"tab_1", "tab_2", "tab_3"};
int tab_selected = 0;
auto tab_toggle = Toggle(&tab_values, &tab_selected);
auto tab_container = Container::Tab({
[...] // Component for tab 1.
[...] // Component for tab 2.
[...] // Component for tab 3.
},
&tab_selected);
auto final_component = Container::Vertical({
tab_toggle,
tab_container,
}); You see "tab_selected" is both referenced from the Toggle (to select the right tab) and from Container::Tab (to display the right tab) If Toggle or Container::Tab was owning their own version of "tab_selected", we would have to implement some synchronization in between them. |
Beta Was this translation helpful? Give feedback.
It is essential for the component to be able not to "own" the data.
For instance, let say you to implement a tab bar, displaying multiple subcomponent, depending on the value selected.