Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add translation function for logic classes #7188

Closed

Conversation

@jcfr

This comment was marked as outdated.

@jcfr
Copy link
Member

jcfr commented Aug 23, 2023

As outlined in the build log1, the linking is currently failing on Linux:

[1537/5178] Linking CXX shared library bin/libMRMLLogic.so
FAILED: bin/libMRMLLogic.so 
...
Libs/MRML/Logic/CMakeFiles/MRMLLogic.dir/vtkMRMLApplicationLogic.cxx.o: In function `vtkMRMLApplicationLogic::GetTranslatorInstance()':
vtkMRMLApplicationLogic.cxx:(.text+0x1c97): undefined reference to `vtkMRMLApplicationLogic::TranslatorInstance'
Libs/MRML/Logic/CMakeFiles/MRMLLogic.dir/vtkMRMLApplicationLogic.cxx.o: In function `vtkMRMLApplicationLogic::SetTranslatorInstance(vtkMRMLTranslator*)':
vtkMRMLApplicationLogic.cxx:(.text+0x1cd7): undefined reference to `vtkMRMLApplicationLogic::TranslatorInstance'
Libs/MRML/Logic/CMakeFiles/MRMLLogic.dir/vtkMRMLApplicationLogic.cxx.o: In function `vtkMRMLApplicationLogic::Translate(char const*, char const*, char const*, int)':
vtkMRMLApplicationLogic.cxx:(.text+0x1d1b): undefined reference to `vtkMRMLApplicationLogic::TranslatorInstance'
collect2: error: ld returned 1 exit status
[1538/5178] Linking CXX shared library lib/Slicer-5.5/ITKFactories/libMRMLIDIOPlugin.so
[1539/5178] Building CXX object Libs/MRML/Core/CMakeFiles/MRMLCorePython.dir/MRMLCorePythonInit.cxx.o
ninja: build stopped: subcommand failed.

Footnotes

  1. https://github.com/Slicer/Slicer/actions/runs/5946372990/job/16126820100?pr=7188

Libs/MRML/Logic/vtkMRMLTranslator.h Show resolved Hide resolved
#ifndef vtkMRMLTranslator_h
#define vtkMRMLTranslator_h

class vtkMRMLTranslator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class vtkMRMLTranslator
class VTK_MRML_LOGIC_EXPORT vtkMRMLTranslator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this comment, the update is done. May I know why we need to add VTK_MRML_LOGIC_EXPORT before the class name ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding VTK_MRML_LOGIC_EXPORT designates the class to be visible on the public API of the shared library. VTK_MRML_LOGIC_EXPORT is a compiler/linker-specific macro defined in an automatically generated .h file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I better understand now !

Base/QTCore/qMRMLTranslator.h Show resolved Hide resolved

#include "vtkMRMLTranslator.h"

class qMRMLTranslator: public vtkMRMLTranslator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class qMRMLTranslator: public vtkMRMLTranslator
class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator

Base/QTCore/qMRMLTranslator.h Outdated Show resolved Hide resolved
Libs/MRML/Logic/vtkMRMLTranslator.cxx Outdated Show resolved Hide resolved
Libs/MRML/Logic/vtkMRMLTranslator.h Outdated Show resolved Hide resolved
Base/QTCore/qSlicerCoreApplication.cxx Outdated Show resolved Hide resolved
Libs/MRML/Logic/vtkMRMLApplicationLogic.h Outdated Show resolved Hide resolved
Libs/MRML/Logic/vtkMRMLApplicationLogic.h Outdated Show resolved Hide resolved
@mhdiop mhdiop force-pushed the add-translation-function-for-logic-classes branch from e6f2233 to 711c669 Compare August 23, 2023 11:11

#include "qSlicerBaseQTCoreExport.h"

class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this class is derived from a VTK base class, it has to be a VTK class, i.e., vtkMRMLTranslator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular class should probably be called qSlicerQtVTKTranslationBridge ?

Note that since its role is to establish a "bridge" so that VTK logic classes can indirectly leverage Qt translation capabilities through polymorphism, it doesn't have to strictly start with the prefix vtk.

That said, may be the name vtkSlicerQtVTKTranslationBridge would be better ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vtk prefix is an important hint about how the class is allowed to be constructed and used. vtk... classes are based on vtkObject and rely on internal reference counting. So, for clarity and consistency we either use a proper VTK class (with appropriate naming, internal reference counting, private constructor, etc.) or we don't make the translator a VTK class at all. We could make it a Qt class (derived from QObject, following Qt conventions) then, or just make it a simple C++ class (we establishing all rules).

Since initialization and destruction of singleton classes is a difficult topic, I would use already established singleton patterns in Slicer or VTK. The current design seems to be some custom implementation, which may not work correctly in a large application, composed of many shared libraries.

Copy link
Member

@jcfr jcfr Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: memory

Inheriting from vtkObject make sense and will allow to use vtkNew (that way we are consistent and we don't need to make use of std::shared_ptr)

re: established singleton patterns in Slicer or VTK

I am not using our established pattern made available through vtkSingleton.h is needed here.

Simply instantiating and setting using the following should be sufficient:

vtkNew<vtkMRMLTranslator> translator;
this->AppLogic->SetTranslatorInstance(translator);

// Create the translation object used for string translation in logic classes
if (!this->AppLogic->GetTranslatorInstance())
{
this->AppLogic->SetTranslatorInstance(new qMRMLTranslator());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qMRMLTranslator is not a Qt class, and it is not taking care of destroying itself if it is created by new. Instead, it is a VTK class, which can be created for example by vtkNew:

vtkNew<vtkMRMLTranslator> translator;
this->AppLogic->SetTranslatorInstance(translator);

@@ -316,6 +327,8 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic
class vtkInternal;
vtkInternal* Internal;

static vtkMRMLTranslator* TranslatorInstance;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this going to be deleted?
How can you ensure that it is deleted at the right time (not too early - when something may still call it; but not too late - after the VTK leak detection mechanism already counted all the non-deleted VTK objects)?

How would we implement this correctly on Windows, Linux, and macOS? We should not reinvent this from scratch but follow a singleton pattern already used in Slicer or VTK.

Copy link
Member

@jcfr jcfr Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this particular case, I don't think we need to use vtkSingleton.h helper class.

Since the lifecycle of this class is driven by vtkMRMLApplicationLogic (itself instantiated and deleted by the main application), the reference to vtkMRMLTranslator will be released upon destruction of vtkMRMLApplicationLogic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for making a simple vtk class that is owned by the application logic.

@lassoan
Copy link
Contributor

lassoan commented Sep 2, 2023

Superseded by #7210

@lassoan lassoan closed this Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants