userver: userver/storages/postgres/detail/is_in_namespace.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
is_in_namespace.hpp
1#pragma once
2
3#include <string_view>
4#include <type_traits>
5
6#include <boost/current_function.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace storages::postgres::detail {
11
12#ifdef __clang__
13constexpr std::string_view kExpectedPrefix =
14 "postgres::detail::IsInNamespaceImpl(std::string_view) [T = ";
15#else
16constexpr std::string_view kExpectedPrefix =
17 "postgres::detail::IsInNamespaceImpl(std::string_view) [with T = ";
18#endif
19
20constexpr bool StartsWith(std::string_view haystack, std::string_view needle) {
21 return haystack.substr(0, needle.size()) == needle;
22}
23
24template <typename T>
25constexpr bool IsInNamespaceImpl(std::string_view nsp) {
26 constexpr std::string_view fname = BOOST_CURRENT_FUNCTION;
27 constexpr auto pos = fname.find(kExpectedPrefix);
28 if (pos == std::string_view::npos) {
29 return false;
30 }
31 constexpr std::string_view fname_short{fname.data() + pos,
32 fname.size() - pos};
33 static_assert(!fname_short.empty(),
34 "Your compiler produces an unexpected function pretty name");
35 return StartsWith(fname_short.substr(kExpectedPrefix.size()), nsp) &&
36 StartsWith(fname_short.substr(kExpectedPrefix.size() + nsp.size()),
37 "::");
38}
39
40template <typename T>
41constexpr bool IsInNamespace(std::string_view nsp) {
42 return IsInNamespaceImpl<std::remove_const_t<std::decay_t<T>>>(nsp);
43}
44
45template <typename T>
46inline constexpr bool kIsInStdNamespace = IsInNamespace<T>("std");
47template <typename T>
48inline constexpr bool kIsInBoostNamespace = IsInNamespace<T>("boost");
49
50} // namespace storages::postgres::detail
51
52USERVER_NAMESPACE_END