Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| SHA1 Hash: | e8df5ca723b56760b2c6d2c4243a26010d779307 |
|---|---|
| Date: | 2008-11-14 10:52:59 |
| User: | stephan |
| Comment: | updated for API changes |
Changes
Changes to src/createMarshaller.sh
| Old (55543153a1a71131) | New (2584ebf3c3dcf437) | |||
|---|---|---|---|---|
| 1 | #!/bin/bash | 1 | #!/bin/bash | |
| 2 | ######################################################################## | 2 | ######################################################################## | |
| 3 | # A quick hack to generate code for c11n_marshaller classes. | 3 | # A quick hack to generate code for c11n_marshaller classes. | |
| 4 | # Usage: $0 ClassName | 4 | # Usage: $0 ClassName | |
| 5 | # Output will be in ClassName_c11n.h and ClassName_c11n.c | 5 | # Output will be in ClassName_c11n.h and ClassName_c11n.c | |
| 14 hidden lines | ||||
| 20 | CLASS=$(echo ${class} | tr '[a-z]' '[A-Z]') | 20 | CLASS=$(echo ${class} | tr '[a-z]' '[A-Z]') | |
| 21 | 21 | |||
| 22 | guard="_${CLASS}_C11N_H_INCLUDED_" | 22 | guard="_${CLASS}_C11N_H_INCLUDED_" | |
| 23 | 23 | |||
| 24 | marshaller=${class}_c11n | 24 | marshaller=${class}_c11n | |
| 25 | marshallerT=${class}_c11n_t | < | ||
| 26 | 25 | |||
| 27 | ######################################################################## | 26 | ######################################################################## | |
| 28 | # H file: | 27 | # H file: | |
| 29 | { | 28 | { | |
| 30 | cat <<END_OF_SOURCE | 29 | cat <<END_OF_SOURCE | |
| 31 | #if !defined(${guard}) | 30 | #if !defined(${guard}) | |
| 32 | #define ${guard} 1 | 31 | #define ${guard} 1 | |
| 33 | 32 | |||
| > | 33 | #include "c11n.h" | ||
| > | 34 | |||
| 34 | #ifdef __cplusplus | 35 | #ifdef __cplusplus | |
| 35 | extern "C" { | 36 | extern "C" { | |
| 36 | #endif | 37 | #endif | |
| 37 | 38 | |||
| 38 | #include "c11n.h" | < | ||
| 39 | < | |||
| 40 | /** | 39 | /** | |
| 41 | c11n_marshaller instance for handling ${class} objects. | | | 40 | c11n_marshaller instance for de/serializing ${class} objects |
| | | 41 | using the c11n API. | ||
| 42 | */ | 42 | */ | |
| 43 | extern const c11n_marshaller * ${marshaller}; | 43 | extern const c11n_marshaller * ${marshaller}; | |
| 44 | 44 | |||
| 45 | #ifdef __cplusplus | 45 | #ifdef __cplusplus | |
| 46 | } /* extern "C" */ | 46 | } /* extern "C" */ | |
| 11 hidden lines | ||||
| 58 | ######################################################################## | 58 | ######################################################################## | |
| 59 | # C file: | 59 | # C file: | |
| 60 | { | 60 | { | |
| 61 | cat <<END_OF_SOURCE | 61 | cat <<END_OF_SOURCE | |
| 62 | /* | 62 | /* | |
| 63 | This is generated code. Search this file for TODO and OPTIONAL to find | | | 63 | This is generated code for use in serializing ${class} objects |
| | | 64 | using the c11n API. Search this file for TODO and OPTIONAL to find | ||
| 64 | the places which need to be filled out. | 65 | the places which need to be filled out. | |
| 65 | */ | 66 | */ | |
| 66 | #include <stdlib.h> /* malloc()/free() */ | 67 | #include <stdlib.h> /* malloc()/free() */ | |
| 67 | #include "${ofh}" | 68 | #include "${ofh}" | |
| 68 | #include "${classheader}" /** TODO: fix this header path, if necessary. */ | 69 | #include "${classheader}" /** TODO: fix this header path, if necessary. */ | |
| 69 | 70 | |||
| 70 | /** | < | ||
| 71 | c11n_marshaller type for ${class} objects. | < | ||
| 72 | */ | < | ||
| 73 | struct ${marshallerT} | < | ||
| 74 | { | < | ||
| 75 | c11n_marshaller_api api; | < | ||
| 76 | /** | < | ||
| 77 | OPTIONAL | < | ||
| 78 | 71 | |||
| 79 | Any custom members must go after the api member. The api | < | ||
| 80 | member must be named api. | < | ||
| 81 | < | |||
| 82 | The custom members will be accessible via the SELF macro in the | < | ||
| 83 | ${marshaller}_xxx() functions. | < | ||
| 84 | */ | < | ||
| 85 | }; | < | ||
| 86 | typedef struct ${marshallerT} ${marshallerT}; | < | ||
| 87 | < | |||
| 88 | /** Helper macro to cast the 'self' parameter to ${marshallerT} */ | < | ||
| 89 | #define SELF ((const ${marshallerT} *)self) | < | ||
| 90 | 72 | |||
| 91 | /** | 73 | /** | |
| 92 | Serialization implementation for ${class} objects. | | | 74 | Serialization implementation for ${class} objects. |
| 93 | 75 | |||
| 94 | No arguments may be 0. v must be-a ${class} object. | | | 76 | No arguments may be 0. v must be-a pointer to a ${class} object. |
| 95 | */ | 77 | */ | |
| 96 | static bool ${marshaller}_serialize( c11n_marshaller const * self, c11n_node * dest, void const * src ) | 78 | static bool ${marshaller}_serialize( c11n_marshaller const * self, c11n_node * dest, void const * src ) | |
| 97 | { | 79 | { | |
| 98 | if( !self || !dest || !src ) return false; | 80 | if( !self || !dest || !src ) return false; | |
| 99 | c11n_node_api.set_class( dest, self->api.classname ); | | | 81 | c11n_node_set_class( dest, self->api->classname ); |
| | | 82 | /** | ||
| | | 83 | TODO: | ||
| | | 84 | |||
| | | 85 | Store whatever data from src you want into dest. Return true | ||
| | | 86 | on success, false on error. | ||
| | | 87 | */ | ||
| | | 88 | return false; | ||
| | | 89 | } | ||
| | | 90 | |||
| | | 91 | /** | ||
| | | 92 | Deserialization implementation for ${class} objects. | ||
| | | 93 | |||
| | | 94 | No arguments may be 0. v must be-a pointer to a ${class} object. | ||
| | | 95 | */ | ||
| | | 96 | static bool ${marshaller}_deserialize( c11n_marshaller const * self, c11n_node const * src, void * dest ) | ||
| | | 97 | { | ||
| | | 98 | if( !self || !dest || !src ) return false; | ||
| | | 99 | if( ! c11n_compare_str( self->api->classname, c11n_node_get_class(src) ) ) | ||
| | | 100 | { /* type mismatch? */ | ||
| | | 101 | return false; | ||
| | | 102 | } | ||
| 100 | /** | 103 | /** | |
| 101 | TODO: | | | 104 | TODO: Restore whatever data from src you want into dest. |
| 102 | 105 | |||
| 103 | Store whatever data from src you want into dest. Return true on success, | | | 106 | Be sure to: |
| 104 | false on error. | | | 107 | |
| | | 108 | - BEFORE deserializing, clean up any existing allocated memory, so | ||
| | | 109 | we do not overwrite a pointer and leak memory. | ||
| | | 110 | |||
| | | 111 | - On error, clean up any allocated resources. | ||
| | | 112 | |||
| | | 113 | - Return true on success, false on error. | ||
| 105 | */ | 114 | */ | |
| 106 | return false; | 115 | return false; | |
| 107 | } | 116 | } | |
| 108 | 117 | |||
| 109 | /** Deserialization implementation for ${class} objects. | | | 118 | /** |
| 110 | | | 119 | Clears state of v, which must be-a pointer to a ${class} object. | |
| 111 | No arguments may be 0. v must be-a ${class} object. | | | ||
| 112 | */ | 120 | */ | |
| 113 | static bool ${marshaller}_deserialize( c11n_marshaller const * self, c11n_node const * src, void * dest ) | | | 121 | static void ${marshaller}_clear( c11n_marshaller const * self, void * v ) |
| 114 | { | 122 | { | |
| 115 | if( !self || !dest || !src ) return false; | | | 123 | if( self && v ) |
| 116 | if( ! c11n_compare_str( SELF->api.classname, c11n_node_api.get_class(src) ) ) | | | 124 | { |
| 117 | { /* type mismatch? */ | | | 125 | /** |
| 118 | return false; | | | 126 | TODO: |
| 119 | } | | | ||
| 120 | /** | | | ||
| 121 | TODO: Restore whatever data from src you want into dest. | | | ||
| 122 | 127 | |||
| 123 | Be sure to: | | | 128 | Add your impl here, ESPECIALLY if deserialize() allocated |
| 124 | | | 129 | any dynamic memory which is owned by the v object. See the | |
| 125 | - BEFORE deserializing, clean up any existing allocated memory, so we do not | | | 130 | c11n_marshaller_api docs for the exact requirements. |
| 126 | overwrite a pointer and leak memory. | | | 131 | */ |
| 127 | | | 132 | } | |
| 128 | - On error, clean up any allocated resources. | | | ||
| 129 | | | |||
| 130 | - Return true on success, false on error. | | | ||
| 131 | */ | | | ||
| 132 | return false; | | | ||
| 133 | } | 133 | } | |
| 134 | 134 | |||
| 135 | /** | 135 | /** | |
| 136 | Clears state of v, which must be-a ${class} object. | | | 136 | Destroys v, which must be-a $pointer to a ${class} object. |
| 137 | */ | 137 | */ | |
| 138 | static void ${marshaller}_clear( c11n_marshaller const * self, void * v ) | < | ||
| 139 | { | < | ||
| 140 | if( ! self || !v ) return; | < | ||
| 141 | /** | < | ||
| 142 | TODO: | < | ||
| 143 | < | |||
| 144 | Add your impl here, ESPECIALLY if deserialize() allocated any | < | ||
| 145 | dynamic memory which is owned by the v object. See the | < | ||
| 146 | c11n_marshaller_api docs for the exact requirements. | < | ||
| 147 | */ | < | ||
| 148 | } | < | ||
| 149 | < | |||
| 150 | /** Destroys v, which must be-a ${class} object. */ | < | ||
| 151 | static void ${marshaller}_destroy( c11n_marshaller const * self, void * v ) | 138 | static void ${marshaller}_destroy( c11n_marshaller const * self, void * v ) | |
| 152 | { | 139 | { | |
| 153 | SELF->api.clear( self, v ); | | | 140 | if( self ) |
| 154 | /** | | | 141 | { |
| 155 | TODO: if your type has a special destructor function, use | | | 142 | self->api->clear( self, v ); |
| 156 | it here instead of free(v). | | | 143 | /** |
| 157 | */ | | | 144 | TODO: |
| 158 | free(v); | | | 145 | |
| | | 146 | If ${class} has a special destructor function, use it | ||
| | | 147 | here instead of free(v). | ||
| | | 148 | */ | ||
| | | 149 | free(v); | ||
| | | 150 | } | ||
| 159 | } | 151 | } | |
| 160 | 152 | |||
| 161 | /** | 153 | /** | |
| 162 | Creates and returns a new ${class} object. | 154 | Creates and returns a new ${class} object. | |
| 163 | */ | 155 | */ | |
| 164 | static void * ${marshaller}_create( c11n_marshaller const * self ) | 156 | static void * ${marshaller}_create( c11n_marshaller const * self ) | |
| 165 | { | 157 | { | |
| 166 | ${class} * m = self ? ((${class}*)malloc(sizeof(${class}))) : 0; | 158 | ${class} * m = self ? ((${class}*)malloc(sizeof(${class}))) : 0; | |
| 167 | if( m ) | 159 | if( m ) | |
| 168 | { | 160 | { | |
| 169 | self->api.clear( self, m ); | | | 161 | self->api->clear( self, m ); |
| 170 | /* | 162 | /* | |
| 171 | TODO: initialize m to some default state | 163 | TODO: initialize m to some default state | |
| 172 | */ | 164 | */ | |
| 173 | } | 165 | } | |
| 174 | return m; | 166 | return m; | |
| 175 | } | 167 | } | |
| 176 | 168 | |||
| 177 | #undef SELF | | | 169 | static const c11n_marshaller_api c11n_marshaller_api_${marshaller} = |
| 178 | | | |||
| 179 | /** | | | ||
| 180 | Shared ${marshallerT} instance. | | | ||
| 181 | */ | | | ||
| 182 | static const struct ${marshallerT} ${marshallerT}_bogo = { | | | ||
| 183 | C11N_MARSHALLER_API_INIT("${class}", | 170 | C11N_MARSHALLER_API_INIT("${class}", | |
| 184 | ${marshaller}_serialize, | 171 | ${marshaller}_serialize, | |
| 185 | ${marshaller}_deserialize, | 172 | ${marshaller}_deserialize, | |
| 186 | ${marshaller}_create, | 173 | ${marshaller}_create, | |
| 187 | ${marshaller}_clear, | 174 | ${marshaller}_clear, | |
| 188 | ${marshaller}_destroy) | | | 175 | ${marshaller}_destroy); |
| 189 | }; | | | 176 | |
| | | 177 | |||
| | | 178 | /** | ||
| | | 179 | Shared ${marshaller} instance. | ||
| | | 180 | */ | ||
| | | 181 | static const struct c11n_marshaller ${marshaller}_bogo = { &c11n_marshaller_api_${marshaller} }; | ||
| 190 | 182 | |||
| 191 | /** | 183 | /** | |
| 192 | Public interface into ${marshallerT}. | | | 184 | Public interface into ${marshaller}. |
| 193 | */ | 185 | */ | |
| 194 | const c11n_marshaller * ${marshaller} = C11N_MARSHALLERC(&${marshallerT}_bogo); | | | 186 | const c11n_marshaller * ${marshaller} = &${marshaller}_bogo; |
| 195 | 187 | |||
| 196 | END_OF_SOURCE | 188 | END_OF_SOURCE | |
| 197 | } > $ofc | 189 | } > $ofc | |
| 198 | 190 | |||
| 199 | ######################################################################## | 191 | ######################################################################## | |
| 200 | 192 | |||
| 201 | echo "Done. Output is in [${ofh}] and [${ofc}]." | 193 | echo "Done. Output is in [${ofh}] and [${ofc}]." | |