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.h
Go to the documentation of this file.
1 /// @file Carbon.h
2 ///
3 /// Carbon generates c-linkable interfaces for constructs generated by Alchemy.
4 ///
5 /// The MIT License(MIT)
6 ///
7 /// @copyright 2015 Paul M Watt
8 ///
9 /// Permission is hereby granted, free of charge, to any person obtaining a copy
10 /// of this software and associated documentation files(the "Software"), to deal
11 /// in the Software without restriction, including without limitation the rights
12 /// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
13 /// copies of the Software, and to permit persons to whom the Software is
14 /// furnished to do so, subject to the following conditions :
15 ///
16 /// The above copyright notice and this permission notice shall be included in
17 /// all copies or substantial portions of the Software.
18 ///
19 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
22 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /// THE SOFTWARE.
26 ///
27 // ***************************************************************************
28 #ifndef CARBON_H_INCLUDED
29 #define CARBON_H_INCLUDED
30 // Includes ******************************************************************
31 #define ALCHEMY_CARBONATE
32 #include <Alchemy.h>
33 
34 // The definition that indicates these
35 // functions will be publically accessible.
36 #if defined(_WIN32)
37 
38 # if defined(ALCHEMY_EXPORT)
39 # define ALCHEMY_API __declspec(dllexport)
40 # else
41 # define ALCHEMY_API __declspec(dllimport)
42 # endif
43 
44 #else
45 
46 #define ALCHEMY_API
47 
48 #endif
49 
50 
51 
52 // Disable name-mangling for these
53 // functions when compiled with C++.
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif
58 
59 // ****************************************************************************
60 typedef unsigned char Hg_msg_t;
61 typedef unsigned long Hg_type_t;
62 
63 // ****************************************************************************
64 /// Reports the type of endianess of the local system.
65 ///
66 /// @return
67 ///
68 ALCHEMY_API
69 int Hg_local_endianess();
70 
71 // ****************************************************************************
72 /// Creates a new Hg message object of the specified type.
73 /// The caller must call Hg_destroy when they are done with the returned
74 /// object to return the messages allocated memory.
75 ///
76 /// @param msg_type[in]
77 ///
78 /// @return On success, an allocated message pointer is returned.
79 /// This pointer can be cast to the structure that
80 /// matches the type of requested message.
81 /// 0 is returned on failure.
82 ///
83 ALCHEMY_API
84 Hg_msg_t* Hg_create(
85  Hg_type_t msg_type
86 );
87 
88 // ****************************************************************************
89 /// Creates a complete clone of an existing Hg message object.
90 /// The caller must call Hg_destroy when they are done with the returned
91 /// object to return the messages allocated memory.
92 ///
93 /// @param p_src[in] A valid message object that should be cloned.
94 /// All of the data inside of the message, including
95 /// dynamically allocated space will be initialized to
96 /// match the source message.
97 ///
98 /// @return On success, an allocated message pointer is returned.
99 /// This pointer can be cast to the structure that
100 /// matches the type of requested message.
101 /// 0 is returned on failure.
102 ///
103 ALCHEMY_API
104 Hg_msg_t* Hg_clone(
105  const Hg_msg_t* p_src
106 );
107 
108 // ****************************************************************************
109 /// The specified message is destroyed.
110 /// ALL memory is reclaimed, including dynamically allocated space.
111 ///
112 /// @param p_msg[in] The previously allocated message to be destroyed.
113 /// This message should not be used after it is passed
114 /// to this function.
115 ///
116 ALCHEMY_API
117 void Hg_destroy(
118  Hg_msg_t* p_msg
119 );
120 
121 // ****************************************************************************
122 /// Resize the buffer for a dynamically allocated field in an existing message.
123 ///
124 /// @param p_msg[inout]
125 /// @param p_field[inout]
126 /// @param len[in]
127 ///
128 /// @return The number of bytes allocated for the field is returned.
129 /// If no bytes can be allocated, 0 is returned.
130 ///
131 ALCHEMY_API
132 size_t Hg_resize_dynamic(
133  Hg_msg_t* p_msg,
134  void* p_field,
135  size_t len
136 );
137 
138 // ****************************************************************************
139 /// Returns the type of the specified message.
140 ///
141 /// @param p_msg[in] The message to query.
142 ///
143 /// @return The enumerated type identifying this message is returned.
144 ///
145 ALCHEMY_API
146 Hg_type_t Hg_type(
147  const Hg_msg_t* p_msg
148 );
149 
150 // ****************************************************************************
151 /// Returns the size of the specified message.
152 ///
153 /// @param p_msg[in] The message to query.
154 ///
155 /// @return The size of the buffer to hold the main message.
156 /// 0 is returned if the buffer does not represent a valid msg.
157 ///
158 ALCHEMY_API
159 size_t Hg_size(
160  const Hg_msg_t* p_msg
161 );
162 
163 // ****************************************************************************
164 /// Returns the number of bytes that are required to serialize this object.
165 ///
166 /// @param p_msg[in] The message to query.
167 ///
168 /// @return The calculated buffer size required to serialize
169 /// the specified message.
170 /// 0 is returned if the buffer does not represent a valid msg.
171 ///
172 ALCHEMY_API
173 size_t Hg_data_size(
174  const Hg_msg_t* p_msg
175 );
176 
177 // ****************************************************************************
178 /// Converts this object, in-place, to network byte-order.
179 /// No actions will be performed if the local platform is big-endian.
180 ///
181 /// @param p_msg[in] The message to convert.
182 ///
183 /// @return A status code indicating the result.
184 /// 0 indicates success
185 /// A negative value indicates an error.
186 ///
187 ALCHEMY_API
188 int Hg_to_network(
189  Hg_msg_t* p_msg
190 );
191 
192 // ****************************************************************************
193 /// Converts this object, in-place, to host byte-order.
194 /// No actions will be performed if the local platform is big-endian.
195 ///
196 /// @param p_msg[in] The message to convert.
197 ///
198 /// @return A status code indicating the result.
199 /// 0 indicates success
200 /// A negative value indicates an error.
201 ///
202 ALCHEMY_API
203 int Hg_to_host(
204  Hg_msg_t* p_msg
205 );
206 
207 // ****************************************************************************
208 /// Converts this object, in-place, to big-endian byte-order.
209 /// The message will always be converted as requested.
210 ///
211 /// @param p_msg[in] The message to convert.
212 ///
213 /// @return A status code indicating the result.
214 /// 0 indicates success
215 /// A negative value indicates an error.
216 ///
217 ALCHEMY_API
218 int Hg_to_big_end(
219  Hg_msg_t* p_msg
220 );
221 
222 // ****************************************************************************
223 /// Converts this object, in-place, to little-endian byte-order.
224 /// The message will always be converted as requested.
225 ///
226 /// @param p_msg[in] The message to convert.
227 ///
228 /// @return A status code indicating the result.
229 /// 0 indicates success
230 /// A negative value indicates an error.
231 ///
232 ALCHEMY_API
233 int Hg_to_little_end(
234  Hg_msg_t* p_msg
235 );
236 
237 // ****************************************************************************
238 /// Serializes the specified message into a buffer.
239 ///
240 /// @param p_msg[in] The message to serialize.
241 /// @param p_buffer[out] The buffer that will be written into.
242 /// @param len[in] The length of the output buffer.
243 ///
244 /// @return The number of bytes written to the serialized buffer.
245 /// 0 indicates an error occurred.
246 ///
247 ALCHEMY_API
248 size_t Hg_pack(
249  const Hg_msg_t* p_msg,
250  void* p_buffer,
251  size_t len
252 );
253 
254 // ****************************************************************************
255 /// Deserializes the specified message from a buffer.
256 ///
257 /// @param p_msg[in] The message to populate from the serialized buffer.
258 /// @param p_buffer[out] The buffer that will be read from.
259 /// @param len[in] The length of the input buffer.
260 ///
261 /// @return The number of bytes read from the serialized buffer.
262 /// 0 indicates an error occurred.
263 ///
264 ALCHEMY_API
265 size_t Hg_unpack(
266  Hg_msg_t* p_msg,
267  const void* p_buffer,
268  size_t len
269 );
270 
271 // ****************************************************************************
272 // Function pointer declarations for dynamically-loading the library.
273 //
274 typedef int (*pfn_Hg_local_endianess) ();
275 typedef Hg_msg_t* (*pfn_Hg_create) (Hg_type_t);
276 typedef void (*pfn_Hg_destroy) (Hg_msg_t*);
277 typedef Hg_type_t (*pfn_Hg_type) (const Hg_msg_t*);
278 typedef size_t (*pfn_Hg_size) (const Hg_msg_t*);
279 typedef size_t (*pfn_Hg_data_size) (const Hg_msg_t*);
280 typedef int (*pfn_Hg_to_network) (Hg_msg_t*);
281 typedef int (*pfn_Hg_to_host) (Hg_msg_t*);
282 typedef int (*pfn_Hg_to_big_end) (Hg_msg_t*);
283 typedef int (*pfn_Hg_to_little_end) (Hg_msg_t*);
284 typedef size_t (*pfn_Hg_pack) (const Hg_msg_t*, void*, size_t);
285 typedef size_t (*pfn_Hg_unpack) (Hg_msg_t*, const void*, size_t);
286 
287 
288 // End of name-mangling guard.
289 #ifdef __cplusplus
290 }
291 #endif
292 
293 #endif