Adding initial support for null structs (but not generic ones)

This commit is contained in:
Pavel Shevaev 2023-05-05 12:12:49 +03:00
parent 7480928350
commit 8fb747a2e4
2 changed files with 32 additions and 16 deletions

View File

@ -348,23 +348,30 @@ function var_reset($name, \mtgType $type, $default = null)
else if($type instanceof \mtgMetaStruct)
{
$str = "";
$is_pod = $type->hasToken('POD');
if($is_pod)
$str .= "$name.reset(); ";
else
$str .= "Meta.Reset(ref $name); ";
if(!is_null_str($default))
{
$is_pod = $type->hasToken('POD');
if($is_pod)
$str .= "$name.reset(); ";
else
$str .= "Meta.Reset(ref $name); ";
}
if($default)
{
$default = is_array($default) ? $default : json_decode($default, true);
if(!is_array($default))
throw new Exception("Bad default value for struct: $default");
foreach($default as $k => $v)
if(is_array($default))
{
$kf = $type->getField($k);
$str .= var_reset("$name." . $kf->getName(), $kf->getType(), $v);
foreach($default as $k => $v)
{
$kf = $type->getField($k);
$str .= var_reset("$name." . $kf->getName(), $kf->getType(), $v);
}
}
else if($default === null)
$str .= "$name = default; ";
else
throw new Exception("Bad default value for struct: " . var_export($default, true));
}
}
else
@ -372,6 +379,11 @@ function var_reset($name, \mtgType $type, $default = null)
return $str;
}
function is_null_str($default)
{
return is_string($default) && json_decode($default, true) === null;
}
function var_sync($fname, \mtgType $type, $buf, array $tokens, $opts)
{
$optional = array_key_exists('optional', $tokens);

View File

@ -128,14 +128,18 @@ public FieldsMask fields_mask;
{%- macro decl_struct_field(o, f) -%}
{{_self.attributes(f)}}
public {{f.type|cs_type|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD') -%} {{_self.decl_init_value(f.type)}} {%- endif -%};
public {{f.type|cs_type|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD') -%} {{_self.decl_init_value(f)}} {%- endif -%};
{%- endmacro -%}
{% macro decl_init_value(type) %}
{%- if type is instanceof('\\mtgBuiltinType') -%}
{%- if type.isstring -%} = ""{%- endif -%}
{% macro decl_init_value(f) %}
{%- if f.type is instanceof('\\mtgBuiltinType') -%}
{%- if f.type.isstring -%} = ""{%- endif -%}
{%- else -%}
= new {{type|cs_type}}()
{%- if has_token(f, 'default') and token(f, 'default') == 'null' -%}
/*null*/
{%- else -%}
= new {{f.type|cs_type}}()
{%- endif -%}
{%- endif -%}
{% endmacro %}