Make wxRescaleCoord() safer by requiring explicitly using From/To
This should help with accidentally exchanging the order of parameters and makes the code using this function more readable. No real changes.
This commit is contained in:
@@ -18,26 +18,98 @@
|
||||
#include "wx/msw/wrapwin.h"
|
||||
#endif
|
||||
|
||||
// wxRescaleCoord is used to scale the components of the given wxSize by the
|
||||
// ratio between 2 scales, with rounding. It doesn't not scale the components
|
||||
// if they're set to -1 (wxDefaultCoord), as this value is special in wxSize.
|
||||
//
|
||||
// The way it's used is special because we want to ensure there is no confusion
|
||||
// between the scale being converted from and the scale being converted to, so
|
||||
// instead of just using a single function, we use an intermediate object,
|
||||
// which is not supposed to be used directly, but is only returned by From() in
|
||||
// order to allow calling To() on it.
|
||||
//
|
||||
// Another complication is that we want this to work for both wxSize and
|
||||
// wxPoint, so wxRescaleCoord() is a overloaded and the helper classes are
|
||||
// templates, with their template parameter being either one or the other.
|
||||
|
||||
// Scale the given value by the ratio between 2 other values, with rounding.
|
||||
// Do not scale the value if it's -1, just return it unchanged in this case.
|
||||
inline int wxRescaleCoord(int n, int newScale, int oldScale)
|
||||
namespace wxPrivate
|
||||
{
|
||||
return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale);
|
||||
|
||||
template <typename T> class wxRescaleCoordWithValue;
|
||||
|
||||
template <typename T>
|
||||
class wxRescaleCoordWithFrom
|
||||
{
|
||||
public:
|
||||
T To(wxSize newScale) const
|
||||
{
|
||||
T value(m_value);
|
||||
|
||||
if ( value.x != wxDefaultCoord )
|
||||
value.x = wxMulDivInt32(value.x, newScale.x, m_oldScale.x);
|
||||
|
||||
if ( value.y != wxDefaultCoord )
|
||||
value.y = wxMulDivInt32(value.y, newScale.y, m_oldScale.y);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
T To(int newScaleX, int newScaleY) const
|
||||
{
|
||||
return To(wxSize(newScaleX, newScaleY));
|
||||
}
|
||||
|
||||
private:
|
||||
wxRescaleCoordWithFrom(T value, wxSize oldScale)
|
||||
: m_value(value), m_oldScale(oldScale)
|
||||
{
|
||||
}
|
||||
|
||||
const T m_value;
|
||||
const wxSize m_oldScale;
|
||||
|
||||
// Only it can create objects of this class.
|
||||
friend wxRescaleCoordWithValue<T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class wxRescaleCoordWithValue
|
||||
{
|
||||
public:
|
||||
explicit wxRescaleCoordWithValue(T value)
|
||||
: m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
wxRescaleCoordWithFrom<T> From(wxSize oldScale)
|
||||
{
|
||||
return wxRescaleCoordWithFrom<T>(m_value, oldScale);
|
||||
}
|
||||
|
||||
wxRescaleCoordWithFrom<T> From(int oldScaleX, int oldScaleY)
|
||||
{
|
||||
return From(wxSize(oldScaleX, oldScaleY));
|
||||
}
|
||||
|
||||
private:
|
||||
const T m_value;
|
||||
};
|
||||
|
||||
} // namespace wxPrivate
|
||||
|
||||
inline wxPrivate::wxRescaleCoordWithValue<wxSize> wxRescaleCoord(wxSize sz)
|
||||
{
|
||||
return wxPrivate::wxRescaleCoordWithValue<wxSize>(sz);
|
||||
}
|
||||
|
||||
inline wxPoint
|
||||
wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale)
|
||||
inline wxPrivate::wxRescaleCoordWithValue<wxSize> wxRescaleCoord(int x, int y)
|
||||
{
|
||||
return wxPoint(wxRescaleCoord(pt.x, newScale.x, oldScale.x),
|
||||
wxRescaleCoord(pt.y, newScale.y, oldScale.y));
|
||||
return wxPrivate::wxRescaleCoordWithValue<wxSize>(wxSize(x, y));
|
||||
}
|
||||
|
||||
inline wxSize
|
||||
wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale)
|
||||
inline wxPrivate::wxRescaleCoordWithValue<wxPoint> wxRescaleCoord(wxPoint pt)
|
||||
{
|
||||
return wxSize(wxRescaleCoord(sz.x, newScale.x, oldScale.x),
|
||||
wxRescaleCoord(sz.y, newScale.y, oldScale.y));
|
||||
return wxPrivate::wxRescaleCoordWithValue<wxPoint>(pt);
|
||||
}
|
||||
|
||||
#endif // _WX_PRIVATE_RESCALE_H_
|
||||
|
Reference in New Issue
Block a user