libtrbase  1.0.2
Web server and task management solution.
serializable.h
Go to the documentation of this file.
1 /**
2  * \file
3  * Classes that implement this interface can be converted to an
4  * array of bytes and created from an array of bytes if it has
5  * the correct format. The format of th byte array is class
6  * specific.
7  *
8  * \author Georg Hopp
9  *
10  * \copyright
11  * Copyright © 2014 Georg Hopp
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #ifndef __TR_SERIALIZABLE_H__
28 #define __TR_SERIALIZABLE_H__
29 
30 #include "tr/interface.h"
31 
32 
33 typedef void (* fptr_serialize)(void *, unsigned char **, size_t *);
34 typedef void (* fptr_unserialize)(void *, const unsigned char *, size_t);
35 
36 TR_INTERFACE(TR_Serializable) {
37  TR_IFID;
38  fptr_serialize serialize;
39  fptr_unserialize unserialize;
40 };
41 
42 /**
43  * Serialize the given instance to a byte array
44  */
45 inline
46 void
48  void * serializable,
49  unsigned char ** serialized,
50  size_t * nserialized)
51 {
52  TR_CALL(serializable, TR_Serializable, serialize, serialized,
53  nserialized);
54 }
55 
56 /**
57  * Unerialize the given instance to a byte array
58  */
59 inline
60 void
62  void * serializable,
63  const unsigned char * serialized,
64  size_t nserialized)
65 {
66  TR_CALL(serializable, TR_Serializable, unserialize, serialized,
67  nserialized);
68 }
69 
70 #endif // __TR_SERIALIZABLE_H__
71 
72 // vim: set ts=4 sw=4:
#define TR_CALL(object, _iface, method,...)
Definition: class.h:256
void TR_unserialize(void *serializable, const unsigned char *serialized, size_t nserialized)
Definition: serializable.h:61
#define TR_IFID
Definition: interface.h:61
void(* fptr_unserialize)(void *, const unsigned char *, size_t)
Definition: serializable.h:34
TR_INTERFACE(TR_Serializable)
Definition: serializable.h:36
void(* fptr_serialize)(void *, unsigned char **, size_t *)
Definition: serializable.h:33
void TR_serialize(void *serializable, unsigned char **serialized, size_t *nserialized)
Definition: serializable.h:47