123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #ifndef LOCAL_UTILS_H_
- #define LOCAL_UTILS_H_
- #include <limits>
- #include <type_traits>
- #include "android-base/logging.h"
- #define SAFELY_CLAMP(SRC, DST_TYPE, MIN, MAX) \
- local_utils::internal::SafelyClamp<decltype(SRC), DST_TYPE, MIN, MAX, MIN, \
- MAX>(SRC)
- #if defined(__clang__)
- #define NONNULL [[gnu::nonnull]]
- #define RETURNS_NONNULL [[gnu::returns_nonnull]]
- #else
- #define NONNULL
- #define RETURNS_NONNULL
- #endif
- namespace android {
- namespace wifilogd {
- namespace local_utils {
- template <typename T>
- constexpr auto CastEnumToInteger(T enum_value) {
- static_assert(std::is_enum<T>::value, "argument must be of an enum type");
- return static_cast<typename std::underlying_type<T>::type>(enum_value);
- }
- template <typename T>
- T CopyFromBufferOrDie(NONNULL const void* buf, size_t buf_len) {
- static_assert(std::is_trivially_copyable<T>::value,
- "CopyFromBufferOrDie can only copy trivially copyable types");
- T out;
- CHECK(buf_len >= sizeof(out));
- std::memcpy(&out, buf, sizeof(out));
- return out;
- }
- template <typename T>
- constexpr T GetMaxVal() {
-
-
- static_assert(std::is_integral<T>::value,
- "GetMaxVal requires an integral type");
- return std::numeric_limits<T>::max();
- }
- template <typename T>
- constexpr T GetMaxVal(const T& ) {
- return GetMaxVal<T>();
- }
- inline bool IsAsciiPrintable(uint8_t c) {
- return (c == '\t' || c == '\n' || (c >= ' ' && c <= '~'));
- }
- namespace internal {
- template <typename SrcType, typename DstType, SrcType MinAsSrcType,
- SrcType MaxAsSrcType, DstType MinAsDstType, DstType MaxAsDstType>
- DstType SafelyClamp(SrcType input) {
- static_assert(std::is_integral<SrcType>::value,
- "source type must be integral");
- static_assert(std::is_integral<DstType>::value,
- "destination type must be integral");
- static_assert(MinAsSrcType < MaxAsSrcType, "invalid source range");
- static_assert(MinAsDstType < MaxAsDstType, "invalid destination range");
-
-
-
-
-
-
-
-
-
-
- static_assert(MinAsSrcType == MinAsDstType, "inconsistent range min");
- static_assert(MaxAsSrcType == MaxAsDstType, "inconsistent range max");
- if (input < MinAsSrcType) {
- return MinAsDstType;
- } else if (input > MaxAsSrcType) {
- return MaxAsDstType;
- } else {
-
-
-
-
-
-
-
-
-
-
- return static_cast<DstType>(input);
- }
- }
- }
- }
- }
- }
- #endif
|