Mir
Loading...
Searching...
No Matches
size_generic.h
Go to the documentation of this file.
1/*
2 * Copyright © 2021 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_SIZE_GENERIC_H_
18#define MIR_GEOMETRY_SIZE_GENERIC_H_
19
20#include "dimensions_generic.h"
21#include <ostream>
22
23namespace mir
24{
25namespace geometry
26{
27namespace detail
28{
29struct PointBase;
30struct SizeBase{};
31}
32namespace generic
33{
34template<template<typename> typename T>
35struct Point;
36template<template<typename> typename T>
37struct Displacement;
38
39template<template<typename> typename T>
41{
42 template<typename Tag>
43 using Corresponding = T<Tag>;
44
47
48 constexpr Size() noexcept {}
49 constexpr Size(Size const&) noexcept = default;
50 Size& operator=(Size const&) noexcept = default;
51
52 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
53 explicit constexpr Size(S const& other) noexcept
54 : width{T<WidthTag>{other.width}},
55 height{T<HeightTag>{other.height}}
56 {
57 }
58
59 template<typename WidthType, typename HeightType>
60 constexpr Size(WidthType&& width, HeightType&& height) noexcept : width(width), height(height) {}
61
62 T<WidthTag> width;
63 T<HeightTag> height;
64};
65
66template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
67inline constexpr bool operator == (S const& lhs, S const& rhs)
68{
69 return lhs.width == rhs.width && lhs.height == rhs.height;
70}
71
72template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
73inline constexpr bool operator != (S const& lhs, S const& rhs)
74{
75 return lhs.width != rhs.width || lhs.height != rhs.height;
76}
77
78template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
79std::ostream& operator<<(std::ostream& out, S const& value)
80{
81 out << '(' << value.width << ", " << value.height << ')';
82 return out;
83}
84
85template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
86inline constexpr S operator*(Scalar scale, S const& size)
87{
88 return S{scale*size.width, scale*size.height};
89}
90
91template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
92inline constexpr S operator*(S const& size, Scalar scale)
93{
94 return scale*size;
95}
96
97template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
98inline constexpr S operator/(S const& size, Scalar scale)
99{
100 return S{size.width / scale, size.height / scale};
101}
102
103template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
104inline constexpr typename P::SizeType as_size(P const& point)
105{
106 return typename P::SizeType{point.x.as_value(), point.y.as_value()};
107}
108
109template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
110inline constexpr typename S::PointType as_point(S const& size)
111{
112 return typename S::PointType{size.width.as_value(), size.height.as_value()};
113}
114}
115}
116}
117
118#endif // MIR_GEOMETRY_SIZE_GENERIC_H_
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
constexpr S operator/(S const &size, Scalar scale)
Definition: size_generic.h:98
constexpr D::PointType as_point(D const &disp)
Definition: displacement_generic.h:178
constexpr bool operator!=(D const &lhs, D const &rhs)
Definition: displacement_generic.h:75
constexpr D::SizeType as_size(D const &disp)
Definition: displacement_generic.h:166
constexpr D operator*(Scalar scale, D const &disp)
Definition: displacement_generic.h:148
constexpr bool operator==(D const &lhs, D const &rhs)
Definition: displacement_generic.h:69
Definition: splash_session.h:22
Definition: displacement.h:30
Definition: point.h:30
Used for determining if a type is a size.
Definition: size_generic.h:30
Definition: displacement_generic.h:43
Definition: point_generic.h:40
Definition: size_generic.h:41
T< Tag > Corresponding
Definition: size_generic.h:43
T< WidthTag > width
Definition: size_generic.h:62
Size & operator=(Size const &) noexcept=default
constexpr Size() noexcept
Definition: size_generic.h:48
constexpr Size(S const &other) noexcept
Definition: size_generic.h:53
constexpr Size(WidthType &&width, HeightType &&height) noexcept
Definition: size_generic.h:60
T< HeightTag > height
Definition: size_generic.h:63
constexpr Size(Size const &) noexcept=default

Copyright © 2012-2022 Canonical Ltd.
Generated on Tue Nov 8 21:37:30 UTC 2022
This documentation is licensed under the GPL version 2 or 3.