Files
externals/linux/include/ActivMiddleware/Misc/StringView.h
T

231 lines
7.7 KiB
C++

/**
* @file include/ActivMiddleware/Misc/StringView.h
*
* @brief string_view helpers.
*/
#if (!defined ACTIV_MISC_STRING_VIEW_H)
#define ACTIV_MISC_STRING_VIEW_H
#include "ActivMiddleware/ActivMiddleware.h"
#include "ActivMiddleware/CxxYZ/string_view.h"
#include "ActivMiddleware/CxxYZ/type_traits.h"
#include ACTIV_BOOST_INCLUDE(functional/hash.hpp)
#include <functional>
namespace ACTIV_CXXYZ_STRING_VIEW_NAMESPACE
{
inline std::size_t hash_value(string_view str)
{
// Sadly ADL won't work with types using-ed into another namespace, so we have to define this in the underlying
// string_view namespace.
return Boost::hash_range(str.begin(), str.end());
}
} // namespace ACTIV_CXXYZ_STRING_VIEW_NAMESPACE
// ---------------------------------------------------------------------------------------------------------------------------------
#if (!defined ACTIV_BOOST_NO_CXX11_HDR_FUNCTIONAL) && (!defined ACTIV_CXXYZ_USING_STD_STRING_VIEW)
namespace std
{
template<>
struct hash<Activ::CxxYZ::string_view>
{
size_t operator()(Activ::CxxYZ::string_view str) const
{
return hash_value(str);
}
};
} // namespace std
#endif // (!defined ACTIV_BOOST_NO_CXX11_HDR_FUNCTIONAL) && (!defined ACTIV_CXXYZ_USING_STD_STRING_VIEW)
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Activ namespace.
namespace Activ
{
namespace Detail
{
template<typename TARGET, typename SOURCE>
class ToStringTypeImpl
{
public:
TARGET operator()(const SOURCE &string) const ACTIV_BOOST_NOEXCEPT
{
return TARGET(string.data(), string.length());
}
};
} // namespace Detail
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Convert a string-like type to another string-like type. string_view doesn't support vstring,
* so this allows generic code to work with std::string, String, etc.
*
* @tparam TARGET a string-like type (constructed from a const char * and a length).
* @tparam SOURCE a string-like type (with data() and length() methods).
*
* @param string reference to a string-like object.
*
* @return A new TARGET containing or referring to @a string.
*/
template<typename TARGET, typename SOURCE>
TARGET ToStringType(const SOURCE &string) ACTIV_BOOST_NOEXCEPT
{
// Extra level of indirection to allow partial specialization, should we ever need it.
return Activ::Detail::ToStringTypeImpl<TARGET, SOURCE>()(string);
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Functor that can compare any combination of string_view or String. For use in, for example,
* a multi_index to allow compatible key lookups. N3421 will probably obviate the need for this one day.
*
* @tparam COMPARE the comparison predicate to use (std::equal_to, std::less, etc.).
*/
template<template<typename> class COMPARE>
class StringComparisonFunctor
{
public:
template<typename T1, typename T2>
bool operator()(const T1 &lhs, const T2 &rhs) const
{
ACTIV_BOOST_STATIC_ASSERT(CxxYZ::is_same<typename T1::value_type, typename T2::value_type>::value);
return COMPARE<CxxYZ::string_view>()(ToStringType<CxxYZ::string_view>(lhs), ToStringType<CxxYZ::string_view>(rhs));
}
typedef void is_transparent;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Functor that can hash a string_view or String. For use in, for example, a multi_index hashed_(non_)unique
* index to allow compatible key lookups.
*/
class StringHashFunctor
{
public:
template<typename T>
size_t operator()(const T &string) const
{
return Boost::hash<T>()(string);
}
};
} // namespace Activ
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_USING_ALTERNATIVE_STRING)
namespace ACTIV_CXXYZ_STRING_VIEW_NAMESPACE
{
// Boost pass string_view by value, so we'll do the same.
inline bool operator==(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs == Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator==(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) == rhs;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator!=(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs != Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator!=(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) != rhs;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator<(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs < Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator<(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) < rhs;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator>(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs > Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator>(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) > rhs;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator<=(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs <= Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator<=(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) <= rhs;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator>=(string_view lhs, const Activ::String &rhs) ACTIV_BOOST_NOEXCEPT
{
return lhs >= Activ::ToStringType<string_view>(rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool operator>=(const Activ::String &lhs, string_view rhs) ACTIV_BOOST_NOEXCEPT
{
return Activ::ToStringType<string_view>(lhs) >= rhs;
}
} // namespace ACTIV_CXXYZ_STRING_VIEW_NAMESPACE
#endif // (defined ACTIV_USING_ALTERNATIVE_STRING)
#endif // (!defined ACTIV_MISC_STRING_VIEW_H)