Permalink
Browse files

Added static to_int<Chars>

  • Loading branch information...
1 parent a78e0c1 commit a0502087a399199a96d11a060632bb1d29e8c31f @saleyn committed Jul 17, 2014
Showing with 28 additions and 0 deletions.
  1. +22 −0 include/utxx/meta.hpp
  2. +6 −0 test/test_meta.cpp
View
@@ -83,6 +83,28 @@ template <typename E>
constexpr typename std::underlying_type<E>::type to_underlying(E e) {
return static_cast<typename std::underlying_type<E>::type>(e);
}
+
+namespace {
+ template<int N, char... Chars>
+ struct to_int_helper;
+
+ template<int N, char C>
+ struct to_int_helper<N, C> {
+ static const size_t value = ((size_t)C) << (8*N);
+ };
+
+ template <int N, char C, char... Tail>
+ struct to_int_helper<N, C, Tail...> {
+ static const size_t value =
+ ((size_t)C) << (8*N) | to_int_helper<N-1, Tail...>::value;
+ };
+}
+
+template <char... Chars>
+struct to_int {
+ static const size_t value = to_int_helper<(sizeof...(Chars))-1, Chars...>::value;
+};
+
#endif
} // namespace utxx
View
@@ -44,6 +44,12 @@ BOOST_AUTO_TEST_CASE( test_meta )
BOOST_STATIC_ASSERT(1 == to_underlying(B::B1));
BOOST_REQUIRE_EQUAL(1, to_underlying(B::B1));
+
+ BOOST_STATIC_ASSERT(0x00 == to_int<'\0'>::value);
+ BOOST_STATIC_ASSERT(0x01 == to_int<'\1'>::value);
+ BOOST_STATIC_ASSERT(0x0100 == to_int<'\1', '\0'>::value);
+ BOOST_STATIC_ASSERT(0x0102 == to_int<'\1', '\2'>::value);
+ BOOST_STATIC_ASSERT(0x010203 == to_int<'\1', '\2', '\3'>::value);
}
#endif

0 comments on commit a050208

Please sign in to comment.