Skip to content

Some OpenMS classes are derived from std::vector [184] #184

Closed

Description

@timosachsenberg

Submitted by aiche on 2010-09-13 14:14:05

stl containers are generally not designed for inheritance, so why do we do it in the library?

Affected classes vor std::vector are

  • AcquisitionInfo
  • DoubleList (see a1f2f38)
  • IntList (see df9ea40)
  • StringList (see 9e1468b)
  • ConsensusMap (changed to private inheritance)
  • MSChromatogram
  • MSSpectrum
  • FeatureMap (changed to private inheritance)
  • MSExperiment (changed to member, see 1d6dea8)
  • SampleProteins (in src/openms/include/OpenMS/SIMULATION/SimTypes.h)
  • SampleChannels (in src/openms/include/OpenMS/SIMULATION/SimTypes.h)
  • MassTraces (in src/openms/include/OpenMS/TRANSFORMATIONS/FEATUREFINDER/FeatureFinderAlgorithmPickedHelperStructs.h) (see 69f58ce)
  • TextFile

Other STL containers

Activity

  1. added 5 commits that reference this issue on Jan 9, 2014
  2. added a commit that references this issue on Jan 9, 2014
  3. hroest commented on Sep 26, 2014

    @hroest
    Contributor

    Commented by aiche on 2010-09-13 14:14:51:
    updated description for better readability


    Commented by hendrikweisser on 2010-09-13 15:05:26:

    stl containers are generally not designed for inheritance, so why do we do it in the library?

    The affected classes are not intended to be used polymorphically. There is no reason to act on any of the classes through a pointer to the base class (vector<...>), let alone delete an object through a base-class pointer.

    See also [http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7].

    I'd say in this case it's okay to derive from std::vector. It may not technically be a great solution, but I'd still prefer it to the alternatives (make a Vector class just for the virtual destructor, or wrap all the vector functions in each affected class).

    By the way, Map is derived from std::map, so the same problem would apply.


    Commented by witek96 on 2010-09-15 12:06:51:
    Hendrik, you are not the only one that thinks that this is OK (you are the only one which does not count till 10 ... some others brush it under the carpet ... ). Although you tried to explain why you think it is "OK" I do not get it.

    First of all, a partial fixing of the problem is not much code at all (see below)!
    Secondly, MSspectrum is implemented "in terms of" a vector what is OK. But you should not use public inheritance to achieve it. See: http://www.gotw.ca/publications/mill06.htm.
    Now, just assume you want to change the underlying implementation, and in case of MSExperiment class this was discussed already in order to allow for buffering. You will not be able too do so because some of the calling/using code assumes ALREADY that MSSpectrum "is a" concrete vector (see for example Line 134 in LinearResampler.h).
    The less ugly part of the problem will be the already overblown interface of MSspectrum (for what the heck it needs all the vector methods?) which has already a lot of redundancies in it.

    Changing the implementation if necessary ... A NIGHTMARE.

    As I said above. Fixing the problem is not much code at all (YET)! If you derive private from std::vector you can apply the using clause (see code snippet below).

    Private inheritance will prevent that the following example code compiles, and this is exactly (IMHO) what we want to prevent happening.

    class A{};
    void foo(A & /*x*/){}
    class B:private A{};
    int main()
    {
    //this should not compile because of lack of virtual constructor in A.
    A * x = new B; //conversion to inaccessible base class "A" 
                   //is not allowed A* x = new B;
    
    B b;
    
    //this should not compile because you do not want B to used as if it is A.
    foo(b);// conversion to inaccessible base
         // class "A" is not allowed  f(b);
    }
    

    After the fix MS spectrum looks like this:

    class MSSpectrum
       : private std::vector<PeakT>,
                           public RangeManager<1>,
                           public SpectrumSettings,
                           public PersistentObject
           {
                   typedef std::vector<PeakT> privvec;
                   public:
                   using privvec::value_type;
                   using privvec::pointer;
                   using privvec::const_pointer;
        ... etc (siehe Anhang)
    

    In the remaining code it was necessary to fix Line 134 in file LinearResampler.h
    as follows:
    //resampled_peak_container.swap(spectrum);
    spectrum.assign(resampled_peak_container.begin(),resampled_peak_container.end());

    running make test and looking at the test results (see below) did not revealed problems which I could track to the changes introduced.

    Regards
    Eryk

    91% tests passed, 70 tests failed out of 801
    
    Total Test time (real) = 241.75 sec
    
    The following tests FAILED:
           377 - TOPP_WSDL_1 (SEGFAULT)
           378 - TOPP_WSDL_1_out1 (Failed)
           379 - TOPPWRITEINI_AdditiveSeries (SEGFAULT)
           402 - TOPPWRITEINI_IDRTCalibration (SEGFAULT)
           413 - TOPPWRITEINI_PILISModel (Failed)
           417 - TOPPWRITEINI_PepNovoAdapter (SEGFAULT)
           428 - TOPPWRITEINI_SequestAdapter (Not Run)
           435 - TOPPWRITEINI_SpectraMerger (SEGFAULT)
           446 - TOPPWRITEINI_IDMassAccuracy (SEGFAULT)
           461 - TOPP_AdditiveSeries_1 (SEGFAULT)
           462 - TOPP_AdditiveSeries_1_out1 (Failed)
           463 - TOPP_AdditiveSeries_1_out2 (Failed)
           464 - TOPP_AdditiveSeries_1_out3 (Failed)
           465 - TOPP_AdditiveSeries_1_out4 (Failed)
           475 - TOPP_Decharger_1_out1 (Failed)
           483 - TOPP_FeatureFinder_4_out1 (Failed)
           485 - TOPP_FeatureFinder_5_out1 (Failed)
           491 - TOPP_FileConverter_3_out1 (Failed)
           531 - TOPP_FileFilter_10_out1 (Failed)
           533 - TOPP_FileFilter_11_out1 (Failed)
           555 - TOPP_FileMerger_1_out1 (Failed)
           557 - TOPP_FileMerger_2_out1 (Failed)
           559 - TOPP_FileMerger_3_out1 (Failed)
           563 - TOPP_FileMerger_5_out1 (Failed)
           568 - TOPP_IDRTCalibration_1 (SEGFAULT)
           569 - TOPP_IDRTCalibration_1_out1 (Failed)
           577 - TOPP_IDFilter_1_out1 (Failed)
           579 - TOPP_IDFilter_2_out1 (Failed)
           581 - TOPP_IDFilter_3_out1 (Failed)
           583 - TOPP_IDFilter_4_out1 (Failed)
           585 - TOPP_IDFilter_5_out1 (Failed)
           587 - TOPP_IDFilter_6_out1 (Failed)
           589 - TOPP_IDFilter_7_out1 (Failed)
           592 - TOPP_MapAligner_1_out2 (Failed)
           595 - TOPP_MapAligner_1_out5 (Failed)
           596 - TOPP_MapAligner_1_out6 (Failed)
           599 - TOPP_MapAligner_2_out2 (Failed)
           612 - TOPP_MapAligner_6_out2 (Failed)
           625 - TOPP_MapAligner_11_out1 (Failed)
           648 - TOPP_RTModel_1_out1 (Failed)
           650 - TOPP_RTModel_2_out1 (Failed)
           654 - TOPP_RTModel_3_out1 (Failed)
           657 - TOPP_RTModel_4_out1 (Failed)
           660 - TOPP_PTModel_1_out1 (Failed)
           664 - TOPP_PTPredict_1_out1 (Failed)
           666 - TOPP_RTPredict_1_out1 (Failed)
           668 - TOPP_RTPredict_2_out1 (Failed)
           669 - TOPP_RTPredict_2_out2 (Failed)
           671 - TOPP_RTPredict_3_out1 (Failed)
           688 - TOPP_SpectraFilter_1_out1 (Failed)
           690 - TOPP_SpectraFilter_2_out1 (Failed)
           692 - TOPP_SpectraFilter_3_out1 (Failed)
           699 - TOPP_TextExporter_2_out1 (Failed)
           700 - TOPP_TextExporter_2_out2 (Failed)
           701 - TOPP_TextExporter_2_out3 (Failed)
           702 - TOPP_TextExporter_2_out4 (Failed)
           704 - TOPP_TextExporter_3_out1 (Failed)
           706 - TOPP_TextExporter_4_out1 (Failed)
           712 - TOPP_TextExporter_7_out1 (Failed)
           714 - TOPP_SILACAnalyzer_1_out1 (Failed)
           720 - TOPP_FeatureLinker_1_out1 (Failed)
           728 - TOPP_FeatureLinker_5_out1 (Failed)
           748 - TOPP_CompNovo_1_out1 (Failed)
           750 - TOPP_CompNovo_2_out1 (Failed)
           764 - TOPP_IDPosteriorErrorProbability_1_out1 (Failed)
           766 - TOPP_IDPosteriorErrorProbability_2_out1 (Failed)
           768 - TOPP_IDPosteriorErrorProbability_3_out1 (Failed)
           770 - TOPP_IDPosteriorErrorProbability_4_out1 (Failed)
           772 - TOPP_IDPosteriorErrorProbability_5_out1 (Failed)
           774 - TOPP_IDPosteriorErrorProbability_6_out1 (Failed)
    
    
    
  4. hroest commented on Sep 26, 2014

    @hroest
    Contributor

    Commented by hendrikweisser on 2010-09-15 16:05:30:
    I don't want to get into long discussions about this, because I won't be the one who has to implement any changes anyway. I'm just arguing that development time could be spent more sensibly on issues with more tangible results (there are enough tickets in the tracker).

    It looks to me like the problem in LinearResampler could be solved by redefining "resampled_peak_container" to be of type "MSSpectrum". Then you only expect MSSpectrum to provide a "swap" method and don't care where it comes from.

    Private inheritance will prevent that the following example code compiles, and this is exactly (IMHO) what we want to prevent happening.

    I think this is what our disagreement boils down to: I see no reason why such code should exist in our case, and therefore I also see no reason why we have to prevent it. (That's probably more of a Python than a C++ attitude, but I find it useful in general.)


    Commented by aiche on 2010-09-17 11:01:00:
    regarding [7388], [7387]

    I thought that we maybe should get to a generally accepted conclusion '''before''' commiting code to the head!


    Commented by witek96 on 2010-09-17 11:14:44:
    Hi Stephan,

    Sorry. You are right. My excuses is:

    • I will be away for the next two weeks.
    • I did a build yesterday on a newly checked out version. I could not reproduce any of the errors I reported previously. There were no Unit test errors and no compiler warnings.
    • Removing my commit should not be much work. If it is I am really sorry. But please before you remove my commit you should really check if it impairs anything, take a look. Probieren geht ueber studieren.

    regards


    Commented by cbielow on 2010-09-17 12:32:53:
    to add another view to the discussion:
    deriving from STL containers produces hundreds of warnings in MSVC, which I disabled using compiler flags (/wd4251 /wd4275).
    , see http://msdn.microsoft.com/en-us/library/esew7y1w.aspx and http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx.

    Bottom line: its a bad idea to derive from STL containers, as they are not (and should not) be exported to the DLL. This is also the reason why I had to remove all std::npos occurences recently, because in VS2010 MS decided to put them in different libs, and with our current setup that causes the linker not to find npos where expected (see https://connect.microsoft.com/VisualStudio/feedback/details/562448/std-string-npos-lnk2001-when-inheriting-a-dll-class-from-std-string?wa=wsignin1.0). Involved story, I don't understand it 100% myself. Again: deriving from STL containers is bad.


    Commented by aiche on 2010-09-20 09:52:59:
    I reverted the changes made in [7388], [7387] in the HEAD but they are still available in the branch

    https://open-ms.svn.sourceforge.net/svnroot/open-ms/branches/spectrum_refactoring/

    We will need some more testing of this, before we move this into the HEAD ..


    Commented by hroest on 2013-06-07 11:55:46:
    The main KERNEL classes that are affected are:

    ConsensusMap.h
    FeatureMap.h
    MSChromatogram.h
    MSSpectrum.h

    In addition we have (of course) DoubleList, StringList, IntList and in other folders

    METADATA/AcquisitionInfo.h
    SIMULATION/SimTypes.h
    SIMULATION/SimTypes.h
    TRANSFORMATIONS/FEATUREFINDER/FeatureFinderAlgorithmPickedHelperStructs.h (MassTraces)


    Commented by hroest on 2013-06-28 16:31:05:
    r11403 to r11407 remove the inheritance from FeatureMap and ConsensusMap, thus all large containers are now mostly free std::vector inheritance and MSChromatogram.h and MSSpectrum.h are left.


    Commented by cbielow on 2013-07-12 12:26:52:
    augmented for ConsensusFeatures derived from std::set in [11539]


    Commented by witek96 on 2013-10-08 09:43:03:
    include/OpenMS/DATASTRUCTURES/String.h

    class String :
    public std::string
    {
    public:

    std::string, that is, basically the whole basic_string template is not designed to be derived from. There are zillions of articles about that already. It doesn't have any virtual functions so there is nothing to override. The best you can do is hide something. Best is to use composition/aggregation! That is, just keep a member of type string in your class and forward the calls! Again, just to make sure


    Commented by witek96 on 2013-10-25 10:41:47:
    Regarding the String class.
    Open possible approach would be:
    a) start moving the methods from String into a StringUtility as i.e. static members.
    b) remove this methods from the interface of the String class.
    c) when no other method than the std::string methods are left in the String class remove this class from openms.

    To summarize, instead of providing the functionality which is in String through inheritance, implement StringUtility class, and remove the String class from OpenMS.

  5. added a commit that references this issue on Oct 5, 2014
  6. added and removed
    major
    on Apr 28, 2017
  7. hroest commented on Apr 28, 2017

    @hroest
    Contributor

    seems like mostly resolved except for OpenMS::String

  8. hendrikweisser commented on Mar 27, 2019

    @hendrikweisser
    Contributor

    It seems that all the vector cases are handled. We have #448 still open to discuss the issues related to String. I'm closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions