TypeTemplate.fromType constructor

TypeTemplate.fromType(
  1. Type type, [
  2. bool addToWarehouse = false
])

Implementation

TypeTemplate.fromType(Type type, [bool addToWarehouse = false]) {
  // debugging print("FromType ${type.toString()}");

  var instance = Warehouse.createInstance(type);

  if (instance is IResource) {
    _templateType = TemplateType.Resource;
  } else if (instance is IRecord) {
    _templateType = TemplateType.Record;
  } else if (instance is IEnum) {
    _templateType = TemplateType.Enum;
  } else
    throw new Exception(
        "Type must implement IResource, IRecord, IEnum or a subtype of DistributedResource.");

  _isWrapper = (instance is DistributedResource);

  // if (instance is IRecord)
  //   _templateType = TemplateType.Record;
  // else if (instance is IResource)
  //   _templateType = TemplateType.Resource;
  // else
  //   throw new Exception("Type is neither a resource nor a record.");

  TemplateDescriber describer = instance.template;

  _definedType = type;

  _className = describer.nameSpace;

  // set guid
  _classId = getTypeGuid(_className);

  _version = describer.version;

  _annotation = describer.annotation;

  if (addToWarehouse) Warehouse.putTemplate(this);
  // _templates.add(template.classId, template);

  if (describer.constants != null) {
    var consts = describer.constants as List<Const>;
    for (var i = 0; i < consts.length; i++) {
      var ci = consts[i];
      var ct = ConstantTemplate(
          this,
          i,
          ci.name,
          false,
          RepresentationType.fromType(ci.type) ?? RepresentationType.Void,
          ci.value,
          ci.annotation);

      constants.add(ct);
    }
  }

  if (describer.properties != null) {
    var props = describer.properties as List<Prop>;

    for (var i = 0; i < props.length; i++) {
      var pi = props[i];
      var pt = PropertyTemplate(
          this,
          i,
          pi.name,
          false,
          RepresentationType.fromType(pi.type) ?? RepresentationType.Dynamic,
          pi.readAnnotation,
          pi.writeAnnotation,
          false);
      properties.add(pt);
    }
  }

  if (_templateType == TemplateType.Resource) {
    if (describer.functions != null) {
      var funcs = describer.functions as List<Func>;

      for (var i = 0; i < funcs.length; i++) {
        var fi = funcs[i];

        List<ArgumentTemplate> args = fi.args
            .asMap()
            .entries
            .map((arg) => ArgumentTemplate(
                arg.value.name,
                RepresentationType.fromType(arg.value.type) ??
                    RepresentationType.Dynamic,
                arg.value.optional,
                arg.key))
            .toList();

        var ft = FunctionTemplate(
            this,
            i,
            fi.name,
            false,
            fi.isStatic,
            args,
            RepresentationType.fromType(fi.returnType) ??
                RepresentationType.Void,
            fi.annotation);

        functions.add(ft);
      }
    }

    if (describer.events != null) {
      var evts = describer.events as List<Evt>;
      for (var i = 0; i < evts.length; i++) {
        var ei = evts[i];

        var et = new EventTemplate(
            this,
            i,
            ei.name,
            false,
            RepresentationType.fromType(ei.type) ??
                RepresentationType.Dynamic,
            ei.annotation,
            ei.listenable);

        events.add(et);
      }
    }
  }

  // append signals
  events.forEach(_members.add);
  // append slots
  functions.forEach(_members.add);
  // append properties
  properties.forEach(_members.add);
  // append constants
  constants.forEach(_members.add);

  // bake it binarily
  var b = BinaryList()
    ..addUint8((_annotation != null ? 0x40 : 0x0) | _templateType.index)
    ..addGuid(classId)
    ..addUint8(className.length)
    ..addString(className);

  if (_annotation != null) {
    var classAnnotationBytes = DC.stringToBytes(_annotation!);
    b
      ..addUint16(classAnnotationBytes.length)
      ..addDC(classAnnotationBytes);
  }

  b
    ..addInt32(_version)
    ..addUint16(_members.length);

  functions.forEach((ft) => b.addDC(ft.compose()));
  properties.forEach((pt) => b.addDC(pt.compose()));
  events.forEach((et) => b.addDC(et.compose()));

  _content = b.toDC();
}