EVE 1.0
label-table.h
Go to the documentation of this file.
1 #ifndef TOC_LABEL_TABLE_H_INCLUDED
2 #define TOC_LABEL_TABLE_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 CLabelTableEntry;
45 
46 template <>
47 struct CLabelTableEntry<IntToType<-1> >
48 {};
49 
50 /* Helper type, merges two types in one */
51 template <class T, class U>
52 struct MergeLabelTables
53  : public T
54  , public U
55 {};
56 
57 /* Deduct return type for makeLabelTable */
58 template<class T>
59 struct label_table_t
60 {
61  typedef typename std::conditional<
62  /* Check for empty string. */
63  (sizeof(CLabelTableEntry<T>) != 1),
64  /* Include the entry if the duplicate is not found */
65  MergeLabelTables<decltype(makeLabelTable(IntToType<T::value - 1>())), CLabelTableEntry<T> >,
66  /* Skip the entry if the duplicate is found */
67  decltype(makeLabelTable(IntToType<T::value - 1>()))
68  >::type type;
69 };
70 
71 /* Stop recursion */
72 template<>
73 struct label_table_t<IntToType<-1> >
74 {
75  typedef CLabelTableEntry<IntToType<-1> > type;
76 };
77 
78 /* Recursively make the table. */
79 template <class T>
80 constexpr typename label_table_t<T>::type makeLabelTable(T)
81 {
82  return MergeLabelTables<
83  decltype(makeLabelTable(IntToType<T::value - 1>())),
84  CLabelTableEntry<T>
85  >();
86 }
87 
88 /* Stop recursion */
89 template <>
90 constexpr CLabelTableEntry<IntToType<-1> > makeLabelTable(IntToType<-1> t)
91 {
92  return CLabelTableEntry<IntToType<-1> >();
93 }
94 
95 #define TOC_DECLARE_LABEL(id, label) \
96 template <> \
97 struct CLabelTableEntry<IntToType<id> > \
98 { \
99  const char content[sizeof(label)] = label; \
100  static constexpr int raw_offset = \
101  packed_sizeof(makeLabelTable(IntToType<id - 1>())); \
102  static constexpr int offset = (sizeof(content) == 1) ? \
103  -1 : raw_offset == 1 ? 0 : raw_offset; \
104 }
105 
106 template<class Length>
107 struct CLabelPool
108 {
109  const typename label_table_t<Length>::type impl = makeLabelTable(Length());
110 };
111 
112 #endif
A part of TOC table compile-time generator.