diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 02890b6..fe2a607 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -96,6 +96,12 @@ function _add_twig_support(\Twig\Environment $twig) return $opts; } )); + $twig->addFunction(new \Twig\TwigFunction('field_reset', + function($name, $type, $tokens) + { + return field_reset($name, $type, $tokens); + } + )); $twig->addFunction(new \Twig\TwigFunction('buf2var', function($name, $fname, $type, $buf, $tokens, $is_ptr) { @@ -201,6 +207,68 @@ function builtin_type_prefix(\mtgBuiltinType $type) } } +function field_reset($name, \mtgType $type, array $tokens) +{ + $str = ''; + + if($type instanceof \mtgBuiltinType) + { + if($type->isNumeric()) + { + if(array_key_exists('default', $tokens) && is_numeric($tokens['default'])) + $str .= "self.$name = ".$tokens['default']; + else + $str .= " self.$name = 0"; + } + else if($type->isString()) + { + if(array_key_exists('default', $tokens)) + $str .= "self.$name = ".$tokens['default']; + else + $str .= "self.$name = \"\""; + } + else if($type->isBool()) + { + if(array_key_exists('default', $tokens)) + $str .= "self.$name = ".$tokens['default']; + else + $str .= "self.$name = false"; + } + else if($type->isBlob()) + { + if(array_key_exists('default', $tokens)) + $str .= "self.$name = ".$tokens['default']; + else + $str .= "self.$name = nil"; + } + else + throw new Exception("Unknown type '$type'"); + } + else if($type instanceof \mtgArrType) + { + $str = "if self.$name == nil { \nself.$name = make(".go_type($field->getType(), $field->getTokens()).",0) \n}\n "; + $str .= "self.$name = self.{$name}[0:0]"; + } + else if($type instanceof \mtgMetaEnum) + { + if(array_key_exists('default', $tokens)) + $str = "self.$name = ".go_type($field->getType(), $field->getTokens())."_".trim($tokens['default'], '"'); + else + $str = "self.$name = 0"; + } + else if($type instanceof \mtgMetaStruct) + { + $is_virtual = array_key_exists("virtual", $tokens); + if($is_virtual) + $str .= "self.$name = New".ltrim(go_type($field->getType(), $field->getTokens()),'I')."() "; + else + $str .= "self.$name.Reset()"; + } + else + throw new Exception("Unknown type '$type'"); + return $str; +} + function buf2var($name, $fname, \mtgType $type, $buf, array $tokens = array(), $is_ptr = false) { $str = ''; diff --git a/tpl/macro.twig b/tpl/macro.twig index bc553c4..cbaff20 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -70,7 +70,7 @@ func New{{o.name}}() *{{o.name}} { return item } func (self *{{o.name}}) Reset() { - %fields_reset% + {{_self.struct_reset_fields(o)}} } func (self *{{o.name}}) Read(reader meta.Reader) error { return meta.ReadStruct(reader, self, "") @@ -132,6 +132,22 @@ fieldsMask meta.FieldsMask //@bitfields support {% endmacro %} +{% macro struct_reset_fields(o) %} + +{%- if o.parent ~%} + self.{{o.parent.name}}.Reset() +{%- endif ~%} + +{%- for f in o.fields ~%} +{{field_reset(f.name|ucfirst, f.type, f.tokens)}} +{%- endfor ~%} + +{%if has_token(o, 'bitfields') %} +self.fieldsMask = meta.FieldsMask{} +{%- endif ~%} + +{% endmacro %} + {% macro struct_read_fields(o) %} {%if has_token(o, 'bitfields') %}