Mir
Loading...
Searching...
No Matches
point_generic.h
Go to the documentation of this file.
1/*
2 * Copyright © 2020 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2 or 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef MIR_GEOMETRY_POINT_GENERIC_H_
18#define MIR_GEOMETRY_POINT_GENERIC_H_
19
20#include "dimensions_generic.h"
21#include <ostream>
22
23namespace mir
24{
25namespace geometry
26{
27namespace detail
28{
29struct PointBase{};
30}
31namespace generic
32{
33template<template<typename> typename T>
34struct Size;
35template<template<typename> typename T>
36struct Displacement;
37
38template<template<typename> typename T>
40{
41 template<typename Tag>
42 using Corresponding = T<Tag>;
43
46
47 constexpr Point() = default;
48 constexpr Point(Point const&) = default;
49 Point& operator=(Point const&) = default;
50
51 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
52 explicit constexpr Point(P const& other) noexcept
53 : x{T<XTag>{other.x}},
54 y{T<YTag>{other.y}}
55 {
56 }
57
58 template<typename XType, typename YType>
59 constexpr Point(XType&& x, YType&& y) : x(x), y(y) {}
60
61 T<XTag> x;
62 T<YTag> y;
63};
64
65template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
66inline constexpr bool operator == (P const& lhs, P const& rhs)
67{
68 return lhs.x == rhs.x && lhs.y == rhs.y;
69}
70
71template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
72inline constexpr bool operator != (P const& lhs, P const& rhs)
73{
74 return lhs.x != rhs.x || lhs.y != rhs.y;
75}
76
77template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
78inline constexpr P operator+(P lhs, Corresponding<P, DeltaXTag> rhs) { return{lhs.x + rhs, lhs.y}; }
79template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
80inline constexpr P operator+(P lhs, Corresponding<P, DeltaYTag> rhs) { return{lhs.x, lhs.y + rhs}; }
81
82template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
83inline constexpr P operator-(P lhs, Corresponding<P, DeltaXTag> rhs) { return{lhs.x - rhs, lhs.y}; }
84template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
85inline constexpr P operator-(P lhs, Corresponding<P, DeltaYTag> rhs) { return{lhs.x, lhs.y - rhs}; }
86
87template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
88inline P& operator+=(P& lhs, Corresponding<P, DeltaXTag> rhs) { lhs.x += rhs; return lhs; }
89template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
90inline P& operator+=(P& lhs, Corresponding<P, DeltaYTag> rhs) { lhs.y += rhs; return lhs; }
91
92template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
93inline P& operator-=(P& lhs, Corresponding<P, DeltaXTag> rhs) { lhs.x -= rhs; return lhs; }
94template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
95inline P& operator-=(P& lhs, Corresponding<P, DeltaYTag> rhs) { lhs.y -= rhs; return lhs; }
96
97template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
98std::ostream& operator<<(std::ostream& out, P const& value)
99{
100 out << value.x << ", " << value.y;
101 return out;
102}
103
104}
105}
106}
107
108#endif // MIR_GEOMETRY_POINT_GENERIC_H_
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
constexpr D operator-(D const &lhs, D const &rhs)
Definition: displacement_generic.h:94
typename GeometricType::template Corresponding< Tag > Corresponding
Definition: dimensions_generic.h:139
constexpr bool operator!=(D const &lhs, D const &rhs)
Definition: displacement_generic.h:75
constexpr D operator+(D const &lhs, D const &rhs)
Definition: displacement_generic.h:88
constexpr bool operator==(D const &lhs, D const &rhs)
Definition: displacement_generic.h:69
constexpr D::PointType & operator+=(typename D::PointType &lhs, D const &rhs)
Definition: displacement_generic.h:130
constexpr D::PointType & operator-=(typename D::PointType &lhs, D const &rhs)
Definition: displacement_generic.h:136
Definition: splash_session.h:22
Definition: displacement.h:30
Definition: size.h:30
Used for determining if a type is a point.
Definition: point_generic.h:29
Definition: displacement_generic.h:43
Definition: point_generic.h:40
T< XTag > x
Definition: point_generic.h:61
T< Tag > Corresponding
Definition: point_generic.h:42
T< YTag > y
Definition: point_generic.h:62
constexpr Point(P const &other) noexcept
Definition: point_generic.h:52
Point & operator=(Point const &)=default
constexpr Point()=default
constexpr Point(Point const &)=default
constexpr Point(XType &&x, YType &&y)
Definition: point_generic.h:59
Definition: size_generic.h:41

Copyright © 2012-2023 Canonical Ltd.
Generated on Tue Jan 24 16:42:22 UTC 2023
This documentation is licensed under the GPL version 2 or 3.