2 // Copyright Antony Polukhin, 2015-2016.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/type_index/ctti_type_index.hpp>
10 #include <boost/lexical_cast.hpp>
14 #include <boost/core/lightweight_test.hpp>
16 const char* hello1
= "Hello word";
17 const char* hello1_end
= hello1
+ sizeof("Hello word");
18 const char* hello2
= "Hello word, pal!";
19 const char* hello2_end
= hello2
+ sizeof("Hello word, pal!");
22 using boost::typeindex::detail::constexpr_strcmp
;
25 constexpr_strcmp(hello1
, hello1
) == 0
29 constexpr_strcmp(hello2
, hello2
) == 0
33 constexpr_strcmp(hello1
, hello2
) != 0
37 constexpr_strcmp(hello2
, hello1
) != 0
41 (constexpr_strcmp(hello2
, hello1
) < 0)
43 (std::strcmp(hello2
, hello1
) < 0)
47 (constexpr_strcmp(hello1
, hello2
) < 0)
49 (std::strcmp(hello1
, hello2
) < 0)
54 using boost::typeindex::detail::constexpr_search
;
56 constexpr_search(hello1
, hello1_end
, hello2
, hello2_end
) == std::search(hello1
, hello1_end
, hello2
, hello2_end
)
60 constexpr_search(hello2
, hello2_end
, hello1
, hello1_end
) == std::search(hello2
, hello2_end
, hello1
, hello1_end
)
63 const char* word
= "word";
64 const char* word_end
= word
+ sizeof("word") - 1;
66 constexpr_search(hello1
, hello1_end
, word
, word_end
) == std::search(hello1
, hello1_end
, word
, word_end
)
70 constexpr_search(hello2
, hello2_end
, word
, word_end
) == std::search(hello2
, hello2_end
, word
, word_end
)
74 template <class T
, std::size_t N
>
75 BOOST_CXX14_CONSTEXPR
bool in_namespace(const char (&ns
)[N
]) BOOST_NOEXCEPT
{
76 BOOST_CXX14_CONSTEXPR
const char* name
= boost::typeindex::ctti_type_index::type_id
<T
>().raw_name();
77 for (std::size_t i
= 0; i
< N
- 1; ++i
)
85 BOOST_CXX14_CONSTEXPR
bool is_boost_namespace() BOOST_NOEXCEPT
{
86 return in_namespace
<T
>("boost::") || in_namespace
<T
>("class boost::") || in_namespace
<T
>("struct boost::");
89 void constexpr_test() {
90 using namespace boost::typeindex
;
92 BOOST_CXX14_CONSTEXPR ctti_type_index t_int0
= ctti_type_index::type_id
<int>();
93 BOOST_CXX14_CONSTEXPR ctti_type_index t_short0
= ctti_type_index::type_id
<short>();
94 BOOST_CXX14_CONSTEXPR ctti_type_index t_int1
= ctti_type_index::type_id
<int>();
95 BOOST_CXX14_CONSTEXPR ctti_type_index t_short1
= ctti_type_index::type_id
<short>();
97 BOOST_CXX14_CONSTEXPR
bool same0
= (t_int0
== t_int1
);
100 BOOST_CXX14_CONSTEXPR
bool same1
= (t_short1
== t_short0
);
103 BOOST_CXX14_CONSTEXPR
bool same2
= (t_int1
== t_int1
);
106 BOOST_CXX14_CONSTEXPR
bool same3
= (t_short0
== t_short0
);
109 BOOST_CXX14_CONSTEXPR
bool same4
= !(t_short0
< t_short0
|| t_short0
> t_short0
);
112 BOOST_CXX14_CONSTEXPR
bool same5
= (t_short0
<= t_short0
&& t_short0
>= t_short0
);
116 BOOST_CXX14_CONSTEXPR
bool not_same0
= (t_int0
!= t_short1
);
117 BOOST_TEST(not_same0
);
119 BOOST_CXX14_CONSTEXPR
bool not_same1
= (t_int1
!= t_short0
);
120 BOOST_TEST(not_same1
);
122 BOOST_CXX14_CONSTEXPR
bool not_same2
= (t_int1
< t_short0
|| t_int1
> t_short0
);
123 BOOST_TEST(not_same2
);
126 BOOST_CXX14_CONSTEXPR
const char* int_name
= t_int0
.name();
127 BOOST_TEST(*int_name
!= '\0');
129 BOOST_CXX14_CONSTEXPR
const char* short_name
= t_short0
.name();
130 BOOST_TEST(*short_name
!= '\0');
132 BOOST_CXX14_CONSTEXPR
bool in_namespace
= is_boost_namespace
<ctti_type_index
>();
133 BOOST_TEST(in_namespace
);
135 BOOST_CXX14_CONSTEXPR
bool not_in_namespace
= !is_boost_namespace
<std::string
>();
136 BOOST_TEST(not_in_namespace
);
144 return boost::report_errors();