cheshirekow  v0.1.0
binary_literal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_UTIL_BINARY_LITERAL_H_
28 #define MPBLOCKS_UTIL_BINARY_LITERAL_H_
29 
30 #include <limits>
31 #include <stdexcept>
32 
33 namespace mpblocks {
34 namespace utility {
35 
36 template <typename T>
37 constexpr size_t NumberOfBits() {
38  static_assert(std::numeric_limits<T>::is_integer, "only integers allowed");
39 
40  // from en.cppreference.com: The value of std::numeric_limits<T>::digits is
41  // the number of digits in base-radix that can be represented by the type T
42  // without change. For integer types, this is the number of bits not counting
43  // the sign bit. For floating-point types, this is the number of digits in
44  // the mantissa.
45  return std::numeric_limits<T>::digits;
46 }
47 
48 // compute the length of a string using recursion, so that it can be
49 // compile-time evaluated
50 constexpr size_t StringLength(const char* str, size_t current_len = 0) {
51  return *str == '\0'
52  ? current_len // end of recursion
53  : StringLength(str + 1, current_len + 1); // compute recursively
54 }
55 
56 // validates that a character is part of a binary string
57 constexpr bool IsBinary(char c) { return c == '0' || c == '1'; }
58 
59 // implementation function called after validating string length
60 template <typename OutputType=unsigned>
61 constexpr unsigned _BinaryLiteral(const char* str, size_t val = 0) {
62  return StringLength(str) == 0
63  ? val // end of recursion
64  : IsBinary(*str) // check for non-binary digit
65  ? _BinaryLiteral(str + 1, 2 * val + *str - '0')
66  : throw std::logic_error("char is not '0' or '1'");
67 }
68 
69 template <typename OutputType=unsigned>
70 constexpr OutputType BinaryLiteral(const char* str, size_t val = 0) {
71  return StringLength(str) <= NumberOfBits<OutputType>()
72  ? _BinaryLiteral(str,val)
73  : throw std::logic_error("Binary literal is too long for type");
74 }
75 
76 } // namespace utility
77 } // namespace mpblocks
78 
79 #define BINARY_LITERAL(X) mpblocks::utility::BinaryLiteral(#X)
80 
81 #if (__cplusplus >= 201103L)
82 constexpr unsigned operator"" _b( const char * str ) {
84 }
85 #endif
86 
87 #endif // MPBLOCKS_UTIL_BINARY_LITERAL_H_
constexpr unsigned _BinaryLiteral(const char *str, size_t val=0)
constexpr size_t NumberOfBits()
constexpr bool IsBinary(char c)
constexpr OutputType BinaryLiteral(const char *str, size_t val=0)
const Char8_t * str(const Char8_t *s1, const Char8_t *s2)
constexpr size_t StringLength(const char *str, size_t current_len=0)