Miam-Player  0.8.0
A nice music player
mkid.h
Go to the documentation of this file.
1 /******************************************************************************
2  mkid: map chars to int at build time. Template based. A replacement of FourCC
3  Copyright (C) 2012-2015 Wang Bin <wbsecg1@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ******************************************************************************/
19 
20 #ifndef MKID_H
21 #define MKID_H
22 
30 namespace mkid {
31 namespace detail {
32 template<int base, int x> struct map_base;
33 } // namespace internal
34 
35 template<int base, int a0>
36 struct id32_1 { enum { value = detail::map_base<base, a0>::value};};
37 template<int base, int a0, int a1>
39 template<int base, int a0, int a1, int a2>
41 template<int base, int a0, int a1, int a2, int a3>
43 template<int base, int a0, int a1, int a2, int a3, int a4>
45 template<int base, int a0, int a1, int a2, int a3, int a4, int a5>
47 
48 // template based FourCC
49 template<int a0, int a1, int a2, int a3>
50 struct fourcc : public id32_4<256, a0, a1, a2, a3>{};
51 
52 // a0~a4: '0'~'9', 'A'~'Z', 'a'~'z', '_', '.' ==>> [0,63]
53 template<int a0>
54 struct id32base64_1 : public id32_1<64, a0>{};
55 template<int a0, int a1>
56 struct id32base64_2 : public id32_2<64, a0, a1>{};
57 template<int a0, int a1, int a2>
58 struct id32base64_3 : public id32_3<64, a0, a1, a2>{};
59 template<int a0, int a1, int a2, int a3>
60 struct id32base64_4 : public id32_4<64, a0, a1, a2, a3>{};
61 template<int a0, int a1, int a2, int a3, int a4>
62 struct id32base64_5 : public id32_5<64, a0, a1, a2, a3, a4>{};
63 
64 // a0~a5: '0'~'9', 'A'~'Z', 'a'~'z' ==>> [0,63], upper and lower letters are equal
65 template<int a0>
66 struct id32base36_1 : public id32_1<36, a0>{};
67 template<int a0, int a1>
68 struct id32base36_2 : public id32_2<36, a0, a1>{};
69 template<int a0, int a1, int a2>
70 struct id32base36_3 : public id32_3<36, a0, a1, a2>{};
71 template<int a0, int a1, int a2, int a3>
72 struct id32base36_4 : public id32_4<36, a0, a1, a2, a3>{};
73 template<int a0, int a1, int a2, int a3, int a4>
74 struct id32base36_5 : public id32_5<36, a0, a1, a2, a3, a4>{};
75 template<int a0, int a1, int a2, int a3, int a4, int a5>
76 struct id32base36_6 : public id32_6<36, a0, a1, a2, a3, a4, a5>{};
77 
78 namespace detail {
80 template<bool b, typename T1, typename T2> struct if_then_else;
81 template<typename T1, typename T2> struct if_then_else<true, T1, T2> { typedef T1 Type;};
82 template<typename T1, typename T2> struct if_then_else<false, T1, T2> { typedef T2 Type;};
83 template<int c> struct is_lower_letter { enum { value = c >= 97 && c <= 122 };};
84 template<int c> struct is_upper_letter { enum { value = c >= 65 && c <= 90 };};
85 template<int c> struct is_num { enum { value = c >= 48 && c <= 57 };};
86 template<int c> struct is_underline { enum { value = c == 95 }; };
87 template<int c> struct is_dot { enum { value = c == 46 };};
88 struct lower_letter_t; struct upper_letter_t; struct number_t; struct underline_t; struct dot_t;
89 template<int x, typename T> struct map64_helper;
90 template<int x> struct map64_helper<x, number_t> { enum { value = x-48}; };
91 template<int x> struct map64_helper<x, upper_letter_t> { enum { value = x-65+10}; };
92 template<int x> struct map64_helper<x, lower_letter_t> { enum { value = x-97+10+26}; };
93 template<int x> struct map64_helper<x, underline_t> { enum { value = 62}; };
94 template<int x> struct map64_helper<x, dot_t> { enum { value = 63}; };
95 struct invalid_char_must_be_number_26letters_underline_dot;
96 template<int x> struct map_base<64, x> {
97  enum {
98  value = map64_helper<x,
99  typename if_then_else<is_num<x>::value, number_t,
100  typename if_then_else<is_upper_letter<x>::value, upper_letter_t,
101  typename if_then_else<is_lower_letter<x>::value, lower_letter_t,
102  typename if_then_else<is_underline<x>::value, underline_t,
103  typename if_then_else<is_dot<x>::value, dot_t,
104  invalid_char_must_be_number_26letters_underline_dot>::Type>::Type>::Type>::Type>::Type>::value
105  };
106 };
107 template<int x, typename T> struct map36_helper;
108 template<int x> struct map36_helper<x, number_t> { enum { value = x-48}; };
109 template<int x> struct map36_helper<x, upper_letter_t> { enum { value = x-65+10}; };
110 template<int x> struct map36_helper<x, lower_letter_t> { enum { value = x-97+10}; };
111 struct invalid_char_must_be_number_26letters;
112 template<int x> struct map_base<36, x> {
113  enum {
114  value = map36_helper<x,
115  typename if_then_else<is_num<x>::value, number_t,
116  typename if_then_else<is_upper_letter<x>::value, upper_letter_t,
117  typename if_then_else<is_lower_letter<x>::value, lower_letter_t,
118  invalid_char_must_be_number_26letters>::Type>::Type>::Type>::value
119  };
120 };
121 // for FourCC
122 template<int> struct out_of_lower_bound {};
123 template<int> struct out_of_upper_bound {};
124 struct map_identical_t;
125 template<int x, typename T> struct map_identical_helper;
126 template<int x> struct map_identical_helper<x,map_identical_t>{enum{value=x};};
127 template<int x> struct map_base<256, x> {
128  enum {
132  map_identical_t>::Type>::Type>::value
133  };
134 };
135 } // namespace detail
136 } // namespace mkid
137 #endif // MKID_H
Definition: mkid.h:60
Definition: mkid.h:76
Definition: mkid.h:38
Definition: mkid.h:36
Definition: mkid.h:42
Definition: mkid.h:122
Definition: mkid.h:66
Definition: mkid.h:58
Definition: mkid.h:74
Definition: mkid.h:86
Definition: mkid.h:50
Definition: mkid.h:56
Definition: mkid.h:123
Definition: mkid.h:44
Definition: mkid.h:83
Definition: mkid.h:72
Definition: mkid.h:87
Definition: mkid.h:107
Definition: mkid.h:54
Definition: mkid.h:46
Definition: mkid.h:89
Definition: mkid.h:70
Definition: mkid.h:62
Definition: mkid.h:80
Example: int id1 = mkid::fourcc<&#39;H&#39;, &#39;E&#39;, &#39;V&#39;, &#39;C&#39;>::value; int id2 = mkid::id32base64_5<&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;>::value; int id3 = mkid::id32base36_6<&#39;M&#39;, &#39;r&#39;, &#39;W&#39;, &#39;a&#39;, &#39;n&#39;, &#39;g&#39;>::value; For (u)int32 result, base 64 accepts at most 5 characters, while base 36 accepts at most 6 characters.
Definition: mkid.h:30
Definition: mkid.h:84
Definition: mkid.h:32
Definition: mkid.h:85
Definition: mkid.h:40
Definition: mkid.h:68