You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Some OpenMS classes are derived from std::vector [184] #184
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.
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.
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.
Activity
[FIX-#184] FeatureMap: private inheritance from std::vector
[FIX-#184] Feature Map: remove unused functions from the interface
[FIX-#184] FeatureMap: replace front() with begin()
[FIX-#184] ConsensusMap: private inheritance from std::vector
[FIX-#184] ConsensusMap: remove unused functions from the interface
[FIX-#184, TEST] ConsensusFeature does not inherit from std::set anym…
hroest commentedon Sep 26, 2014
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:
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.
After the fix MS spectrum looks like this:
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
hroest commentedon Sep 26, 2014
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.
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:
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.
[FIX] removed public inheritance from FFAlgoPickedHelperStructs::Mass…
[FIX] removed public inheritance from FFAlgoPickedHelperStructs::Mass…
Merge pull request #1029 from aiche/feature/remove-public-vector-inhe…
hroest commentedon Apr 28, 2017
seems like mostly resolved except for
OpenMS::Stringhendrikweisser commentedon Mar 27, 2019
It seems that all the
vectorcases are handled. We have #448 still open to discuss the issues related toString. I'm closing this ticket.[FIX] CI: restore contrib submodule pointer (unbreak Windows build)
New CI Build/Release Workflows (#9523)