OpenVPN 3 Core Library
Loading...
Searching...
No Matches
intrinsic_type.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <type_traits>
15
16namespace openvpn {
17
32template <typename BaseT, typename T>
33 requires std::is_arithmetic_v<T>
35{
36 using value_type = T;
37
43 constexpr explicit IntrinsicType(value_type v = value_type()) noexcept
44 : mValue(v) {};
45
52 {
53 mValue = v;
54 return CrtpBase();
55 }
56
62 BaseT &operator=(BaseT arg) noexcept
63 {
64 mValue = arg.mValue;
65 return CrtpBase();
66 }
67
73 constexpr BaseT operator+=(BaseT arg) noexcept
74 {
75 mValue += arg.mValue;
76 return CrtpBase();
77 }
78
84 constexpr BaseT operator-=(BaseT arg) noexcept
85 {
86 mValue -= arg.mValue;
87 return CrtpBase();
88 }
89
95 constexpr BaseT operator*=(BaseT arg) noexcept
96 {
97 mValue *= arg.mValue;
98 return CrtpBase();
99 }
100
106 constexpr BaseT operator/=(BaseT arg) noexcept
107 {
108 mValue /= arg.mValue;
109 return CrtpBase();
110 }
111
119 constexpr BaseT &operator|=(BaseT arg) noexcept
120 requires std::is_integral_v<T>
121 {
122 mValue |= arg.mValue;
123 return CrtpBase();
124 }
125
133 constexpr BaseT &operator&=(BaseT arg) noexcept
134 requires std::is_integral_v<T>
135 {
136 mValue &= arg.mValue;
137 return CrtpBase();
138 }
139
144 constexpr value_type get() const noexcept
145 {
146 return mValue;
147 }
148
153 constexpr operator value_type() const noexcept
154 {
155 return mValue;
156 }
157
158 private:
163 BaseT &CrtpBase() noexcept
164 {
165 return static_cast<BaseT &>(*this);
166 }
167
172 const BaseT &CrtpBase() const noexcept
173 {
174 return static_cast<const BaseT &>(*this);
175 }
176
177 private:
179};
180
187template <typename BaseT, typename T>
188 requires std::is_arithmetic_v<T>
190{
191 return lhs.get() == rhs.get();
192}
193
200template <typename BaseT, typename T>
201 requires std::is_arithmetic_v<T>
203{
204 return lhs.get() <=> rhs.get();
205}
206
213template <typename BaseT, typename T>
214 requires std::is_arithmetic_v<T>
216{
217 return BaseT(lhs.get() + rhs.get());
218}
219
226template <typename BaseT, typename T>
227 requires std::is_arithmetic_v<T>
229{
230 return BaseT(lhs.get() - rhs.get());
231}
232
239template <typename BaseT, typename T>
240 requires std::is_arithmetic_v<T>
242{
243 return BaseT(lhs.get() * rhs.get());
244}
245
252template <typename BaseT, typename T>
253 requires std::is_arithmetic_v<T>
255{
256 return BaseT(lhs.get() / rhs.get());
257}
258
268template <typename BaseT, typename T>
269 requires std::is_integral_v<T>
270constexpr BaseT operator~(IntrinsicType<BaseT, T> t) noexcept
271{
272 return BaseT{~T(t)};
273}
274
285template <typename BaseT, typename T>
286 requires std::is_integral_v<T>
288{
289 return BaseT{T(l) | T(r)};
290}
291
302template <typename BaseT, typename T>
303 requires std::is_integral_v<T>
305{
306 return BaseT{T(l) & T(r)};
307}
308
309} // namespace openvpn
constexpr BaseT operator/(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Division operator.
constexpr BaseT operator&(IntrinsicType< BaseT, T > l, IntrinsicType< BaseT, T > r) noexcept
Performs bitwise AND operation between two IntrinsicType objects.
constexpr BaseT operator|(IntrinsicType< BaseT, T > l, IntrinsicType< BaseT, T > r) noexcept
Performs bitwise OR operation between two IntrinsicType objects.
constexpr BaseT operator-(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Subtraction operator.
constexpr BaseT operator~(IntrinsicType< BaseT, T > t) noexcept
Performs bitwise NOT operation on an IntrinsicType.
constexpr auto operator<=>(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Equality comparison operator.
constexpr BaseT operator*(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Multiplication operator.
constexpr bool operator==(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Equality comparison operator.
constexpr BaseT operator+(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Addition operator.
CRTP type designed to allow creation of strong types based on intrinsics.
constexpr BaseT operator+=(BaseT arg) noexcept
Add assignment operator.
constexpr BaseT & operator|=(BaseT arg) noexcept
Bitwise OR assignment operator.
BaseT & operator=(value_type v)
Assignment operator from value_type.
BaseT & operator=(BaseT arg) noexcept
Assignment operator from BaseT.
constexpr BaseT operator/=(BaseT arg) noexcept
Divide assignment operator.
constexpr BaseT operator-=(BaseT arg) noexcept
Subtract assignment operator.
BaseT & CrtpBase() noexcept
Helper function to perform CRTP cast.
constexpr BaseT operator*=(BaseT arg) noexcept
Multiply assignment operator.
const BaseT & CrtpBase() const noexcept
Const version of helper function to perform CRTP cast.
constexpr IntrinsicType(value_type v=value_type()) noexcept
Constructs an IntrinsicType object.
constexpr BaseT & operator&=(BaseT arg) noexcept
Bitwise AND assignment operator.
constexpr value_type get() const noexcept
Getter for the underlying value.