Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
carbon_def.h
1 // @file C/carbon_def.h
2 //
3 // Internal implementation MACROS for C-linkable structs and function calls.
4 //
5 // Contains declaration MACROs implementation to define data structure formats.
6 // The implementations are hidden behind a second layer to simplify the
7 // header file the user will interact with.
8 //
9 // @note This header file must not be included directly and the
10 // MACROS defined in this file should not be accessed
11 // directly. Include and used the definitions from the file
12 // **<Carbon.h>** instead.
13 //
14 // The MIT License(MIT)
15 // @copyright 2015 Paul M Watt
16 // ****************************************************************************
17 #ifndef CARBON_DEF_H_INCLUDED
18 #define CARBON_DEF_H_INCLUDED
19 // Private Usage Include Guard ************************************************
20 // Only allow this header file to be included through Alchemy.h
21 #ifndef ALCHEMY_H_INCLUDED
22 # error Do not include this file directly. Use <Alchemy.h> instead
23 #endif
24 
25 
26 // ****************************************************************************
27 // If carbon is enabled, generate the extra C-types that are required.
28 //
29 #if defined(ALCHEMY_CARBONATE)
30 
31 #include <Pb/detail/int_defs.h>
32 #include <Pb/meta_macros.h>
33 
34 // ****************************************************************************
35 // Utility Constructs For Use With Carbon Types *******************************
36 // ****************************************************************************
37 #define EACH_C_PARAM(r, data, i, x) \
38  BOOST_PP_TUPLE_ELEM(2,0,x) BOOST_PP_TUPLE_ELEM(2,1,x);
39 
40 
41 #if defined(_MSC_VER)
42 
43 #define DEFINE_C_STRUCT_PARAMS(S) \
44  BOOST_PP_SEQ_FOR_EACH_I(EACH_C_PARAM, unused, BOOST_PP_VARIADIC_TO_SEQ(S))
45 
46 #else
47 
48 #define DEFINE_C_STRUCT_PARAMS(...) \
49  BOOST_PP_SEQ_FOR_EACH_I(EACH_C_PARAM, unused, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
50 
51 #endif
52 
53 // ****************************************************************************
54 // Abstracted Message Definition MACROS ***************************************
55 // Simplified user MACROS use these definitions.
56 // These definitions have been abstracted to simplify the user header files.
57 // ****************************************************************************
58 
59 // ****************************************************************************
60 // Defines the outer value container as well as the formatted type-list.
61 //
62 #ifdef __cplusplus
63 #define C_DECLARE_STRUCT_HEADER(F, ...) \
64  extern "C" typedef struct tag_##F \
65  { \
66  DEFINE_C_STRUCT_PARAMS(__VA_ARGS__) \
67  C_DECLARE_STRUCT_FOOTER(F)
68  //C_DECLARE_STRUCT_TO_MSG(F,__VA_ARGS__) \/
69  //C_DECLARE_MSG_TO_STRUCT(F,__VA_ARGS__)
70 
71 #else
72 #define C_DECLARE_STRUCT_HEADER(F, ...) \
73  typedef struct tag_##F \
74  { \
75  DEFINE_C_STRUCT_PARAMS(__VA_ARGS__) \
76  C_DECLARE_STRUCT_FOOTER(F)
77 #endif
78 
79 // ****************************************************************************
80 #define C_DECLARE_STRUCT_FOOTER(F) \
81  } F; \
82  extern const uint32_t k_##F##_id;
83 
84 
85 // ****************************************************************************
86 #define C_DATUM_X(T,P) (T,P)
87 
88 // ****************************************************************************
89 #define C_DECLARE_DATUM(T,P) C_DATUM_X(T,P)
90 
91 // ****************************************************************************
92 #define C_DECLARE_ARRAY_DATUM(T, N, P) C_DATUM_X(T,P[N])
93 
94 // ****************************************************************************
95 #define C_DECLARE_VECTOR_DATUM(T, N, P) C_DATUM_X(T*, P)
96 
97 // ****************************************************************************
98 #define C_DECLARE_ALLOCATOR_DATUM(T, A, N, P) C_DECLARE_ALLOC(T, N, P)
99 
100 
101 // ****************************************************************************
102 // Bit Fields *****************************************************************
103 // ****************************************************************************
104 
105 #define C_EACH_BIT_FIELD(r, data, i, x) \
106  x
107 
108 #if defined(_MSC_VER)
109 
110 #define C_DEFINE_PACKED_FIELDS(S) \
111  BOOST_PP_SEQ_FOR_EACH_I(C_EACH_BIT_FIELD, unused, BOOST_PP_VARIADIC_TO_SEQ(S))
112 
113 #else
114 
115 #define C_DEFINE_PACKED_FIELDS(...) \
116  BOOST_PP_SEQ_FOR_EACH_I(C_EACH_BIT_FIELD, unused, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
117 
118 #endif
119 
120 #ifdef __cplusplus
121 
122 // ****************************************************************************
123 # define C_DECLARE_PACKED_HEADER(T,P, ...) \
124  extern "C" typedef struct tag_##P \
125  { \
126  C_DEFINE_PACKED_FIELDS(__VA_ARGS__) \
127  } P;
128 
129 #else
130 
131 // ****************************************************************************
132 # define C_DECLARE_PACKED_HEADER(T,P, ...) \
133  typedef struct tag_##P \
134  { \
135  C_DEFINE_PACKED_FIELDS(__VA_ARGS__) \
136  } P;
137 #endif
138 
139 // ****************************************************************************
140 # define C_DECLARE_BIT_FIELD(IDX,P,N) \
141  unsigned int P:N;
142 
143 
144 
145 // Exclusive to C++ ***********************************************************
146 // This section contains MACRO Functions specific to C++ builds.
147 // These create conversion and dispatch functions for the exported types.
148 //
149 #ifdef __cplusplus
150 
152 
153 // Forward Declarations *******************************************************
154 template< typename T, typename U >
155 U& struct_to_msg(T& src, U& dest);
156 
157 template< typename T, typename U >
158 U& msg_to_struct(T& src, U& dest);
159 
160 END_NAMESPACE(C)
161 
162 #define EACH_C_VALUE(r, xlate, i, x) \
163  C::xlate(src.##BOOST_PP_TUPLE_ELEM(2,1,x), dest.##BOOST_PP_TUPLE_ELEM(2,1,x));
164 
165 
166 #if defined(_MSC_VER)
167 
168 #define DEFINE_C_TYPE_TRANSLATION(XLATE,S) \
169  BOOST_PP_SEQ_FOR_EACH_I(EACH_C_VALUE, XLATE, BOOST_PP_VARIADIC_TO_SEQ(S))
170 
171 #else
172 
173 #define DEFINE_C_TYPE_TRANSLATION(XLATE,...) \
174  BOOST_PP_SEQ_FOR_EACH_I(EACH_C_VALUE, XLATE, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
175 
176 #endif
177 
178 // ****************************************************************************
179 // Assigns the values of each field to the message.
180 //
181 #define C_DECLARE_STRUCT_TO_MSG(NAME, ...) \
182  BEGIN_NAMESPACE(C) \
183  template< > \
184  Hg::##NAME& struct_to_msg(NAME& src, Hg::##NAME& dest) \
185  { using namespace Hg; \
186  DEFINE_C_TYPE_TRANSLATION(struct_to_msg, __VA_ARGS__) \
187  } \
188  END_NAMESPACE(C)
189 
190 // ****************************************************************************
191 // Assigns the values of each field to the struct.
192 //
193 #define C_DECLARE_MSG_TO_STRUCT(NAME, ...) \
194  BEGIN_NAMESPACE(C) \
195  template< > \
196  NAME& msg_to_struct(Hg::##NAME& src, NAME& dest) \
197  { using namespace Hg; \
198  DEFINE_C_TYPE_TRANSLATION(msg_to_struct, __VA_ARGS__) \
199  } \
200  END_NAMESPACE(C)
201 
202 #else
203 // These functions used overloading
204 // and are not compatible with C.
205 #define C_DECLARE_STRUCT_TO_MSG(NAME, ...)
206 #define C_DECLARE_MSG_TO_STRUCT(NAME, ...)
207 
208 
209 #endif
210 
211 #else
212 
213 // ****************************************************************************
214 // Carbon has not been enabled.
215 // Define empty code generation MACROS
216 //
217 # define C_DECLARE_STRUCT_HEADER(F, ...)
218 # define C_DECLARE_STRUCT_FOOTER(F)
219 
220 # define C_DATUM_X(T,P)
221 # define C_DECLARE_DATUM(T,P)
222 # define C_DECLARE_ARRAY(T, N, P)
223 # define C_DECLARE_DYNAMIC(T, N, P)
224 # define C_DECLARE_ALLOCATOR(T, A, N, P)
225 
226 # define C_DECLARE_PACKED_HEADER(T,C, ...)
227 # define C_DECLARE_BIT_FIELD(IDX,P,N)
228 # define C_DECLARE_PACKED_FOOTER
229 
230 #endif
231 
232 
233 #endif