13#include <userver/compiler/impl/lifetime.hpp>
14#include <userver/formats/common/meta.hpp>
15#include <userver/utils/fmt_compat.hpp>
16#include <userver/utils/meta.hpp>
17#include <userver/utils/strong_typedef_fwd.hpp>
18#include <userver/utils/underlying_value.hpp>
21#include <fmt/format.h>
22#include <boost/functional/hash_fwd.hpp>
25namespace boost::
uuids {
33std::string PrintToString(
const T& value);
37USERVER_NAMESPACE_BEGIN
45constexpr bool operator&(StrongTypedefOps op, StrongTypedefOps mask)
noexcept {
49constexpr auto operator|(StrongTypedefOps op1, StrongTypedefOps op2)
noexcept {
54namespace impl::strong_typedef {
57struct InitializerListImpl {
59 using type = DoNotMatch;
63requires requires {
typename T::value_type; }
64struct InitializerListImpl<T> {
65 using type = std::initializer_list<
typename T::value_type>;
71using InitializerList =
typename InitializerListImpl<T>::type;
73template <
class T,
class U>
74constexpr void CheckStrongCompare() {
76 std::is_same_v<
typename T::UnderlyingType,
typename U::UnderlyingType> &&
77 std::is_same_v<
typename T::TagType,
typename U::TagType> && T::kOps == U::kOps &&
78 (T::kOps & StrongTypedefOps::kCompareStrong),
79 "Comparing those StrongTypedefs is forbidden"
83template <
class Typedef>
84constexpr void CheckTransparentCompare() {
86 Typedef::kOps & StrongTypedefOps::kCompareTransparentOnly,
87 "Comparing this StrongTypedef to a raw value is forbidden"
92const auto& UnwrapIfStrongTypedef(
const T& value) {
93 if constexpr (IsStrongTypedef<T>) {
94 return value.GetUnderlying();
104 meta::
IsRange<T> && !
meta::IsInstantiationOf<std::remove_const_t<T>, std::basic_string> &&
105 !
meta::IsRecursiveRange<T> && !std::is_same_v<boost::
uuids::uuid, std::remove_const_t<T>>;
108constexpr void CheckIfAllowsLogging() {
109 static_assert(IsStrongTypedef<T>);
111 if constexpr (T::kOps & StrongTypedefOps::kNonLoggable) {
112 static_assert(!
sizeof(T),
"Trying to print a non-loggable StrongTypedef");
116template <
class To,
class... Args>
117constexpr bool IsStrongToStrongConversion()
noexcept {
118 static_assert(IsStrongTypedef<To>);
120 if constexpr (
sizeof...(Args) == 1) {
121 using FromDecayed = std::decay_t<
decltype((std::declval<Args>(), ...))>;
122 if constexpr (IsStrongTypedef<FromDecayed>) {
124 return !std::is_same_v<FromDecayed, To> &&
125 (std::is_same_v<
typename FromDecayed::UnderlyingType,
typename To::UnderlyingType> ||
126 std::is_arithmetic_v<
typename To::UnderlyingType>);
161template <
class Tag,
class T, StrongTypedefOps Ops>
162class StrongTypedef :
public impl::strong_typedef::StrongTypedefTag {
163 static_assert(!std::is_reference_v<T>);
164 static_assert(!std::is_pointer_v<T>);
166 static_assert(!std::is_reference_v<Tag>);
167 static_assert(!std::is_pointer_v<Tag>);
170 using UnderlyingType = T;
172 static constexpr StrongTypedefOps kOps = Ops;
174 StrongTypedef() =
default;
175 StrongTypedef(
const StrongTypedef&) =
default;
176 StrongTypedef(StrongTypedef&&)
noexcept =
default;
177 StrongTypedef& operator=(
const StrongTypedef&) =
default;
178 StrongTypedef& operator=(StrongTypedef&&)
noexcept =
default;
180 constexpr StrongTypedef(impl::strong_typedef::InitializerList<T> lst)
184 template <
typename... Args>
185 requires std::is_constructible_v<T, Args...>
186 constexpr explicit StrongTypedef(Args&&... args)
noexcept(
noexcept(T(std::forward<Args>(args)...)))
187 : data_(std::forward<Args>(args)...)
189 using impl::strong_typedef::IsStrongToStrongConversion;
191 !IsStrongToStrongConversion<StrongTypedef, Args...>(),
192 "Attempt to convert one StrongTypedef to another. Use "
193 "utils::StrongCast to do that"
197 constexpr explicit operator
const T&()
const&
noexcept USERVER_IMPL_LIFETIME_BOUND {
return data_; }
198 constexpr explicit operator T() &&
noexcept {
return std::move(data_); }
199 constexpr explicit operator T&() &
noexcept USERVER_IMPL_LIFETIME_BOUND {
return data_; }
201 constexpr const T& GetUnderlying()
const&
noexcept USERVER_IMPL_LIFETIME_BOUND {
return data_; }
202 constexpr T GetUnderlying() &&
noexcept {
return std::move(data_); }
203 constexpr T& GetUnderlying() &
noexcept USERVER_IMPL_LIFETIME_BOUND {
return data_; }
206 requires impl::strong_typedef::Range<T>
208 return std::begin(data_);
212 requires impl::strong_typedef::Range<T>
214 return std::end(data_);
218 requires impl::strong_typedef::Range<T>
220 return std::begin(data_);
224 requires impl::strong_typedef::Range<T>
226 return std::end(data_);
230 requires impl::strong_typedef::Range<T>
232 return std::cbegin(data_);
236 requires impl::strong_typedef::Range<T>
238 return std::cend(data_);
244 return std::size(data_);
247 auto empty()
const {
return data_.empty(); }
249 auto clear() {
return data_.clear(); }
252 decltype(
auto) operator[](Arg&& i) {
253 return data_[std::forward<Arg>(i)];
256 decltype(
auto) operator[](Arg&& i)
const {
257 return data_[std::forward<Arg>(i)];
267#define UTILS_STRONG_TYPEDEF_REL_OP(OPERATOR)
272 impl::strong_typedef::IsStrongTypedef<T> || impl::strong_typedef::IsStrongTypedef<U>,
275 constexpr auto operator
OPERATOR(const T& lhs, const U& rhs)
276 ->decltype(impl::strong_typedef::UnwrapIfStrongTypedef(lhs
277 ) OPERATOR impl::strong_typedef::UnwrapIfStrongTypedef(rhs)) {
278 if constexpr (impl::strong_typedef::IsStrongTypedef<T>) {
279 if constexpr (impl::strong_typedef::IsStrongTypedef<U>) {
280 impl::strong_typedef::CheckStrongCompare<T, U>();
281 return lhs.GetUnderlying() OPERATOR rhs.GetUnderlying();
283 impl::strong_typedef::CheckTransparentCompare<T>();
284 return lhs.GetUnderlying() OPERATOR rhs;
287 impl::strong_typedef::CheckTransparentCompare<U>();
288 return lhs OPERATOR rhs.GetUnderlying();
304#undef UTILS_STRONG_TYPEDEF_REL_OP
308template <
class Tag,
class T, StrongTypedefOps Ops>
309std::ostream& operator<<(std::ostream& os,
const StrongTypedef<Tag, T, Ops>& v) {
310 impl::strong_typedef::CheckIfAllowsLogging<StrongTypedef<Tag, T, Ops>>();
311 return os << v.GetUnderlying();
314template <
class Tag,
class T, StrongTypedefOps Ops>
315logging::LogHelper& operator<<(
logging::LogHelper& os,
const StrongTypedef<Tag, T, Ops>& v) {
316 impl::strong_typedef::CheckIfAllowsLogging<StrongTypedef<Tag, T, Ops>>();
317 return os << v.GetUnderlying();
321template <
class Tag,
class T, StrongTypedefOps Ops>
322constexpr decltype(
auto) UnderlyingValue(
const StrongTypedef<Tag, T, Ops>& v)
noexcept {
323 return v.GetUnderlying();
326template <
class Tag,
class T, StrongTypedefOps Ops>
327constexpr T UnderlyingValue(StrongTypedef<Tag, T, Ops>&& v)
noexcept {
328 return std::move(v).GetUnderlying();
331constexpr bool IsStrongTypedefLoggable(StrongTypedefOps ops) {
return !(ops & StrongTypedefOps::kNonLoggable); }
335template <impl::strong_typedef::IsStrongTypedef T, formats::common::IsFormatValue Value>
336T Parse(
const Value& source, formats::
parse::
To<T>) {
337 return T{source.
template As<
typename T::UnderlyingType>()};
340template <impl::strong_typedef::IsStrongTypedef T,
typename Value>
341Value Serialize(
const T& object, formats::
serialize::
To<Value>) {
342 impl::strong_typedef::CheckIfAllowsLogging<T>();
343 return typename Value::Builder(object.GetUnderlying()).ExtractValue();
346template <impl::strong_typedef::IsStrongTypedef T,
typename StringBuilder>
347void WriteToStream(
const T& object, StringBuilder& sw) {
348 impl::strong_typedef::CheckIfAllowsLogging<T>();
349 WriteToStream(object.GetUnderlying(), sw);
352template <
typename Tag, StrongTypedefOps Ops>
353std::string ToString(
const StrongTypedef<Tag, std::string, Ops>& object) {
354 impl::strong_typedef::CheckIfAllowsLogging<StrongTypedef<Tag, std::string, Ops>>();
355 return object.GetUnderlying();
358template <
typename Tag, meta::IsInteger T, StrongTypedefOps Ops>
359std::string ToString(
const StrongTypedef<Tag, T, Ops>& object) {
360 impl::strong_typedef::CheckIfAllowsLogging<StrongTypedef<Tag, std::string, Ops>>();
361 return std::to_string(object.GetUnderlying());
364template <
typename Tag, std::floating_point T, StrongTypedefOps Ops>
365std::string ToString(
const StrongTypedef<Tag, T, Ops>& object) {
366 impl::strong_typedef::CheckIfAllowsLogging<StrongTypedef<Tag, std::string, Ops>>();
367 return fmt::to_string(object.GetUnderlying());
375template <
typename Target,
typename Tag,
typename T, StrongTypedefOps Ops>
376constexpr Target
StrongCast(
const StrongTypedef<Tag, T, Ops>& src) {
377 static_assert(impl::strong_typedef::IsStrongTypedef<Target>,
"Expected strong typedef as target type");
379 std::is_convertible_v<T,
typename Target::UnderlyingType>,
380 "Source strong typedef underlying type must be convertible to "
381 "target's underlying type"
383 return Target{src.GetUnderlying()};
386template <
typename Target,
typename Tag,
typename T, StrongTypedefOps Ops>
387constexpr Target StrongCast(StrongTypedef<Tag, T, Ops>&& src) {
388 static_assert(impl::strong_typedef::IsStrongTypedef<Target>,
"Expected strong typedef as target type");
390 std::is_convertible_v<T,
typename Target::UnderlyingType>,
391 "Source strong typedef underlying type must be convertible to "
392 "target's underlying type"
394 return Target{std::move(src).GetUnderlying()};
397template <
class Tag,
class T, StrongTypedefOps Ops>
398std::size_t hash_value(
const StrongTypedef<Tag, T, Ops>& v) {
399 return boost::hash<T>{}(v.GetUnderlying());
403template <
class Tag,
class T, StrongTypedefOps Ops>
404void PrintTo(
const StrongTypedef<Tag, T, Ops>& v, std::ostream* os) {
405 *os << testing::PrintToString(v.GetUnderlying());
412template <
class Tag,
class T>
413using NonLoggable = StrongTypedef<Tag, T, StrongTypedefOps::kCompareStrong | StrongTypedefOps::kNonLoggable>;
420template <
class Tag,
class T, USERVER_NAMESPACE::
utils::StrongTypedefOps Ops>
421struct std::
hash<USERVER_NAMESPACE::
utils::StrongTypedef<Tag, T, Ops>> : std::hash<T> {
422 std::size_t operator()(
const USERVER_NAMESPACE::
utils::StrongTypedef<Tag, T, Ops>& v
423 )
const noexcept(
noexcept(std::declval<
const std::hash<T>>()(std::declval<
const T&>()))) {
424 return std::hash<T>::operator()(v.GetUnderlying());
429template <USERVER_NAMESPACE::utils::impl::strong_typedef::IsStrongTypedef T,
class Char>
430requires(!USERVER_NAMESPACE::
utils::impl::strong_typedef::Range<T>)
431struct fmt::formatter<T, Char> :
fmt::formatter<
typename T::UnderlyingType, Char> {
432 template <
typename FormatContext>
434 USERVER_NAMESPACE::
utils::impl::strong_typedef::CheckIfAllowsLogging<T>();
435 return fmt::formatter<
typename T::UnderlyingType, Char>::format(v.GetUnderlying(), ctx);