The original motivation is to initialize the change_ member but do it only once.
This fixes coverity issue 23432.
/////////////////////////////////////////////////////////////////////////
ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
/////////////////////////////////////////////////////////////////////////
ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
- : Changer<FontInfo, FontShape>(font)
+ : Changer<FontInfo, FontShape>(font, font.shape())
{ 0, 0, -3, -5 },
{ 3, 3, 0, -2 },
{ 5, 5, 2, 0 } };
{ 0, 0, -3, -5 },
{ 3, 3, 0, -2 },
{ 5, 5, 2, 0 } };
int t = diff[mb.style][style];
if (t > 0)
while (t--)
int t = diff[mb.style][style];
if (t > 0)
while (t--)
: Changer<MetricsBase>(mb), change_(really_change_font)
{
if (change_) {
: Changer<MetricsBase>(mb), change_(really_change_font)
{
if (change_) {
FontSize oldsize = save_.font.size();
ColorCode oldcolor = save_.font.color();
docstring const oldname = from_ascii(save_.fontname);
FontSize oldsize = save_.font.size();
ColorCode oldcolor = save_.font.color();
docstring const oldname = from_ascii(save_.fontname);
: Changer<MetricsBase>(mb), change_(really_change_font)
{
if (change_) {
: Changer<MetricsBase>(mb), change_(really_change_font)
{
if (change_) {
FontSize oldsize = save_.font.size();
ColorCode oldcolor = save_.font.color();
docstring const oldname = from_ascii(save_.fontname);
FontSize oldsize = save_.font.size();
ColorCode oldcolor = save_.font.color();
docstring const oldname = from_ascii(save_.fontname);
WidthChanger::WidthChanger(MetricsBase & mb, int w)
: Changer<MetricsBase>(mb)
{
WidthChanger::WidthChanger(MetricsBase & mb, int w)
: Changer<MetricsBase>(mb)
{
ColorChanger::ColorChanger(FontInfo & font, ColorCode color,
bool really_change_color)
ColorChanger::ColorChanger(FontInfo & font, ColorCode color,
bool really_change_color)
- : Changer<FontInfo, ColorCode>(font), change_(really_change_color)
+ : Changer<FontInfo, ColorCode>(font, font.color()), change_(really_change_color)
font.setColor(color);
}
}
font.setColor(color);
}
}
class TextMetricsInfo {};
class TextMetricsInfo {};
-/// Generic base for temporarily changing things.
-/// The original state gets restored when the Changer is destructed.
+/// Generic base for temporarily changing things. The derived class is
+/// responsible for restoring the original state when the Changer is
+/// destructed.
template <class Struct, class Temp = Struct>
class Changer {
template <class Struct, class Temp = Struct>
class Changer {
-public:
- ///
- Changer(Struct & orig) : orig_(orig) {}
+ ///
+ Changer(Struct & orig, Temp const & save) : orig_(orig), save_(save) {}
+ ///
+ Changer(Struct & orig) : orig_(orig), save_(orig) {}