I18n: add per-socket translation contexts for nodes #105195
|
@ -183,22 +183,19 @@ namespace blender::ed::space_node {
|
|||
|
||||
static const char *node_socket_get_translation_context(const bNodeSocket &socket)
|
||||
{
|
||||
std::stringstream output;
|
||||
const nodes::SocketDeclaration &socket_decl = *socket.runtime->declaration;
|
||||
|
||||
/* The node is not explicitly defined. */
|
||||
if (&socket_decl == nullptr) {
|
||||
if (socket.runtime->declaration == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
blender::StringRef translation_context = socket.runtime->declaration->translation_context;
|
||||
|
||||
/* Default context. */
|
||||
blender::StringRef translation_context = socket_decl.translation_context;
|
||||
if (translation_context.is_empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
output << translation_context.data();
|
||||
return BLI_strdup(output.str().c_str());
|
||||
return BLI_strdup(translation_context.data());
|
||||
pioverfour marked this conversation as resolved
Hans Goudey
commented
Why Why `BLI_strdup`? Could you just use `StringRefNull` and return that directly? It looks like the result isn't being freed, and this negates the benefit of any small string optimization in `std::string`
|
||||
}
|
||||
|
||||
static void node_socket_add_tooltip_in_node_editor(const bNodeTree &ntree,
|
||||
pioverfour marked this conversation as resolved
Bastien Montagne
commented
Not sure why you are using an intermediate Not sure why you are using an intermediate `std::stringstream` here? Can't you just do `return BLI_strdup(socket_decl.translation_context.c_str());` ?
Jacques Lucke
commented
That sounds reasonable. That sounds reasonable.
I guess the reason for why the `std::stringstream` was used is that you assigned it to a `StringRef` which does not have `c_str` method, because it might not be null-terminated. Using `const StringRefNull` or what @mont29 would be better though.
Damien Picard
commented
To be quite honest I copied code doing a similar thing from To be quite honest I copied code doing a similar thing from `node_socket_get_tooltip()`, but I see that it doesn’t really apply here.
I’ll go with @mont29 ’s solution.
|
||||
|
|
This is needed to avoid a crash when certain nodes are added (for instance, Render Layers in the compositing nodes).
GCC gives a warning:
the compiler can assume that the address of ‘socket_decl’ will never be NULL [-Waddress]
.I suppose it means this should not happen because a null pointer would mean a bug occurs before reaching this point. But I don’t know how to fix this better since it appears not all nodes have a declaration.
You cannot use a reference if there is a chance that the pointer will be NULL. So if
socket.runtime->declaration
can benullptr
, you need to use a pointer forsocket_decl
- or do the check before assigning it to your reference.At first check
socket.runtime->declaration != nullptr
, and after that dereference pointerThanks for the help! I hope I fixed this correctly.