[core] WriteObject overload for TObject-derived classes#8934
[core] WriteObject overload for TObject-derived classes#8934vepadulano merged 1 commit intoroot-project:masterfrom
Conversation
|
Starting build on |
|
Build failed on ROOT-ubuntu16/nortcxxmod. Errors:
|
|
Build failed on ROOT-debian10-i386/cxx14. Errors:
|
|
Build failed on mac11.0/cxx17. Errors:
|
|
Build failed on ROOT-performance-centos8-multicore/default. |
|
Build failed on mac1014/python3. Errors:
|
pcanal
left a comment
There was a problem hiding this comment.
Approved pending test improvement
704d6c5 to
c3d73fa
Compare
|
Starting build on |
|
Build failed on ROOT-performance-centos8-multicore/default. Failing tests: |
|
Build failed on ROOT-ubuntu16/nortcxxmod. Failing tests: |
|
Build failed on ROOT-debian10-i386/cxx14. Failing tests: |
|
Build failed on mac11.0/cxx17. Failing tests: |
|
Build failed on mac1014/python3. Failing tests: |
|
Build failed on windows10/cxx14. Failing tests: |
|
Following Philippe's suggestion for the test (thanks!) it's clear that the current logic is not enough. |
|
Starting build on |
|
Build failed on mac11.0/cxx17. Failing tests: |
|
Starting build on |
|
Build failed on mac11.0/cxx17. Failing tests: |
The [TDirectory::WriteObject](https://github.com/root-project/root/blob/35b5aaef38b6635e131e7d93a0c96f69bb293b9d/core/base/inc/TDirectory.h#L265-L268) method allows writing objects to files. If the written object actually has a title, this would be discarded because the function doesn't manage it as a TObject-derived instance on purpose. For example, the program below: ```cpp int main(){ TFile f{"myfile.root","recreate"}; TH1F h{"myhistoname","myhistotitle",100,0,100}; f.WriteObject(&h, h.GetName()); f.Close(); } ``` When executed creates a file where the object "h" gets the default title "object title": ```bash $ rootls -l myfile.root TH1F Aug 21 10:41 2021 myhistoname;1 "object title" ``` This is because The [TKey constructor that accepts a void pointer](https://github.com/root-project/root/blob/35b5aaef38b6635e131e7d93a0c96f69bb293b9d/io/io/src/TKey.cxx#L299-L300) calls the parent TNamed constructor with a default title, because in general there is no guarantee the object has the interface `GetTitle(),SetTitle()` and there is no extra "title" parameter to the constructor. This commit provides a solution by creating a new overload for `TDirectory::WriteObject`, using SFINAE to make it available for types that are derived from TObject. The method redirects to `WriteTObject` instead of `WriteObjectAny`. This way, the correct TKey constructor is called that uses the actual object title. As a result, the example above will now output a file like this: ``` $ rootls -l myfile.root TH1F Aug 21 11:00 2021 myhistoname;1 "myhistotitle" ``` The already present method is modified with SFINAE as well, to only be available if the type T of the template is not derived from TObject.
911b9de to
6ba1641
Compare
|
Starting build on |
|
Updated docs, run clang-tidy on the modified file and squashed to one commit. If tests pass I will merge |
|
Build failed on mac11.0/cxx17. Failing tests: |
The TDirectory::WriteObject method allows writing objects to files. If the written object actually has a title, this should be discarded because the function doesn't manage it as a TObject-derived instance on purpose. For example, the program below:
When executed creates a file where the object "h" gets the default title "object title":
This is because The TKey constructor that accepts a void pointer calls the parent TNamed constructor with a default title, because in general there is no guarantee the object has the interface
GetTitle(),SetTitle()and there is no extra "title" parameter to the constructor.This commit provides a solution by creating a new overload for
TDirectory::WriteObject, using SFINAE to make it available for types that are derived from TObject. The method redirects toWriteTObjectinstead ofWriteObjectAny. This way, the correct TKey constructor is called that uses the actual object title. As a result, the example above will now output a file like this:The already present method is modified with SFINAE as well, to only be available if the type T of the template is not derived from TObject.