X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2c%2Fc-string-view.hpp;h=54c87de9f1df581541e6eec5b9af40e3155bcd1a;hb=821af09916878cc088b71e380980a75dc1017e10;hp=a7b54e70651fa271c3cc1dc71793a9ae044e19dd;hpb=05f7d581bee6323522f6b9c9843f87d3d8bff070;p=babeltrace.git diff --git a/src/cpp-common/bt2c/c-string-view.hpp b/src/cpp-common/bt2c/c-string-view.hpp index a7b54e70..54c87de9 100644 --- a/src/cpp-common/bt2c/c-string-view.hpp +++ b/src/cpp-common/bt2c/c-string-view.hpp @@ -15,6 +15,8 @@ #include "cpp-common/bt2s/string-view.hpp" #include "cpp-common/vendor/fmt/format.h" +#include "type-traits.hpp" + namespace bt2c { /* @@ -183,6 +185,67 @@ static inline const char *format_as(const CStringView& str) return str ? *str : "(null)"; } +namespace internal { + +template +const char *asConstCharPtr(StrT&& val) noexcept +{ + return val.data(); +} + +inline const char *asConstCharPtr(const char * const val) noexcept +{ + return val; +} + +template +using ComparableWithCStringView = + IsOneOf::type, CStringView, std::string, const char *>; + +} /* namespace internal */ + +/* + * Returns true if `lhs` is equal to `rhs`. + * + * `LhsT` and `RhsT` may be any of: + * + * • `const char *` + * • `std::string` + * • `CStringView` + * + * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data. + */ +template < + typename LhsT, typename RhsT, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type> +bool operator==(LhsT&& lhs, RhsT&& rhs) noexcept +{ + const auto rawLhs = internal::asConstCharPtr(lhs); + const auto rawRhs = internal::asConstCharPtr(rhs); + + BT_ASSERT_DBG(rawLhs); + BT_ASSERT_DBG(rawRhs); + return std::strcmp(rawLhs, rawRhs) == 0; +} + +/* + * Returns true if `lhs` is not equal to `rhs`. + * + * `LhsT` and `RhsT` may be any of: + * + * • `const char *` + * • `std::string` + * • `CStringView` + * + * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data. + */ +template +bool operator!=(LhsT&& lhs, RhsT&& rhs) noexcept +{ + return !(std::forward(lhs) == std::forward(rhs)); +} + } /* namespace bt2c */ #endif /* BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP */