diff options
author | Ed Tanous <edtanous@google.com> | 2022-03-11 02:38:01 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2022-07-08 23:34:43 +0300 |
commit | c715ec296f0560d986d2cfaac2a7b4bb083f4735 (patch) | |
tree | db4cf29bb4ab5e36f9e48c8eaf729c3cac9eab82 /http/utility.hpp | |
parent | 7b1dd2f90b04942289cc51031b4e9228da201365 (diff) | |
download | bmcweb-c715ec296f0560d986d2cfaac2a7b4bb083f4735.tar.xz |
Remove magic numbers
There's lots of magic numbers in this file that we inherited from crow.
This commit Adds a TypeCode enum class that can be used in place of the
magic numbers. It keeps the same values as were present previously
(0-6) in case there are places where this abstraction leaked out, but I
believe this catches all of them.
Tested: Redfish service validator passes.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I063955adb8bf75d9bb6298e29e6e44c210ee9cc3
Diffstat (limited to 'http/utility.hpp')
-rw-r--r-- | http/utility.hpp | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/http/utility.hpp b/http/utility.hpp index 7b0af130a6..431a2fe40b 100644 --- a/http/utility.hpp +++ b/http/utility.hpp @@ -10,6 +10,7 @@ #include <array> #include <chrono> +#include <cstddef> #include <cstdint> #include <ctime> #include <functional> @@ -27,58 +28,76 @@ namespace crow namespace black_magic { +enum class TypeCode : uint8_t +{ + Unspecified = 0, + Integer = 1, + UnsignedInteger = 2, + Float = 3, + String = 4, + Path = 5, + Max = 6, +}; + +// Remove when we have c++23 +template <typename E> +constexpr typename std::underlying_type<E>::type toUnderlying(E e) noexcept +{ + return static_cast<typename std::underlying_type<E>::type>(e); +} + template <typename T> -constexpr uint64_t getParameterTag() +constexpr TypeCode getParameterTag() { if constexpr (std::is_same_v<int, T>) { - return 1; + return TypeCode::Integer; } if constexpr (std::is_same_v<char, T>) { - return 1; + return TypeCode::Integer; } if constexpr (std::is_same_v<short, T>) { - return 1; + return TypeCode::Integer; } if constexpr (std::is_same_v<long, T>) { - return 1; + return TypeCode::Integer; } if constexpr (std::is_same_v<long long, T>) { - return 1; + return TypeCode::Integer; } if constexpr (std::is_same_v<unsigned int, T>) { - return 2; + return TypeCode::UnsignedInteger; } if constexpr (std::is_same_v<unsigned char, T>) { - return 2; + return TypeCode::UnsignedInteger; } if constexpr (std::is_same_v<unsigned short, T>) { - return 2; + return TypeCode::UnsignedInteger; } if constexpr (std::is_same_v<unsigned long, T>) { - return 2; + return TypeCode::UnsignedInteger; } if constexpr (std::is_same_v<unsigned long long, T>) { - return 2; + return TypeCode::UnsignedInteger; } if constexpr (std::is_same_v<double, T>) { - return 3; + return TypeCode::Float; } if constexpr (std::is_same_v<std::string, T>) { - return 4; + return TypeCode::String; } - return 0; + return TypeCode::Unspecified; } template <typename... Args> @@ -96,8 +115,12 @@ struct computeParameterTagFromArgsList<Arg, Args...> static constexpr int subValue = computeParameterTagFromArgsList<Args...>::value; static constexpr int value = - getParameterTag<typename std::decay<Arg>::type>() != 0 - ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>() + getParameterTag<typename std::decay<Arg>::type>() != + TypeCode::Unspecified + ? static_cast<unsigned long>(subValue * + toUnderlying(TypeCode::Max)) + + static_cast<uint64_t>( + getParameterTag<typename std::decay<Arg>::type>()) : subValue; }; @@ -113,22 +136,23 @@ inline bool isParameterTagCompatible(uint64_t a, uint64_t b) { return a == 0; } - uint64_t sa = a % 6; - uint64_t sb = a % 6; - if (sa == 5) + TypeCode sa = static_cast<TypeCode>(a % toUnderlying(TypeCode::Max)); + TypeCode sb = static_cast<TypeCode>(b % toUnderlying(TypeCode::Max)); + + if (sa == TypeCode::Path) { - sa = 4; + sa = TypeCode::String; } - if (sb == 5) + if (sb == TypeCode::Path) { - sb = 4; + sb = TypeCode::String; } if (sa != sb) { return false; } - a /= 6; - b /= 6; + a /= toUnderlying(TypeCode::Max); + b /= toUnderlying(TypeCode::Max); } return false; } @@ -172,23 +196,24 @@ constexpr inline uint64_t getParameterTag(std::string_view url) if (tag == "<int>") { - tagValue += insertIndex * 1; + tagValue += insertIndex * toUnderlying(TypeCode::Integer); } if (tag == "<uint>") { - tagValue += insertIndex * 2; + tagValue += + insertIndex * toUnderlying(TypeCode::UnsignedInteger); } if (tag == "<float>" || tag == "<double>") { - tagValue += insertIndex * 3; + tagValue += insertIndex * toUnderlying(TypeCode::Float); } if (tag == "<str>" || tag == "<string>") { - tagValue += insertIndex * 4; + tagValue += insertIndex * toUnderlying(TypeCode::String); } if (tag == "<path>") { - tagValue += insertIndex * 5; + tagValue += insertIndex * toUnderlying(TypeCode::Path); } paramIndex++; urlSegmentIndex = std::string_view::npos; |