From a0a96b0a459b6d19d1b8d3b4898f725e120a2762 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 22 Mar 2023 19:27:39 +0300 Subject: [PATCH] Adding initial support for @cs_accessor_interface --- src/codegen.inc.php | 70 +++++++++++++++++++++++++++++++++++++++++++++ tpl/macro.twig | 31 ++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index b13d2b5..df48d61 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -135,6 +135,18 @@ function _add_twig_support(\Twig\Environment $twig) return get_diff_related_units($o); } )); + $twig->addFunction(new \Twig\TwigFunction('get_all_accessor_interfaces', + function($o) + { + return get_all_accessor_interfaces($o); + } + )); + $twig->addFunction(new \Twig\TwigFunction('get_accessor_interfaces', + function($o) + { + return get_accessor_interfaces($o); + } + )); } function cs_type(\mtgType $type) @@ -483,3 +495,61 @@ function get_diff_related_units(\mtgMetaStruct $struct) } return $result; } + +class AccessorInterface +{ + public $field; + public $cs_interface_name; + + function getUID() + { + return $this->getInterfaceName(); + } + + function getInterfaceName() + { + return $this->cs_interface_name; + } + + function getCamelFieldName() + { + $name_parts = explode("_", $this->field->getName()); + $uc_parts = array(); + foreach($name_parts as $part) + $uc_parts[] = ucfirst($part); + return implode($uc_parts); + } +} + +function get_accessor_interfaces(\mtgMetaStruct $struct) +{ + $ifs = array(); + foreach($struct->getFields() as $field) + { + if($field->hasToken('cs_accessor_interface')) + { + $ai = new AccessorInterface(); + $ai->field = $field; + $ai->cs_interface_name = $field->getToken('cs_accessor_interface'); + $ifs[$ai->getUID()] = $ai; + } + } + return $ifs; +} + +function get_all_accessor_interfaces(\mtgMetaInfo $info) +{ + $all = array(); + foreach($info->getUnits() as $unit) + { + if($unit->object instanceof \mtgMetaStruct) + $all = array_merge($all, get_accessor_interfaces($unit->object)); + else if($unit->object instanceof \mtgMetaRPC) + { + $all = array_merge($all, get_accessor_interfaces($unit->object->getReq())); + $all = array_merge($all, get_accessor_interfaces($unit->object->getRsp())); + } + } + return $all; +} + diff --git a/tpl/macro.twig b/tpl/macro.twig index 99a5b28..07f9d75 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -11,6 +11,10 @@ {%- endif ~%} {%- endfor ~%} +{%- for ai in get_all_accessor_interfaces(meta) ~%} + {{ _self.decl_accessor_interface(ai) }} +{%- endfor ~%} + {% endmacro %} {% macro decl_struct(o, extra = '') %} @@ -110,6 +114,8 @@ public {{_self.struct_type(o)}} {{o.name}} {{_self.base_struct_class(o)}} {{_self.diffable_support(o)}} {%- endif -%} + {{_self.decl_struct_accessor_implements(o)}} + {{extra}} } @@ -169,6 +175,9 @@ ResetFieldMask(); {{- has_token(o, 'cloneable') and not o.parent ? ', IMetaCloneable' -}} {{- has_token(o, 'bhl_bind') and not o.parent ? ', bhl.ITyped' -}} {%- endif -%} +{%- for ai in get_accessor_interfaces(o) -%} +, {{ai.interfacename}} +{%- endfor -%} {%- endmacro -%} {%- macro override(o) -%} @@ -605,3 +614,25 @@ public class {{o.name}} : IRpc {% endif %} {% endmacro %} + +{% macro decl_accessor_interface(ai) ~%} +public interface {{ai.interfacename}} +{ + {{ai.field.type|cs_type}} Get{{ai.camelfieldname}}(); + void Set{{ai.camelfieldname}}({{ai.field.type|cs_type}} v); +} +{% endmacro %} + +{%- macro decl_struct_accessor_implements(o) -%} +{%- for ai in get_accessor_interfaces(o) ~%} +public {{ai.field.type|cs_type}} Get{{ai.camelfieldname}}() +{ + return {{ai.field.name}}; +} +public void Set{{ai.camelfieldname}}({{ai.field.type|cs_type}} v) +{ + this.{{ai.field.name}} = v; +} +{%- endfor ~%} +{% endmacro %} +