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, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
34{
35 using value_type = T;
36
42 constexpr explicit IntrinsicType(value_type v = value_type()) noexcept
43 : mValue(v) {};
44
51 {
52 mValue = v;
53 return CrtpBase();
54 }
55
62 friend constexpr bool operator==(const BaseT lhs, const BaseT rhs) noexcept
63 {
64 return lhs.mValue == rhs.mValue;
65 }
66
72 BaseT &operator=(BaseT arg) noexcept
73 {
74 mValue = arg.mValue;
75 return CrtpBase();
76 }
77
85 template <typename = std::enable_if_t<std::is_integral_v<T>>>
86 constexpr BaseT &operator|=(BaseT arg) noexcept
87 {
88 mValue |= arg.mValue;
89 return CrtpBase();
90 }
91
99 template <typename = std::enable_if_t<std::is_integral_v<T>>>
100 constexpr BaseT &operator&=(BaseT arg) noexcept
101 {
102 mValue &= arg.mValue;
103 return CrtpBase();
104 }
105
110 constexpr value_type get() const noexcept
111 {
112 return mValue;
113 }
114
119 constexpr operator value_type() const noexcept
120 {
121 return mValue;
122 }
123
124 private:
129 BaseT &CrtpBase() noexcept
130 {
131 return static_cast<BaseT &>(*this);
132 }
133
138 const BaseT &CrtpBase() const noexcept
139 {
140 return static_cast<const BaseT &>(*this);
141 }
142
143 private:
145};
146
156template <typename BaseT, typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
157constexpr BaseT operator~(IntrinsicType<BaseT, T> t) noexcept
158{
159 return BaseT{~T(t)};
160}
161
172template <typename BaseT, typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
174{
175 return BaseT{T(l) | T(r)};
176}
177
188template <typename BaseT, typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
190{
191 return BaseT{T(l) & T(r)};
192}
193
194} // namespace openvpn
constexpr BaseT operator~(IntrinsicType< BaseT, T > t) noexcept
Performs bitwise NOT operation on an IntrinsicType.
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.
CRTP type designed to allow creation of strong types based on intrinsics.
BaseT & operator=(BaseT arg) noexcept
Assignment operator from BaseT.
constexpr BaseT & operator&=(BaseT arg) noexcept
Bitwise AND assignment operator.
BaseT & CrtpBase() noexcept
Helper function to perform CRTP cast.
constexpr IntrinsicType(value_type v=value_type()) noexcept
Constructs an IntrinsicType object.
friend constexpr bool operator==(const BaseT lhs, const BaseT rhs) noexcept
Equality operator - class friend.
constexpr BaseT & operator|=(BaseT arg) noexcept
Bitwise OR assignment operator.
const BaseT & CrtpBase() const noexcept
Const version of helper function to perform CRTP cast.
constexpr value_type get() const noexcept
Getter for the underlying value.
BaseT & operator=(value_type v)
Assignment operator from value_type.