All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.thrift_cpp.include.interface_erpc_mem.body.vm Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
#parse("thrift_cpp/include/cxx_schema_include.vm")
#parse("thrift_cpp/include/cxx.helper.vm")
#set( $output_basename = "${schemainfo.programName}_erpc_mem")
#set( $output_filename = "${output_basename}.${source_type}")
$codewriter.setCurrentCxxFilename($output_filename)
#define($max_size_macro_name)${type.baseClass.simpleName}_${name}_MAX_SIZE#end
##############################
$header_begin
/**
 * $interfaceName 接口数据类型初始化与内存释放管理 
* $GENERAED_BY
*/ #if( $source_type == "cpp" ) \#include \#include \#include "${output_basename}.h" #else \#include "${schemainfo.programName}.h" \#include "${schemainfo.programName}_constant.h" #end #if( $source_type == "h" ) #ifdef __cplusplus extern "C" { #endif #end##if source_type #foreach($entry in $structGenseqMap.entries()) #set($package = $entry.key) #foreach($type in $entry.value) #if($type.bean) #if(${schemainfo.isThriftStruct($type.baseClass)}) #set($thriftTypeMetadata = ${schemainfo.getThriftStructMetadata($type.baseClass)}) #elseif(${schemainfo.isDecoratorType($type.baseClass)}) #set($thriftTypeMetadata = ${schemainfo.getThriftStructMetadata($type.decoratorClassName)}) #end #set($erpcClass = ${thriftTypeMetadata.structName} ) #set($thriftStub = ${schemainfo.toStubCxxType($type.baseClass)}) #ifdef __cplusplus ######## init erpcType ########### void init_${erpcClass}_ref(${erpcClass}&obj)$method_declare_suffix #if( $source_type == "cpp" ) { memset(&obj,0,sizeof(${erpcClass})); #foreach($name in $type.fields) #set($field = ${type.getField($name)}) #set($fieldMetadata = ${thriftTypeMetadata.getField($name)}) #if(${type.initValueOf($field)}) #set($initValue = ${type.initValueOf($field)}) #if($fieldMetadata.pointer) #elseif(${fieldMetadata.canPrimitive} && $initValue != "0") obj.m_$name = $initValue; #elseif(${fieldMetadata.erpcType.protocolType.string} && ${fieldMetadata.preAlloc} && ${initValue.length()} > 0) strncpy(obj.m_$name,"$initValue",${max_size_macro_name}); #elseif(${fieldMetadata.erpcType.protocolType.string} && ${initValue.length()} > 0) obj.m_$name = malloc(${initValue.length()} + 1); if(obj.m_$name){ strcpy(obj.m_$name,"$initValue"); } #elseif(${fieldMetadata.erpcType.protocolType.binary} && ${initValue.length()} > 0) obj.m_$name->data = malloc(${initValue.length()} >> 1); if(obj.m_$name->data){ hex_to_binary(obj.m_$name->data,"$initValue",${initValue.length()}); obj.m_$name->dataLength = ${initValue.length()} >> 1; } #end #end##if $initValue #end##foreach name } #end##$source_type ######## thrift stub type to eRPC type ########## // free eRPC type(ref) void free_${erpcClass}_ref(${erpcClass}&obj)$method_declare_suffix #if( $source_type == "cpp" ) { #foreach($name in $type.fields) #set($field = ${type.getField($name)}) #set($fieldMetadata = ${thriftTypeMetadata.getField($name)}) #set($addFreeSelf = "") #if($fieldMetadata.erpcType.protocolType.struct || $fieldMetadata.erpcType.protocolType.union || $fieldMetadata.erpcType.protocolType.list || $fieldMetadata.erpcType.protocolType.binary) #set($free_name = "free_${fieldMetadata.erpcType.ctype}") #set($addFreeSelf = ", true") #if($fieldMetadata.preAlloc || $fieldMetadata.erpcType.protocolType.list || $fieldMetadata.erpcType.protocolType.binary)#set($free_name = "${free_name}_ref")#end #elseif($fieldMetadata.pointer) #set($free_name = "free") #else #set($free_name = "") #end #if($free_name != "") #if($fieldMetadata.pointer) if(obj.m_$name){ ${free_name}(obj.m_$name$!{addFreeSelf}); obj.m_$name = nullptr; } #else ${free_name}(obj.m_$name); #end###if($fieldMetadata.pointer) #end###if($free_name) #end###foreach($name in $type.fields) init_${erpcClass}_ref(obj); } #end##if source_type #endif /** __cplusplus */ void init_${erpcClass}(${erpcClass}*obj)$method_declare_suffix #if( $source_type == "cpp" ) { if(obj){ memset(obj,0,sizeof(${erpcClass})); #foreach($name in $type.fields) #set($field = ${type.getField($name)}) #set($fieldMetadata = ${thriftTypeMetadata.getField($name)}) #if(${type.initValueOf($field)}) #set($initValue = ${type.initValueOf($field)}) #if($fieldMetadata.pointer) #elseif(${fieldMetadata.canPrimitive} && $initValue != "0") obj->m_$name = $initValue; #elseif(${fieldMetadata.erpcType.protocolType.string} && ${fieldMetadata.preAlloc} && ${initValue.length()} > 0) strncpy(obj->m_$name,"$initValue",${max_size_macro_name}); #elseif(${fieldMetadata.erpcType.protocolType.string} && ${initValue.length()} > 0) obj->m_$name = malloc(${initValue.length()} + 1); if(obj->m_$name){ strcpy(obj->m_$name,"$initValue"); } #elseif(${fieldMetadata.erpcType.protocolType.binary} && ${initValue.length()} > 0) obj->m_$name->data = malloc(${initValue.length()} >> 1); if(obj->m_$name->data){ hex_to_binary(obj->m_$name->data,"$initValue",${initValue.length()}); obj->m_$name->dataLength = ${initValue.length()} >> 1; } #end #end##if $initValue #end##foreach name } } #end##$source_type // free eRPC type(ponter) void free_${erpcClass}(${erpcClass} *obj,bool freeSelf)$method_declare_suffix #if( $source_type == "cpp" ) { if(obj){ #foreach($name in $type.fields) #set($field = ${type.getField($name)}) #set($fieldMetadata = ${thriftTypeMetadata.getField($name)}) #set($addFreeSelf = "") #if($fieldMetadata.erpcType.protocolType.struct || $fieldMetadata.erpcType.protocolType.union || $fieldMetadata.erpcType.protocolType.list || $fieldMetadata.erpcType.protocolType.binary) #set($free_name = "free_${fieldMetadata.erpcType.ctype}") #set($addFreeSelf = ", true") #if($fieldMetadata.preAlloc || $fieldMetadata.erpcType.protocolType.list || $fieldMetadata.erpcType.protocolType.binary)#set($free_name = "${free_name}_ref")#end #elseif($fieldMetadata.pointer) #set($free_name = "free") #else #set($free_name = "") #end #if($free_name != "") #if($fieldMetadata.pointer) if(obj->m_$name){ ${free_name}(obj->m_$name$!{addFreeSelf}); obj->m_$name = nullptr; } #else ${free_name}(obj->m_$name); #end###if($fieldMetadata.pointer) #end###if($free_name) #end###foreach($name in $type.fields) if(freeSelf){ free(obj); }else{ init_${erpcClass}(obj); } } } #end##if source_type #end###if($type.bean) #end##foreach($type) #end##foreach($entry) #### 结构列表 ##### ///////////struct list type ////////////// #foreach($listType in ${schemainfo.getCollectionStructs(true)}) #ifdef __cplusplus // set elements,elementsCount to zero void init_${listType.ctype}_ref(${listType.ctype} &list)$method_declare_suffix #if( $source_type == "cpp" ) { list.elements = nullptr; list.elementsCount = 0; } #end##if source_type void free_${listType.ctype}_ref(${listType.ctype} &list)$method_declare_suffix #if( $source_type == "cpp" ) { if (list.elements) { for (decltype(list.elementsCount) i = 0; i < list.elementsCount; ++i) { free_${listType.valueType.ctype}_ref(list.elements[i]); } free(list.elements); list.elements = nullptr; list.elementsCount = 0; } } #end##if source_type // alloc memory for element count specified by size for list struct void alloc_${listType.ctype}_ref(${listType.ctype} &list,size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if (size && list.elementsCount != size) { free_${listType.ctype}_ref(list); list.elements = (${listType.valueType.ctype}*)malloc(size * sizeof(${listType.valueType.ctype})); if(list.elements){ for (decltype(list.elementsCount) i = 0; i < list.elementsCount; ++i) { init_${listType.valueType.ctype}_ref(list.elements[i]); } list.elementsCount = (decltype(list.elementsCount))size; } }else if(!size){ free_${listType.ctype}_ref(list); } } #end##if source_type #endif /** __cplusplus */ // set elements,elementsCount to zero void init_${listType.ctype}(${listType.ctype} *plist)$method_declare_suffix #if( $source_type == "cpp" ) { if(plist){ plist->elements = nullptr; plist->elementsCount = 0; } } #end##if source_type void free_${listType.ctype}(${listType.ctype} *plist,bool freeSelf)$method_declare_suffix #if( $source_type == "cpp" ) { if (plist) { if (plist->elements) { for (decltype(plist->elementsCount) i = 0; i < plist->elementsCount; ++i) { free_${listType.valueType.ctype}(&plist->elements[i],false); } free(plist->elements); plist->elements = nullptr; plist->elementsCount = 0; } if(freeSelf){ free(plist); } } } #end##if source_type // alloc memory for element count specified by size for list struct(pointer) void alloc_${listType.ctype}(${listType.ctype} *plist,size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if(plist){ if (size && plist->elementsCount != size) { free_${listType.ctype}(plist,false); plist->elements = (${listType.valueType.ctype}*)malloc(size * sizeof(${listType.valueType.ctype})); if(plist->elements){ for (decltype(plist->elementsCount) i = 0; i < plist->elementsCount; ++i) { init_${listType.valueType.ctype}_ref(plist->elements[i]); } plist->elementsCount = (decltype(plist->elementsCount))size; } }else if(!size){ free_${listType.ctype}(plist,false); } } } #end##if source_type #end###foreach($listtype in ${schemainfo.getCollectionStructs()}) #### 非结构列表类型 ##### /////// no struct list type /////////// #foreach($listType in ${schemainfo.getCollectionStructs(false)}) #ifdef __cplusplus // set elements,elementsCount to zero void init_${listType.ctype}_ref(${listType.ctype} &list)$method_declare_suffix #if( $source_type == "cpp" ) { list.elements = nullptr; list.elementsCount = 0; } #end##if source_type // free all struct in elements // free elements pointer if not nullptr ,and set elements,elementsCount to zero void free_${listType.ctype}_ref(${listType.ctype} &list)$method_declare_suffix #if( $source_type == "cpp" ) { if (list.elements) { #if($listType.valueType.protocolType.string) for (decltype(list.elementsCount) i = 0; i < list.elementsCount; ++i) { if (list.elements[i]) { free(list.elements[i]); } } #elseif($listType.valueType.protocolType.binary) for (decltype(list.elementsCount) i = 0; i < list.elementsCount; ++i) { free_binary_t_ref(list.elements[i]); } #else free(list.elements); #end list.elements = nullptr; list.elementsCount = 0; } } #end##if source_type // alloc memory for element count specified by size for list struct void alloc_${listType.ctype}_ref(${listType.ctype} &list,size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if (size && list.elementsCount != size) { free_${listType.ctype}_ref(list); list.elements = (decltype(list.elements))malloc(size * sizeof(${listType.valueType.ctype})); if(list.elements){ #if($listType.valueType.protocolType.binary) for (decltype(list.elementsCount) i = 0; i < list.elementsCount; ++i) { init_${listType.valueType.ctype}_ref(list.elements[i]); } #else memset(list.elements,0,size * sizeof(${listType.valueType.ctype})); #end list.elementsCount = (decltype(list.elementsCount))size; } }else if(!size){ free_${listType.ctype}_ref(list); } } #end##if source_type #endif /** __cplusplus */ // set elements,elementsCount to zero void init_${listType.ctype}(${listType.ctype} *plist)$method_declare_suffix #if( $source_type == "cpp" ) { if(plist){ plist->elements = nullptr; plist->elementsCount = 0; } } #end##if source_type // free all struct in elements // free elements pointer if not nullptr ,and set elements,elementsCount to zero // free plist pointer if freeSelf is true void free_${listType.ctype}(${listType.ctype} *plist,bool freeSelf)$method_declare_suffix #if( $source_type == "cpp" ) { if (plist) { if (plist->elements) { #if($listType.valueType.protocolType.string) for (decltype(plist->elementsCount) i = 0; i < plist->elementsCount; ++i) { if (plist->elements[i]) { free(plist->elements[i]); } } #elseif($listType.valueType.protocolType.binary) for (decltype(plist->elementsCount) i = 0; i < plist->elementsCount; ++i) { free_binary_t(&plist->elements[i],false); } #else free(plist->elements); #end plist->elements = nullptr; plist->elementsCount = 0; } if(freeSelf){ free(plist); } } } #end##if source_type // alloc memory for element count specified by size for list struct void alloc_${listType.ctype}(${listType.ctype} *plist,size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if(plist){ if (size && plist->elementsCount != size) { free_${listType.ctype}(plist,false); plist->elements = (decltype(plist->elements))malloc(size * sizeof(${listType.valueType.ctype})); if(plist->elements){ #if($listType.valueType.protocolType.binary) for (decltype(plist->elementsCount) i = 0; i < plist->elementsCount; ++i) { init_${listType.valueType.ctype}_ref(plist->elements[i]); } #else memset(plist->elements,0,size * sizeof(${listType.valueType.ctype})); #end plist->elementsCount = (decltype(plist->elementsCount))size; } }else if(!size){ free_${listType.ctype}(plist,false); } } } #end##if source_type #end###foreach($listtype in ${schemainfo.getCollectionStructs()}) \#if _USE_BINARY #ifdef __cplusplus // set data,dataLength to zero void init_binary_t_ref(binary_t &obj)$method_declare_suffix #if( $source_type == "cpp" ) { obj.data = nullptr; obj.dataLength = 0; } #end##if source_type // free data pointer if not nullptr ,and set data,dataLength to zero void free_binary_t_ref(binary_t &obj)$method_declare_suffix #if( $source_type == "cpp" ) { if (obj.data) { free(obj.data); obj.data = nullptr; } obj.dataLength = 0; } #end##if source_type void alloc_binary_t_ref(binary_t &obj, size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if (nullptr == obj.data) { obj.data = (decltype(obj.data))malloc(size); if (obj.data) { obj.dataLength = (decltype(obj.dataLength))size; } } else if (obj.dataLength != size) { auto p = (decltype(obj.data))realloc(obj.data, size); if (p) { obj.data = p; obj.dataLength = (decltype(obj.dataLength))size; } } } #end##if source_type #endif /** __cplusplus */ // set data,dataLength to zero void init_binary_t(binary_t *obj)$method_declare_suffix #if( $source_type == "cpp" ) { if (obj) { obj->data = nullptr; obj->dataLength = 0; } } #end##if source_type // free data pointer if not nullptr ,and set data,dataLength to zero // free obj pointer if freeSelf is true void free_binary_t(binary_t *obj,bool freeSelf)$method_declare_suffix #if( $source_type == "cpp" ) { if (obj && obj->data) { free(obj->data); obj->data = nullptr; obj->dataLength = 0; if(freeSelf){ free(obj); } } } #end##if source_type void alloc_binary_t(binary_t *obj, size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if(obj){ if (nullptr == obj->data) { obj->data = (decltype(obj->data))malloc(size); if (obj->data) { obj->dataLength = (decltype(obj->dataLength))size; } } else if (obj->dataLength != size) { auto p = (decltype(obj->data))realloc(obj->data, size); if (p) { obj->data = p; obj->dataLength = (decltype(obj->dataLength))size; } } } } #end##if source_type #endif /** _USE_BINARY */ #if( $source_type == "cpp" ) static const char digits[] = "0123456789abcdef"; static uint8_t from_hex_char(char c) { if (c >= '0' && c <= '9') { return c - '0'; } if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } return 0; } #end##if source_type /// length of dst must be size/2 void hex_to_binary(void *dst, const char* src, size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { size &= ~1; if (dst && src && size > 0) { for (size_t i = 0,j = 0; i < size; i = i + 2 ) { ((uint8_t*)dst)[ j++ ] = (from_hex_char(src[ i ]) << 4 ) + from_hex_char(src[ i + 1 ]); } } } #end##if source_type /// length of dst must be size*2 void binary_to_hex(char* dst, const void* src, size_t size)$method_declare_suffix #if( $source_type == "cpp" ) { if (dst && src && size > 0) { size_t j = 0; for (size_t i = 0; i < size; ++i) { auto c = ((uint8_t*)src)[i]; dst[ j++ ] = digits[ c >> 4 ]; dst[ j++ ] = digits[ c & 0x0f ]; } dst[j] = '\0'; } } #end##if source_type #if( $source_type == "h" ) #ifdef __cplusplus } #endif #end $header_end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy