Пример #1
0
function serializeUserType($namespace, $userdef, $indent)
{
    global $module, $current_server, $derived_types;
    $type_type = @$userdef->type;
    $coded_type = @$type_type->coded_type;
    $struct = @$type_type->struct;
    if (!$coded_type && !$struct) {
        return;
    }
    if ($coded_type && isEnum($coded_type)) {
        return;
    }
    $type_name = $userdef->name;
    if (@$derived_types[$current_server][$type_name]) {
        return;
    }
    $indent2 = $indent . "\t";
    //put
    echo "{$indent}\nCERL_IO_BEGIN_PUTTER({$namespace}::{$type_name})\n";
    echo "{$indent2}IoTypeTraits<{$namespace}::{$type_name}>::putType(os);\n";
    if ($coded_type) {
        putCodedItems($namespace, $coded_type->items, $indent2);
    } else {
        $vars = $struct->vars;
        foreach ($vars as $var) {
            echo "{$indent2}NS_CERL_IO::put(os, val.{$var->name});\n";
        }
    }
    echo "{$indent}CERL_IO_END_PUTTER()\n";
    //get
    echo "\n";
    echo "{$indent}\nCERL_IO_BEGIN_GETTER({$namespace}::{$type_name})\n";
    echo "{$indent2}if (!IoTypeTraits<{$namespace}::{$type_name}>::getType(is))\n";
    echo "{$indent2}\treturn false;\n";
    if ($coded_type) {
        getCodedItems($namespace, $coded_type->items, $indent2);
    } else {
        $vars = $struct->vars;
        echo "{$indent2}return ";
        $index = 0;
        foreach ($vars as $var) {
            if ($index++) {
                echo "\n{$indent2}\t&& ";
            }
            echo "NS_CERL_IO::get(is, val.{$var->name})";
        }
        echo ";\n";
    }
    echo "{$indent}CERL_IO_END_GETTER()\n";
    //copy
    //printPODTYPE();
    $podTrue = true;
    echo "\n{$indent}template <class AllocT>\n";
    echo "{$indent}inline void copy(AllocT& alloc, {$namespace}::{$type_name}& dest, const {$namespace}::{$type_name}& src)\n";
    echo "{$indent}{\n";
    echo "{$indent2}dest = src;\n";
    if ($coded_type) {
        $podTrue = copyCodedItems($namespace, $coded_type->items, $indent2);
    } else {
        $vars = $struct->vars;
        foreach ($vars as $var) {
            if (!isPOD($var->type)) {
                echo "{$indent2}NS_CERL_IO::copy(alloc, dest.{$var->name}, src.{$var->name});\n";
                $podTrue = false;
            }
        }
    }
    echo "{$indent}}\n";
    if ($podTrue) {
        echo "{$indent}CERL_PODTYPE({$namespace}::{$type_name}, true);\n";
    } else {
        echo "{$indent}CERL_PODTYPE({$namespace}::{$type_name}, false);\n";
    }
    //dump
    if (@$type_type->array) {
        return;
    }
    //do not generate dump for array
    $vars = $struct->vars;
    echo "\ntemplate<class LogT>\n";
    echo "inline void dump(LogT& log, const {$namespace}::{$type_name}& args)\n";
    echo "{\n";
    echo "\tNS_CERL_IO::dump(log, '{');\n";
    if ($coded_type) {
        dumpCodedItems($namespace, $coded_type->items, $indent2);
    } else {
        $i = 0;
        foreach ($vars as $var) {
            if ($i++ == 0) {
                echo "\tNS_CERL_IO::dump(log, args.{$var->name});\n";
            } else {
                echo "\tNS_CERL_IO::dump(log, \", \");\n";
                echo "\tNS_CERL_IO::dump(log, args.{$var->name});\n";
            }
        }
    }
    echo "\tNS_CERL_IO::dump(log, '}');\n";
    echo "}\n";
}
Пример #2
0
function deftype($type, $typename, $indent)
{
    global $codeset, $typeset, $pod_types;
    global $module, $current_server;
    global $builtin_types, $derived_types;
    $named_type = @$type->named_type;
    $array = @$type->array;
    if ($named_type) {
        $name = $named_type->name;
        $builtin = @$builtin_types[$name];
        if ($builtin) {
            $name = $builtin;
        }
        if ($array) {
            $size = @$array->size;
            if ($size) {
                $name = "cerl::Array<{$name}, {$size}>";
            } else {
                $name = "cerl::BasicArray<{$name}>";
            }
            echo $name;
            return false;
        }
        if ($builtin == "cerl::String") {
            echo $name;
            return false;
        }
        push_pod_types($type, $typename);
        //向 pod_types 数组加入 pod 数据
        $tp_name = "{$current_server}::{$named_type->name}";
        $tp = @$typeset[$tp_name];
        if (!$tp && $current_server != $module) {
            $tp_name = "{$module}::{$named_type->name}";
            $tp = @$typeset[$tp_name];
        }
        if (!$builtin && !$tp) {
            die("\n---ERROR: Type '{$named_type->name}' in '{$current_server}'not defined!\n");
        }
        if ($tp) {
            echo $name;
            return @$tp["primary"];
        }
        echo $name;
        return true;
    } else {
        if ($array) {
            die("ERROR: can't define an array of unnamed type.\n");
        }
        $coded_type = @$type->coded_type;
        if ($coded_type) {
            checkCodedType($coded_type, $typename);
        }
        if (isEnum($coded_type)) {
            echo "cerl::Code";
            return true;
            // means primary type
        }
        $exist_tp = typedefExistsType($type, $typename);
        if ($exist_tp) {
            echo $exist_tp;
            return false;
            // assume that traited types always non-primary
        }
        push_pod_types($type, $typename);
        //向 pod_types 数组加入 pod 数据
        echo "struct {\n";
        if ($coded_type) {
            $items = @$coded_type->items;
            $count = getStructCount($items);
            $indent2 = $indent . "\t";
            echo "{$indent2}cerl::Code _code;\n";
            $indent3 = $indent2;
            if ($count > 1) {
                $indent3 = $indent2 . "\t";
                echo "{$indent2}union {\n";
            }
            foreach ($coded_type->items as $it) {
                $vars = @$it->vars;
                if ($vars) {
                    echo "{$indent3}struct {\n";
                    processVars($vars, $indent3);
                    $it_name = $it->code == "ok" ? "" : " " . $it->code;
                    echo "{$indent3}}{$it_name};\n";
                }
            }
            if ($count > 1) {
                echo "{$indent2}};\n";
            }
            // union
        } else {
            processVars(@$type->struct->vars, $indent);
        }
        echo "{$indent}}";
        // struct
        return false;
        // means non-primary type
    }
}