EVE 1.0
string-table.h
Go to the documentation of this file.
1 #ifndef TOC_STRINGTABLE_H_INCLUDED
2 #define TOC_STRINGTABLE_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2014-2015, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief A part of TOC table compile-time generator
36  *
37  * @author DT, Jetro AS
38  */ /******************************************************************/
39 
40 #include "packed-sizeof.h"
41 
42 /* Table entry; contains string just for the selected ID */
43 template <class Id>
44 struct CTableEntry
45  : public CTableEntry<IntToType<Id::value - 1> >
46 {
47 };
48 
49 template <>
50 struct CTableEntry<IntToType<-1> >
51 {
52  template<class T>
53  static constexpr int getOffset(T) {
54  return 0;
55  }
56 };
57 
58 /* Check if the entries [1..Id] is convertable to the U* */
59 template <class Id, class U>
60 constexpr bool checkDuplicate(Id, U* ptr)
61 {
62  return CTableEntry<Id>::convertable(ptr) ? true : (
63  checkDuplicate(IntToType<Id::value - 1>(), ptr));
64 }
65 
66 /* Stop recursion. */
67 template <class U>
68 constexpr bool checkDuplicate(IntToType<-1>, U* ptr)
69 {
70  return false;
71 }
72 
73 /* Helper type, merges two types in one */
74 template <class T, class U>
75 struct MergeStringTables
76  : public T
77  , public U
78 {
79  using T::getOffset;
80  using U::getOffset;
81 };
82 
83 /* Deduct return type for makeStringTable */
84 template<class T>
85 struct string_table_t
86 {
87  typedef typename std::conditional<
88  /* Check for duplicate. */
89  CTableEntry<T>::important && !checkDuplicate(IntToType<T::value - 1>(), (typename CTableEntry<T>::type_t*)NULL),
90  /* Include the entry if the duplicate is not found */
91  MergeStringTables<decltype(makeStringTable(IntToType<T::value - 1>())), CTableEntry<T> >,
92  /* Skip the entry if the duplicate is found */
93  decltype(makeStringTable(IntToType<T::value - 1>()))
94  >::type type;
95 };
96 
97 /* Stop recursion */
98 template<>
99 struct string_table_t<IntToType<-1> >
100 {
101  typedef CTableEntry<IntToType<-1> > type;
102 };
103 
104 /* Recursively make the table. */
105 template <class T>
106 constexpr typename string_table_t<T>::type makeStringTable(T)
107 {
108  return MergeStringTables<
109  decltype(makeStringTable(IntToType<T::value - 1>())),
110  CTableEntry<T>
111  >();
112 }
113 
114 /* Stop recursion */
115 template <>
116 constexpr CTableEntry<IntToType<-1> > makeStringTable(IntToType<-1> t)
117 {
118  return CTableEntry<IntToType<-1> >();
119 }
120 
121 #define TOC_DECLARE_NAME(id, name) \
122 struct CTableSelector_ ## name ## _t; \
123 template <> \
124 struct CTableEntry<IntToType<id> > \
125 { \
126  typedef CTableSelector_ ## name ## _t type_t; \
127  typedef CTableEntry<IntToType<id> > name ## _t; \
128  const char name[sizeof(#name)] = #name; \
129  static constexpr bool important = true; \
130  static constexpr bool convertable(const void *) { \
131  return false; \
132  } \
133  static constexpr bool convertable(const type_t *) { \
134  return important; \
135  } \
136  static constexpr int raw_offset = \
137  packed_sizeof(makeStringTable(IntToType<id - 1>())); \
138  static constexpr int offset = (raw_offset == 1) ? \
139  0 : raw_offset; \
140  static constexpr int getOffset(); \
141 }
142 
143 template<class T>
144 struct CTableIntSelector;
145 
146 #define TOC_DECLARE_INT(id, val, is_important) \
147 template <> \
148 struct CTableEntry<IntToType<id> > \
149 { \
150  typedef CTableIntSelector<IntToType<val> > type_t; \
151  const int value = val; \
152  static constexpr bool important = is_important; \
153  static constexpr bool convertable(const void *) { \
154  return false; \
155  } \
156  static constexpr bool convertable(const type_t *) { \
157  return important; \
158  } \
159  static constexpr int raw_offset = \
160  packed_sizeof(makeStringTable(IntToType<id - 1>())); \
161  static constexpr int offset = (raw_offset == 1) ? \
162  0 : raw_offset; \
163  static constexpr int getOffset(IntToType<val>) { \
164  return (raw_offset == 1) ? 0 : raw_offset; \
165  } \
166 }; \
167 
168 template<class Length>
169 struct CStringPool
170 {
171  const typename string_table_t<Length>::type impl = makeStringTable(Length());
172 };
173 
174 #endif /* TOC_STRINGTABLE_H_INCLUDED */
A part of TOC table compile-time generator.