2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2014-08-19 21:08:16 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_COORD_H_
|
|
|
|
#define MOZILLA_GFX_COORD_H_
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2015-06-11 23:43:40 +03:00
|
|
|
#include "mozilla/TypeTraits.h" // For IsSame
|
2014-08-19 21:08:16 +04:00
|
|
|
#include "Types.h"
|
|
|
|
#include "BaseCoord.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename> struct IsPixel;
|
|
|
|
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
template <class units> struct IntCoordTyped;
|
2015-06-11 23:43:40 +03:00
|
|
|
template <class units, class F = Float> struct CoordTyped;
|
2014-08-19 21:08:16 +04:00
|
|
|
|
|
|
|
// CommonType<coord, primitive> is a metafunction that returns the type of the
|
|
|
|
// result of an arithmetic operation on the underlying type of a strongly-typed
|
|
|
|
// coordinate type 'coord', and a primitive type 'primitive'. C++ rules for
|
|
|
|
// arithmetic conversions are designed to avoid losing information - for
|
|
|
|
// example, the result of adding an int and a float is a float - and we want
|
|
|
|
// the same behaviour when mixing our coordinate types with primitive types.
|
|
|
|
// We get C++ to compute the desired result type using 'decltype'.
|
|
|
|
|
|
|
|
template <class coord, class primitive>
|
|
|
|
struct CommonType;
|
|
|
|
|
|
|
|
template <class units, class primitive>
|
|
|
|
struct CommonType<IntCoordTyped<units>, primitive> {
|
|
|
|
typedef decltype(int32_t() + primitive()) type;
|
|
|
|
};
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
template <class units, class F, class primitive>
|
|
|
|
struct CommonType<CoordTyped<units, F>, primitive> {
|
|
|
|
typedef decltype(F() + primitive()) type;
|
2014-08-19 21:08:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a base class that provides mixed-type operator overloads between
|
|
|
|
// a strongly-typed Coord and a primitive value. It is needed to avoid
|
|
|
|
// ambiguities at mixed-type call sites, because Coord classes are implicitly
|
|
|
|
// convertible to their underlying value type. As we transition more of our code
|
|
|
|
// to strongly-typed classes, we may be able to remove some or all of these
|
|
|
|
// overloads.
|
2015-06-11 23:43:40 +03:00
|
|
|
|
|
|
|
template <bool B, class coord, class primitive>
|
2014-08-19 21:08:16 +04:00
|
|
|
struct CoordOperatorsHelper {
|
2015-06-11 23:43:40 +03:00
|
|
|
// Using SFINAE (Substitution Failure Is Not An Error) to suppress redundant
|
|
|
|
// operators
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class coord, class primitive>
|
|
|
|
struct CoordOperatorsHelper<true, coord, primitive> {
|
2014-08-19 21:08:16 +04:00
|
|
|
friend bool operator==(coord aA, primitive aB) {
|
|
|
|
return aA.value == aB;
|
|
|
|
}
|
|
|
|
friend bool operator==(primitive aA, coord aB) {
|
|
|
|
return aA == aB.value;
|
|
|
|
}
|
|
|
|
friend bool operator!=(coord aA, primitive aB) {
|
|
|
|
return aA.value != aB;
|
|
|
|
}
|
|
|
|
friend bool operator!=(primitive aA, coord aB) {
|
|
|
|
return aA != aB.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef typename CommonType<coord, primitive>::type result_type;
|
|
|
|
|
|
|
|
friend result_type operator+(coord aA, primitive aB) {
|
|
|
|
return aA.value + aB;
|
|
|
|
}
|
|
|
|
friend result_type operator+(primitive aA, coord aB) {
|
|
|
|
return aA + aB.value;
|
|
|
|
}
|
|
|
|
friend result_type operator-(coord aA, primitive aB) {
|
|
|
|
return aA.value - aB;
|
|
|
|
}
|
|
|
|
friend result_type operator-(primitive aA, coord aB) {
|
|
|
|
return aA - aB.value;
|
|
|
|
}
|
|
|
|
friend result_type operator*(coord aCoord, primitive aScale) {
|
|
|
|
return aCoord.value * aScale;
|
|
|
|
}
|
|
|
|
friend result_type operator*(primitive aScale, coord aCoord) {
|
|
|
|
return aScale * aCoord.value;
|
|
|
|
}
|
|
|
|
friend result_type operator/(coord aCoord, primitive aScale) {
|
|
|
|
return aCoord.value / aScale;
|
|
|
|
}
|
|
|
|
// 'scale / coord' is intentionally omitted because it doesn't make sense.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: 'IntCoordTyped<units>' and 'CoordTyped<units>' do not derive from
|
|
|
|
// 'units' to work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61959.
|
|
|
|
|
|
|
|
template<class units>
|
|
|
|
struct IntCoordTyped :
|
|
|
|
public BaseCoord< int32_t, IntCoordTyped<units> >,
|
2015-06-11 23:43:40 +03:00
|
|
|
public CoordOperatorsHelper< true, IntCoordTyped<units>, float >,
|
|
|
|
public CoordOperatorsHelper< true, IntCoordTyped<units>, double > {
|
2014-08-19 21:08:16 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
|
|
|
typedef BaseCoord< int32_t, IntCoordTyped<units> > Super;
|
|
|
|
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr IntCoordTyped() : Super() {}
|
|
|
|
constexpr MOZ_IMPLICIT IntCoordTyped(int32_t aValue) : Super(aValue) {}
|
2014-08-19 21:08:16 +04:00
|
|
|
};
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
template<class units, class F>
|
2014-08-19 21:08:16 +04:00
|
|
|
struct CoordTyped :
|
2015-06-11 23:43:40 +03:00
|
|
|
public BaseCoord< F, CoordTyped<units, F> >,
|
|
|
|
public CoordOperatorsHelper< !IsSame<F, int32_t>::value, CoordTyped<units, F>, int32_t >,
|
|
|
|
public CoordOperatorsHelper< !IsSame<F, uint32_t>::value, CoordTyped<units, F>, uint32_t >,
|
|
|
|
public CoordOperatorsHelper< !IsSame<F, double>::value, CoordTyped<units, F>, double >,
|
|
|
|
public CoordOperatorsHelper< !IsSame<F, float>::value, CoordTyped<units, F>, float > {
|
2014-08-19 21:08:16 +04:00
|
|
|
static_assert(IsPixel<units>::value,
|
|
|
|
"'units' must be a coordinate system tag");
|
|
|
|
|
2015-06-11 23:43:40 +03:00
|
|
|
typedef BaseCoord< F, CoordTyped<units, F> > Super;
|
2014-08-19 21:08:16 +04:00
|
|
|
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr CoordTyped() : Super() {}
|
|
|
|
constexpr MOZ_IMPLICIT CoordTyped(F aValue) : Super(aValue) {}
|
|
|
|
explicit constexpr CoordTyped(const IntCoordTyped<units>& aCoord) : Super(F(aCoord.value)) {}
|
2014-08-19 21:08:16 +04:00
|
|
|
|
|
|
|
void Round() {
|
|
|
|
this->value = floor(this->value + 0.5);
|
|
|
|
}
|
|
|
|
void Truncate() {
|
|
|
|
this->value = int32_t(this->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntCoordTyped<units> Rounded() const {
|
|
|
|
return IntCoordTyped<units>(int32_t(floor(this->value + 0.5)));
|
|
|
|
}
|
|
|
|
IntCoordTyped<units> Truncated() const {
|
|
|
|
return IntCoordTyped<units>(int32_t(this->value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2014-08-19 21:08:16 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_COORD_H_ */
|