EVE 1.0
ble-id-table.h
Go to the documentation of this file.
1 #ifndef TOC_BLE_ID_TABLE_H_INCLUDED
2 #define TOC_BLE_ID_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 /* Table entry; contains string just for the selected ID */
41 template <class Id>
42 struct CBleIdEntry
43  : public CBleIdEntry<IntToType<Id::value - 1> >
44 {
45  static constexpr bool important = false;
46 };
47 
48 template <>
49 struct CBleIdEntry<IntToType<-1> >
50 {
51  static constexpr bool empty = true;
52 };
53 
54 #if 0
55 /* Check if the entries [1..Id] is convertable to the U* */
56 template <class Id, class U>
57 constexpr bool checkDuplicate(Id, U* ptr)
58 {
59  return CBleIdEntry<Id>::convertable(ptr) ? true : (
60  checkDuplicate(IntToType<Id::value - 1>(), ptr));
61 }
62 
63 /* Stop recursion. */
64 template <class U>
65 constexpr bool checkDuplicate(IntToType<-1>, U* ptr)
66 {
67  return false;
68 }
69 #endif
70 
71 /* Helper type, merges two types in one */
72 template <class T, class U>
73 struct MergeBleIdTables
74  : public T
75  , public U
76 {
77  using U::empty;
78 };
79 
80 #ifndef ECLIPSE_STUB_CODE_ANALYSE
81 /* Deduct return type for makeBleIdTable */
82 template<class T>
83 struct CBleIdTable
84 {
85  typedef typename std::conditional<
86  /* Check for BLE flag. */
87  CBleIdEntry<T>::important,
88  /* Include the entry if the parameter is a BLE parameter */
89  MergeBleIdTables<decltype(makeBleIdTable(IntToType<T::value - 1>())), CBleIdEntry<T> >,
90  /* Skip the entry if the parameter is not a BLE parameter */
91  decltype(makeBleIdTable(IntToType<T::value - 1>()))
92  >::type type;
93 };
94 #endif /* ECLIPSE_STUB_CODE_ANALYSE */
95 
96 /* Stop recursion */
97 template<>
98 struct CBleIdTable<IntToType<-1> >
99 {
100  typedef CBleIdEntry<IntToType<-1> > type;
101 };
102 
103 /* Recursively make the table. */
104 template <class T>
105 constexpr typename CBleIdTable<T>::type makeBleIdTable(T)
106 {
107  return MergeBleIdTables<
108  decltype(makeBleIdTable(IntToType<T::value - 1>())),
109  CBleIdEntry<T>
110  >();
111 }
112 
113 /* Stop recursion */
114 template <>
115 constexpr CBleIdEntry<IntToType<-1> > makeBleIdTable(IntToType<-1> t)
116 {
117  return CBleIdEntry<IntToType<-1> >();
118 }
119 
120 #define TOC_DECLARE_BLE_ID(traits) \
121 template <> \
122 struct CBleIdEntry<IntToType<traits::m_id> > \
123 { \
124  static constexpr bool empty = false; \
125  static constexpr bool important = traits::m_ble; \
126  const char content = traits::m_id; \
127 }
128 
129 template<class Length>
130 struct CBleIdTranslateTable
131 {
132  static constexpr typename CBleIdTable<Length>::type impl = makeBleIdTable(Length());
133  static constexpr bool empty = impl.empty;
134  const typename CBleIdTable<Length>::type instance = impl;
135 };
136 
137 #endif /* TOC_BLE_ID_TABLE_H_INCLUDED */