ValueBase classes "enhancements"
Submitted by José Alburquerque
Link to original bug (#512717)
Description
Working on the GStreamermm Structure class, I noticed that it has setters and getters that set and return ValueBase objects for values which the underlying GstStructure (from GStreamer) contains.
When looking at the getters, I thought that it'd be nice if these getters simply returned a ValueBase instead of accepting a reference to the ValueBase (in which the value is returned) and then returning a boolean telling if the getters are successful in fetching the value.
To this end, I thought it'd really be nice if ValueBases could have a boolean like operator telling if it has been validly set. I did some exploring and found an interesting link explaining how to implement safe boolean operators (http://www.artima.com/cppsource/safebool3.html) and adapted some of this explanation to implement a boolean operator for the ValueBase class.
I also thought that making the ValueBase classes "semi-automatic" so that they can be readily assigned to on declaration and in the course of a program without having to initialize them before hand would be kind of handy, so in the "set()" methods, I added two lines of code to make sure the Value class is initialized before being set. I made all the ValueBase class descendants except for the Value_Enum and Value_Flags semi-automatic because I found that these last two cannot be automatically initialized.
The semi-automatic mechanism installed in the ValueBase classes does not affect code that already use the ValueBase class. It is only triggered when setting ValueBase classes that have not been initialized. If one is declared but never initialized or set, warnings are generated as usual and initialization before usage works just as it has before, so previous code never even notices the difference.
Lastly, I implemented cast and equal operators for easy syntactic usage in expressions. The cast operators are very useful for using these Values as if they were, in fact, the actual type that they "wrap". The pointer types also have dereferencing operators while the RefPtr types only have the member referencing (->) operator allowing similar RefPtr access to the underlying class of the RefPtr (just as a RefPtr would).
I did find ambiguity problems with the stream insertion operator (<<) and the Value classes. This is because a boolean operator is included in the ValueBase class while the descendants have other cast operators that can be used in the same context.
Overall, however, I think that these changes would be handy to enhance the usage of the ValueBase classes so I was inclined to submit a patch. However, I fully understand that there probably are an infinite number of things that I don't have perspective on, so submitting this patch I fully understand that it can be outright rejected because of something I haven't even considered. All in all, if it is rejected, would be possible to keep the ValueBase operator? I sincerely hope that some of this might be useful. Thanks.
Version: 2.15.x