diff options
author | eeight <eeight@yandex-team.ru> | 2022-02-10 16:46:18 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:18 +0300 |
commit | 475c0a46f28166e83fd263badc7546377cddcabe (patch) | |
tree | 39c5a49b8aaad78fe390b6f1f2886bdbda40f3e7 /util/system/hi_lo.h | |
parent | a6e0145a095c7bb3770d6e07aee301de5c73f96e (diff) | |
download | ydb-475c0a46f28166e83fd263badc7546377cddcabe.tar.gz |
Restoring authorship annotation for <eeight@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/hi_lo.h')
-rw-r--r-- | util/system/hi_lo.h | 256 |
1 files changed, 128 insertions, 128 deletions
diff --git a/util/system/hi_lo.h b/util/system/hi_lo.h index f86870534f..3dad401140 100644 --- a/util/system/hi_lo.h +++ b/util/system/hi_lo.h @@ -1,149 +1,149 @@ -#pragma once - -#include "unaligned_mem.h" - +#pragma once + +#include "unaligned_mem.h" + #include <utility> -#ifndef _little_endian_ +#ifndef _little_endian_ #error "Not implemented" -#endif - -namespace NHiLoPrivate { - template <class TRepr> - class TConstIntRef { - public: - explicit TConstIntRef(const char* ptr) - : Ptr(ptr) - { - } - +#endif + +namespace NHiLoPrivate { + template <class TRepr> + class TConstIntRef { + public: + explicit TConstIntRef(const char* ptr) + : Ptr(ptr) + { + } + TRepr Get() const { return ReadUnaligned<TRepr>(Ptr); } operator TRepr() const { return Get(); } - + const char* GetPtr() const { return Ptr; } - - protected: - const char* Ptr; - }; - - template <class TRepr> + + protected: + const char* Ptr; + }; + + template <class TRepr> class TIntRef: public TConstIntRef<TRepr> { - public: - explicit TIntRef(char* ptr) - : TConstIntRef<TRepr>(ptr) - { - } - - TIntRef& operator=(TRepr value) { + public: + explicit TIntRef(char* ptr) + : TConstIntRef<TRepr>(ptr) + { + } + + TIntRef& operator=(TRepr value) { WriteUnaligned<TRepr>(GetPtr(), value); - return *this; - } - + return *this; + } + char* GetPtr() const { return const_cast<char*>(this->Ptr); } - }; - - template <class T> - struct TReferenceType { - using TType = T; - }; - - template <class T> - struct TReferenceType<TConstIntRef<T>> { - using TType = T; - }; - - template <class T> - struct TReferenceType<TIntRef<T>> { - using TType = T; - }; - - template <class TRepr> - auto MakeIntRef(const char* ptr) { - return TConstIntRef<TRepr>(ptr); - } - - template <class TRepr> - auto MakeIntRef(char* ptr) { - return TIntRef<TRepr>(ptr); - } - - template <class T> - const char* CharPtrOf(const T& value) { + }; + + template <class T> + struct TReferenceType { + using TType = T; + }; + + template <class T> + struct TReferenceType<TConstIntRef<T>> { + using TType = T; + }; + + template <class T> + struct TReferenceType<TIntRef<T>> { + using TType = T; + }; + + template <class TRepr> + auto MakeIntRef(const char* ptr) { + return TConstIntRef<TRepr>(ptr); + } + + template <class TRepr> + auto MakeIntRef(char* ptr) { + return TIntRef<TRepr>(ptr); + } + + template <class T> + const char* CharPtrOf(const T& value) { return reinterpret_cast<const char*>(&value); - } - - template <class T> - char* CharPtrOf(T& value) { + } + + template <class T> + char* CharPtrOf(T& value) { return reinterpret_cast<char*>(&value); - } - - template <class T> - const char* CharPtrOf(TConstIntRef<T> value) { - return value.GetPtr(); - } - - template <class T> - char* CharPtrOf(TIntRef<T> value) { - return value.GetPtr(); - } - - template <bool IsLow, class TRepr, class T> - auto MakeIntRef(T&& value) { - using TRef = typename TReferenceType<typename std::decay<T>::type>::TType; - static_assert( + } + + template <class T> + const char* CharPtrOf(TConstIntRef<T> value) { + return value.GetPtr(); + } + + template <class T> + char* CharPtrOf(TIntRef<T> value) { + return value.GetPtr(); + } + + template <bool IsLow, class TRepr, class T> + auto MakeIntRef(T&& value) { + using TRef = typename TReferenceType<typename std::decay<T>::type>::TType; + static_assert( std::is_scalar<TRef>::value, "Hi* and Lo* functions can be applied only to scalar values"); - static_assert(sizeof(TRef) >= sizeof(TRepr), "Requested bit range is not within provided value"); - constexpr size_t offset = IsLow ? 0 : sizeof(TRef) - sizeof(TRepr); - - return MakeIntRef<TRepr>(CharPtrOf(std::forward<T>(value)) + offset); - } -} - -/** - * Return manipulator object that allows to get and set lower or higher bits of the value. - * - * @param value Must be a scalar value of sufficient size or a manipulator object obtained by - * calling any of the other Hi/Lo functions. - * - * @{ - */ -template <class T> -auto Lo32(T&& value) { - return NHiLoPrivate::MakeIntRef<true, ui32>(std::forward<T>(value)); -} - -template <class T> -auto Hi32(T&& value) { - return NHiLoPrivate::MakeIntRef<false, ui32>(std::forward<T>(value)); -} - -template <class T> -auto Lo16(T&& value) { - return NHiLoPrivate::MakeIntRef<true, ui16>(std::forward<T>(value)); -} - -template <class T> -auto Hi16(T&& value) { - return NHiLoPrivate::MakeIntRef<false, ui16>(std::forward<T>(value)); -} - -template <class T> -auto Lo8(T&& value) { - return NHiLoPrivate::MakeIntRef<true, ui8>(std::forward<T>(value)); -} - -template <class T> -auto Hi8(T&& value) { - return NHiLoPrivate::MakeIntRef<false, ui8>(std::forward<T>(value)); -} - -/** @} */ + static_assert(sizeof(TRef) >= sizeof(TRepr), "Requested bit range is not within provided value"); + constexpr size_t offset = IsLow ? 0 : sizeof(TRef) - sizeof(TRepr); + + return MakeIntRef<TRepr>(CharPtrOf(std::forward<T>(value)) + offset); + } +} + +/** + * Return manipulator object that allows to get and set lower or higher bits of the value. + * + * @param value Must be a scalar value of sufficient size or a manipulator object obtained by + * calling any of the other Hi/Lo functions. + * + * @{ + */ +template <class T> +auto Lo32(T&& value) { + return NHiLoPrivate::MakeIntRef<true, ui32>(std::forward<T>(value)); +} + +template <class T> +auto Hi32(T&& value) { + return NHiLoPrivate::MakeIntRef<false, ui32>(std::forward<T>(value)); +} + +template <class T> +auto Lo16(T&& value) { + return NHiLoPrivate::MakeIntRef<true, ui16>(std::forward<T>(value)); +} + +template <class T> +auto Hi16(T&& value) { + return NHiLoPrivate::MakeIntRef<false, ui16>(std::forward<T>(value)); +} + +template <class T> +auto Lo8(T&& value) { + return NHiLoPrivate::MakeIntRef<true, ui8>(std::forward<T>(value)); +} + +template <class T> +auto Hi8(T&& value) { + return NHiLoPrivate::MakeIntRef<false, ui8>(std::forward<T>(value)); +} + +/** @} */ |