userver: userver/storages/postgres/detail/is_in_namespace.hpp Source File
Loading...
Searching...
No Matches
is_in_namespace.hpp
1#pragma once
2
3#include <string_view>
4#include <type_traits>
5
6#include <boost/current_function.hpp>
7
8#include <userver/utils/text_light.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace storages::postgres::detail {
13
14#ifdef __clang__
15constexpr std::string_view kExpectedPrefix =
16 "postgres::detail::IsInNamespaceImpl(std::string_view) [T = ";
17#else
18constexpr std::string_view kExpectedPrefix =
19 "postgres::detail::IsInNamespaceImpl(std::string_view) [with T = ";
20#endif
21
22template <typename T>
23constexpr bool IsInNamespaceImpl(std::string_view nsp) {
24 using USERVER_NAMESPACE::utils::text::StartsWith;
25 constexpr std::string_view fname = BOOST_CURRENT_FUNCTION;
26 constexpr auto pos = fname.find(kExpectedPrefix);
27 if (pos == std::string_view::npos) {
28 return false;
29 }
30 constexpr std::string_view fname_short{fname.data() + pos,
31 fname.size() - pos};
32 static_assert(!fname_short.empty(),
33 "Your compiler produces an unexpected function pretty name");
34 return StartsWith(fname_short.substr(kExpectedPrefix.size()), nsp) &&
35 StartsWith(fname_short.substr(kExpectedPrefix.size() + nsp.size()),
36 "::");
37}
38
39template <typename T>
40constexpr bool IsInNamespace(std::string_view nsp) {
41 return IsInNamespaceImpl<std::remove_const_t<std::decay_t<T>>>(nsp);
42}
43
44template <typename T>
45inline constexpr bool kIsInStdNamespace = IsInNamespace<T>("std");
46template <typename T>
47inline constexpr bool kIsInBoostNamespace = IsInNamespace<T>("boost");
48
49} // namespace storages::postgres::detail
50
51USERVER_NAMESPACE_END