2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-06-27 14:53:11 +00:00
This commit is contained in:
2022-03-12 16:21:29 +03:00
parent 92a26b8ce5
commit 88eba227ae
64 changed files with 38165 additions and 2952 deletions

View File

@ -0,0 +1,18 @@
import '../Security/Authority/Session.dart';
import 'IResource.dart';
import 'Template/EventTemplate.dart';
class EventOccurredInfo {
final EventTemplate eventTemplate;
String get name => eventTemplate.name;
final IResource resource;
final dynamic value;
final issuer;
final bool Function(Session)? receivers;
EventOccurredInfo(this.resource, this.eventTemplate, this.value, this.issuer,
this.receivers) {}
}

View File

@ -1,7 +1,33 @@
class FactoryEntry {
final Type type;
final Function instanceCreator;
final Function arrayCreator;
import '../Data/RepresentationType.dart';
FactoryEntry(this.type, this.instanceCreator, this.arrayCreator);
// class DumClass<T> {
// Type type = T;
// }
// Type getNullableType<T>() => DumClass<T?>().type;
// Type getTypeOf<T>() => DumClass<T>().type;
class FactoryEntry<T> {
Type get type => T;
late Type nullableType;
final Function instanceCreator;
final Function arrayCreator = () => <T>[];
final RepresentationType representationType;
bool isMapKeySubType(Map map) {
return map is Map<T, dynamic>;
}
bool isMapValueSubType(Map map) {
return map is Map<dynamic, T>;
}
bool isListSubType(List list) {
return list is List<T>;
}
FactoryEntry(this.instanceCreator, this.representationType) {
nullableType = getNullableType<T>();
}
}

View File

@ -36,10 +36,10 @@ abstract class IStore implements IResource {
AsyncReply<IResource?> retrieve(int iid);
AsyncReply<bool> put(IResource resource);
String? link(IResource resource);
bool record(IResource resource, String propertyName, dynamic value, int age,
DateTime dateTime);
bool modify(IResource resource, String propertyName, dynamic value, int age,
DateTime dateTime);
bool record(IResource resource, String propertyName, dynamic value, int? age,
DateTime? dateTime);
bool modify(IResource resource, String propertyName, dynamic value, int? age,
DateTime? dateTime);
bool remove(IResource resource);
AsyncReply<KeyList<PropertyTemplate, List<PropertyValue>>?> getRecord(

View File

@ -1,7 +1,6 @@
import 'dart:core';
import '../Data/DC.dart';
import '../Data/Structure.dart';
import '../Data/AutoList.dart';
import './IStore.dart';
import './IResource.dart';
@ -12,6 +11,7 @@ import '../Core/IEventHandler.dart';
import '../Security/Permissions/Ruling.dart';
import '../Security/Permissions/IPermissionsManager.dart';
import '../Security/Permissions/ActionType.dart';
import 'EventOccurredInfo.dart';
import 'Template/TypeTemplate.dart';
import './Template/PropertyTemplate.dart';
import './Template/FunctionTemplate.dart';
@ -24,7 +24,6 @@ import 'Warehouse.dart';
import '../Core/PropertyModificationInfo.dart';
class Instance extends IEventHandler {
String _name;
@ -64,8 +63,8 @@ class Instance extends IEventHandler {
return true;
}
Structure getAttributes([List<String>? attributes = null]) {
var st = new Structure();
Map<String, dynamic> getAttributes([List<String>? attributes = null]) {
var st = Map<String, dynamic>();
if (attributes == null) {
var clone = this.attributes.keys.toList();
@ -77,10 +76,10 @@ class Instance extends IEventHandler {
if (attr == "name")
st["name"] = _name;
else if (attr == "managers") {
var mngrs = <Structure>[];
var mngrs = <Map<String, dynamic>>[];
for (var i = 0; i < _managers.length; i++) {
var mst = new Structure();
var mst = Map<String, dynamic>();
mst["type"] = _managers[i].runtimeType;
mst["settings"] = _managers[i].settings;
@ -103,7 +102,8 @@ class Instance extends IEventHandler {
return st;
}
bool setAttributes(Structure attributes, [bool clearAttributes = false]) {
bool setAttributes(Map<String, dynamic> attributes,
[bool clearAttributes = false]) {
try {
if (clearAttributes) _attributes.clear();
@ -256,7 +256,7 @@ class Instance extends IEventHandler {
/// </summary>
int get age => _instanceAge;
// this must be internal
set age(value) => _instanceAge = value;
set age(int value) => _instanceAge = value;
/// <summary>
/// Last modification date.
@ -381,18 +381,19 @@ class Instance extends IEventHandler {
_ages[pt.index] = _instanceAge;
_modificationDates[pt.index] = now;
if (pt.storage == StorageMode.NonVolatile) {
_store?.modify(_resource, pt.name, value, _ages[pt.index], now);
} else if (pt.storage == StorageMode.Recordable) {
if (pt.recordable) {
_store?.record(_resource, pt.name, value, _ages[pt.index], now);
} else {
_store?.modify(_resource, pt.name, value, _ages[pt.index], now);
}
emitArgs("resourceModified", [_resource, pt.name, value]);
var pmInfo = PropertyModificationInfo(_resource, pt, value, _instanceAge);
emitArgs("PropertyModified", [pmInfo]);
//_resource.emitArgs("modified", [pt.name, value]);
_resource.emitArgs(":${pt.name}", [value]);
_resource.emitProperty(
PropertyModificationInfo(_resource, pt, value, _instanceAge));
_resource.emitProperty(pmInfo);
}
/// <summary>
@ -409,10 +410,11 @@ class Instance extends IEventHandler {
}
}
emitResourceEvent(
issuer, List<Session>? receivers, String name, dynamic args) {
emitArgs(
"resourceEventOccurred", [_resource, issuer, receivers, name, args]);
emitResourceEvent(issuer, bool Function(Session)? receivers,
EventTemplate eventTemplate, dynamic value) {
emitArgs("EventOccurred", [
EventOccurredInfo(_resource, eventTemplate, value, issuer, receivers)
]);
}
/// <summary>

View File

@ -0,0 +1,14 @@
import 'IResource.dart';
import 'Template/PropertyTemplate.dart';
class PropertyModificationInfo {
final IResource resource;
final PropertyTemplate propertyTemplate;
final int age;
final value;
String get name => propertyTemplate.name;
PropertyModificationInfo(
this.resource, this.propertyTemplate, this.value, this.age) {}
}

View File

@ -1,29 +1,34 @@
import '../../Data/RepresentationType.dart';
import '../../Data/DC.dart';
import '../../Data/BinaryList.dart';
import "../../Data/ParseResult.dart";
import './TemplateDataType.dart';
class ArgumentTemplate {
String name;
final String name;
final bool optional;
final RepresentationType type;
final int index;
TemplateDataType type;
static ParseResult<ArgumentTemplate> parse(DC data, int offset, int index) {
var optional = (data[offset++] & 0x1) == 0x1;
static ParseResult<ArgumentTemplate> parse(DC data, int offset) {
var cs = data[offset++];
var name = data.getString(offset, cs);
offset += cs;
var tdr = TemplateDataType.parse(data, offset);
var tdr = RepresentationType.parse(data, offset);
return ParseResult<ArgumentTemplate>(
cs + 1 + tdr.size, ArgumentTemplate(name, tdr.value));
cs + 2 + tdr.size, ArgumentTemplate(name, tdr.type, optional, index));
}
ArgumentTemplate(this.name, this.type);
ArgumentTemplate(this.name, this.type, this.optional, this.index);
DC compose() {
var name = DC.stringToBytes(this.name);
return (BinaryList()
..addUint8(optional ? 1 : 0)
..addUint8(name.length)
..addDC(name)
..addDC(type.compose()))

View File

@ -0,0 +1,46 @@
import '../../Data/BinaryList.dart';
import '../../Data/Codec.dart';
import '../../Data/DC.dart';
import '../../Data/RepresentationType.dart';
import 'MemberTemplate.dart';
import 'TypeTemplate.dart';
class ConstantTemplate extends MemberTemplate {
final dynamic value;
final String? expansion;
final RepresentationType valueType;
ConstantTemplate(TypeTemplate template, int index, String name,
bool inherited, this.valueType, this.value, this.expansion)
: super(template, index, name, inherited) {}
DC compose() {
var name = super.compose();
var hdr = inherited ? 0x80 : 0;
if (expansion != null) {
var exp = DC.stringToBytes(expansion!);
hdr |= 0x70;
return (BinaryList()
..addUint8(hdr)
..addUint8(name.length)
..addDC(name)
..addDC(valueType.compose())
..addDC(Codec.compose(value, null))
..addInt32(exp.length)
..addDC(exp))
.toDC();
} else {
hdr |= 0x60;
return (BinaryList()
..addUint8(hdr)
..addUint8(name.length)
..addDC(name)
..addDC(valueType.compose())
..addDC(Codec.compose(value, null)))
.toDC();
}
}
}

View File

@ -3,20 +3,25 @@ import '../../Data/DC.dart';
import '../../Data/BinaryList.dart';
import 'TypeTemplate.dart';
import 'MemberType.dart';
import 'TemplateDataType.dart';
import '../../Data/RepresentationType.dart';
class EventTemplate extends MemberTemplate {
String? expansion;
bool listenable;
TemplateDataType argumentType;
final String? expansion;
final bool listenable;
final RepresentationType argumentType;
DC compose() {
var name = super.compose();
var hdr = inherited ? 0x80 : 0;
if (listenable) hdr |= 0x8;
if (expansion != null) {
var exp = DC.stringToBytes(expansion as String);
hdr |= 0x50;
return (BinaryList()
..addUint8(listenable ? 0x58 : 0x50)
..addUint8(hdr)
..addUint8(name.length)
..addDC(name)
..addDC(argumentType.compose())
@ -24,8 +29,9 @@ class EventTemplate extends MemberTemplate {
..addDC(exp))
.toDC();
} else {
hdr |= 0x40;
return (BinaryList()
..addUint8(listenable ? 0x48 : 0x40)
..addUint8(hdr)
..addUint8(name.length)
..addDC(name)
..addDC(argumentType.compose()))
@ -33,7 +39,8 @@ class EventTemplate extends MemberTemplate {
}
}
EventTemplate(TypeTemplate template, int index, String name,
this.argumentType, this.expansion, this.listenable)
: super(template, MemberType.Property, index, name) {}
EventTemplate(TypeTemplate template, int index, String name, bool inherited,
this.argumentType,
[this.expansion = null, this.listenable = false])
: super(template, index, name, inherited) {}
}

View File

@ -4,46 +4,40 @@ import '../../Data/BinaryList.dart';
import 'TypeTemplate.dart';
import 'MemberType.dart';
import 'ArgumentTemplate.dart';
import 'TemplateDataType.dart';
import '../../Data/RepresentationType.dart';
class FunctionTemplate extends MemberTemplate {
String? expansion;
// bool isVoid;
// bool isVoid;
TemplateDataType returnType;
List<ArgumentTemplate> arguments;
RepresentationType returnType;
DC compose() {
var name = super.compose();
var name = super.compose();
var bl = new BinaryList()
..addUint8(name.length)
..addDC(name)
..addDC(returnType.compose())
..addUint8(arguments.length);
var bl = new BinaryList()
..addUint8(name.length)
..addDC(name)
..addDC(returnType.compose())
..addUint8(arguments.length);
for (var i = 0; i < arguments.length; i++)
bl.addDC(arguments[i].compose());
for (var i = 0; i < arguments.length; i++) bl.addDC(arguments[i].compose());
if (expansion != null) {
var exp = DC.stringToBytes(expansion as String);
bl
..addInt32(exp.length)
..addDC(exp);
bl.insertUint8(0, inherited ? 0x90 : 0x10);
} else
bl.insertUint8(0, inherited ? 0x80 : 0x0);
if (expansion != null)
{
var exp = DC.stringToBytes(expansion as String);
bl..addInt32(exp.length)
..addDC(exp);
bl.insertUint8(0, 0x10);
}
else
bl.insertUint8(0, 0x0);
return bl.toDC();
return bl.toDC();
}
FunctionTemplate(TypeTemplate template, int index, String name,
this.arguments, this.returnType, this.expansion)
: super(template, MemberType.Property, index, name) {
}
bool inherited, this.arguments, this.returnType,
[this.expansion = null])
: super(template, index, name, inherited) {}
}

View File

@ -1,30 +1,18 @@
import 'MemberType.dart';
import '../../Data/DC.dart';
import 'TypeTemplate.dart';
class MemberTemplate
{
int get index => _index;
String get name => _name;
MemberType get type => _type;
class MemberTemplate {
final TypeTemplate template;
final String name;
final int index;
final bool inherited;
TypeTemplate _template;
String _name;
MemberType _type;
int _index;
MemberTemplate(this.template, this.index, this.name, this.inherited) {}
TypeTemplate get template => _template;
String get fullname => template.className + "." + name;
MemberTemplate(this._template, this._type, this._index, this._name)
{
}
String get fullname => _template.className + "." + _name;
DC compose()
{
return DC.stringToBytes(_name);
}
DC compose() {
return DC.stringToBytes(name);
}
}

View File

@ -1,18 +1,17 @@
import 'TemplateDataType.dart';
import 'MemberTemplate.dart';
import '../../Data/DC.dart';
import '../../Data/BinaryList.dart';
import 'TypeTemplate.dart';
import 'MemberType.dart';
import '../StorageMode.dart';
import '../../Data/RepresentationType.dart';
class PropertyTemplate extends MemberTemplate {
TemplateDataType valueType;
RepresentationType valueType;
int permission = 0;
int storage;
bool recordable;
String? readExpansion;
@ -20,7 +19,9 @@ class PropertyTemplate extends MemberTemplate {
DC compose() {
var name = super.compose();
var pv = ((permission) << 1) | (storage == StorageMode.Recordable ? 1 : 0);
var pv = (permission << 1) | (recordable ? 1 : 0);
if (inherited) pv |= 0x80;
if (writeExpansion != null && readExpansion != null) {
var rexp = DC.stringToBytes(readExpansion as String);
@ -65,8 +66,9 @@ class PropertyTemplate extends MemberTemplate {
}
PropertyTemplate(TypeTemplate template, int index, String name,
this.valueType, this.readExpansion, this.writeExpansion, this.storage)
: super(template, MemberType.Property, index, name) {
//this.Recordable = recordable;
}
bool inherited, this.valueType,
[this.readExpansion = null,
this.writeExpansion = null,
this.recordable = false])
: super(template, index, name, inherited) {}
}

View File

@ -1,125 +1,124 @@
//import 'dart:ffi';
// import '../../Data/IRecord.dart';
// import '../../Resource/IResource.dart';
import '../../Data/IRecord.dart';
import '../../Resource/IResource.dart';
// import '../../Data/Structure.dart';
import '../../Data/Structure.dart';
// import '../../Data/ParseResult.dart';
import '../../Data/ParseResult.dart';
// import '../../Data/DataType.dart';
// import '../../Data/Guid.dart';
// import '../../Data/DC.dart';
// import '../../Data/BinaryList.dart';
// import 'TypeTemplate.dart';
// import '../../Resource/Warehouse.dart';
// import 'TemplateType.dart';
import '../../Data/DataType.dart';
import '../../Data/Guid.dart';
import '../../Data/DC.dart';
import '../../Data/BinaryList.dart';
import 'TypeTemplate.dart';
import '../../Resource/Warehouse.dart';
import 'TemplateType.dart';
// class TemplateDataType {
// late int type;
// TypeTemplate? get typeTemplate => typeGuid == null
// ? null
// : Warehouse.getTemplateByClassId(typeGuid as Guid);
class TemplateDataType {
late int type;
TypeTemplate? get typeTemplate => typeGuid == null
? null
: Warehouse.getTemplateByClassId(typeGuid as Guid);
// Guid? typeGuid;
Guid? typeGuid;
// // @TODO: implement fromType
// TemplateDataType.fromType(type, bool isArray) {
// int dt;
// @TODO: implement fromType
TemplateDataType.fromType(type, bool isArray) {
int dt;
// if (type == null || type == dynamic) {
// dt = DataType.Void;
// }
// // else if (type is int) {
// // dt = type;
// else if (type == bool)
// dt = DataType.Bool;
// // else if (type == Uint8)
// // dt = DataType.UInt8;
// // else if (type == Int8)
// // dt = DataType.Int8;
// // else if (type == Uint16)
// // dt = DataType.UInt16;
// // else if (type == Int16)
// // dt = DataType.Int16;
// // else if (type == Uint32)
// // dt = DataType.UInt32;
// // else if (type == Int32)
// // dt = DataType.Int32;
// // else if (type == Uint64)
// // dt = DataType.UInt64;
// else if (/* type == Int64 || */ type == int)
// dt = DataType.Int64;
// // else if (type == Float)
// // dt = DataType.Float32;
// else if (/* type == Double || */ type == double)
// dt = DataType.Float64;
// else if (type == String)
// dt = DataType.String;
// else if (type == DateTime)
// dt = DataType.DateTime;
// else if (type == Structure)
// dt = DataType.Structure;
// else if (type == IResource) // Dynamic resource (unspecified type)
// dt = DataType.Void;
// else if (type == IRecord) // Dynamic record (unspecified type)
// dt = DataType.Void;
// else {
// var template = Warehouse.getTemplateByType(type);
if (type == null || type == dynamic) {
dt = DataType.Void;
}
// else if (type is int) {
// dt = type;
else if (type == bool)
dt = DataType.Bool;
// else if (type == Uint8)
// dt = DataType.UInt8;
// else if (type == Int8)
// dt = DataType.Int8;
// else if (type == Uint16)
// dt = DataType.UInt16;
// else if (type == Int16)
// dt = DataType.Int16;
// else if (type == Uint32)
// dt = DataType.UInt32;
// else if (type == Int32)
// dt = DataType.Int32;
// else if (type == Uint64)
// dt = DataType.UInt64;
else if (/* type == Int64 || */ type == int)
dt = DataType.Int64;
// else if (type == Float)
// dt = DataType.Float32;
else if (/* type == Double || */ type == double)
dt = DataType.Float64;
else if (type == String)
dt = DataType.String;
else if (type == DateTime)
dt = DataType.DateTime;
else if (type == Structure)
dt = DataType.Structure;
else if (type == IResource) // Dynamic resource (unspecified type)
dt = DataType.Void;
else if (type == IRecord) // Dynamic record (unspecified type)
dt = DataType.Void;
else {
var template = Warehouse.getTemplateByType(type);
// if (template != null) {
// typeGuid = template.classId;
// dt = template.type == TemplateType.Resource
// ? DataType.Resource
// : DataType.Record;
// } else
// dt = DataType.Void;
if (template != null) {
typeGuid = template.classId;
dt = template.type == TemplateType.Resource
? DataType.Resource
: DataType.Record;
} else
dt = DataType.Void;
// // if (template)
// // try {
// // var ins = Warehouse.createInstance(type);
// // if (ins is IResource) {
// // typeGuid = TypeTemplate.getTypeGuid(ins.template.nameSpace);
// // } else if (ins is IRecord) {
// // typeGuid = TypeTemplate.getTypeGuid(ins.template.nameSpace);
// // } else {
// // dt = DataType.Void;
// // }
// // } catch (ex) {
// // dt = DataType.Void;
// // }
// }
// if (template)
// try {
// var ins = Warehouse.createInstance(type);
// if (ins is IResource) {
// typeGuid = TypeTemplate.getTypeGuid(ins.template.nameSpace);
// } else if (ins is IRecord) {
// typeGuid = TypeTemplate.getTypeGuid(ins.template.nameSpace);
// } else {
// dt = DataType.Void;
// }
// } catch (ex) {
// dt = DataType.Void;
// }
}
// if (isArray) dt = dt | 0x80;
if (isArray) dt = dt | 0x80;
// this.type = dt;
// }
this.type = dt;
}
// DC compose() {
// if (type == DataType.Resource ||
// type == DataType.ResourceArray ||
// type == DataType.Record ||
// type == DataType.RecordArray) {
// return (BinaryList()
// ..addUint8(type)
// ..addDC((typeGuid as Guid).value))
// .toDC();
// } else
// return DC.fromList([type]);
// }
DC compose() {
if (type == DataType.Resource ||
type == DataType.ResourceArray ||
type == DataType.Record ||
type == DataType.RecordArray) {
return (BinaryList()
..addUint8(type)
..addDC((typeGuid as Guid).value))
.toDC();
} else
return DC.fromList([type]);
}
// TemplateDataType(this.type, this.typeGuid);
TemplateDataType(this.type, this.typeGuid);
static ParseResult<TemplateDataType> parse(DC data, int offset) {
var type = data[offset++];
if (type == DataType.Resource ||
type == DataType.ResourceArray ||
type == DataType.Record ||
type == DataType.RecordArray) {
var guid = data.getGuid(offset);
return ParseResult<TemplateDataType>(
17, new TemplateDataType(type, guid));
} else
return ParseResult<TemplateDataType>(1, new TemplateDataType(type, null));
}
}
// static ParseResult<TemplateDataType> parse(DC data, int offset) {
// var type = data[offset++];
// if (type == DataType.Resource ||
// type == DataType.ResourceArray ||
// type == DataType.Record ||
// type == DataType.RecordArray) {
// var guid = data.getGuid(offset);
// return ParseResult<TemplateDataType>(
// 17, new TemplateDataType(type, guid));
// } else
// return ParseResult<TemplateDataType>(1, new TemplateDataType(type, null));
// }
// }

View File

@ -1,14 +1,21 @@
import '../../Data/DataType.dart';
class TemplateDescriber {
final List<Prop>? properties;
final List<Evt>? events;
final List<Func>? functions;
final List<Const>? constants;
final String nameSpace;
final int version;
final Type? parent;
TemplateDescriber(this.nameSpace,
{this.properties, this.functions, this.events, this.version = 0});
const TemplateDescriber(this.nameSpace,
{this.parent,
this.properties,
this.functions,
this.events,
this.constants,
this.version = 0});
}
// class Property<T> {
@ -46,10 +53,10 @@ class TemplateDescriber {
class Prop {
final String name;
final Type type;
final bool isArray;
//final bool isNullable;
final String? readAnnotation;
final String? writeAnnotation;
Prop(this.name, this.type, this.isArray,
const Prop(this.name, this.type,
[this.readAnnotation = null, this.writeAnnotation = null]);
}
@ -57,28 +64,36 @@ class Evt {
final String name;
final bool listenable;
final Type type;
final bool isArray;
//final bool isNullable;
final String? annotation;
const Evt(this.name, this.type, [this.listenable = false, this.annotation]);
}
Evt(this.name, this.type, this.isArray,
[this.listenable = false, this.annotation]);
class Const {
final String name;
final Type type;
//final bool isNullable;
final String? annotation;
final value;
const Const(this.name, this.type, this.value, [this.annotation]);
}
class Func {
final String name;
final Type returnType;
final List<Arg> argsType;
final bool isArray;
//final bool isNullable;
final String? annotation;
Func(this.name, this.returnType, this.isArray, this.argsType,
const Func(this.name, this.returnType, this.argsType,
[this.annotation = null]);
}
class Arg {
final String name;
final Type type;
final bool isArray;
Arg(this.name, this.type, this.isArray);
//final bool isNullable;
final bool optional;
const Arg(this.name, this.type, this.optional);
}

View File

@ -3,4 +3,5 @@ enum TemplateType {
Resource,
Record,
Wrapper,
Enum
}

View File

@ -1,4 +1,6 @@
//import 'dart:ffi';
import '../../Data/Codec.dart';
import '../../Data/IEnum.dart';
import '../../Data/RepresentationType.dart';
import '../../Net/IIP/DistributedResource.dart';
@ -16,18 +18,21 @@ import '../../Data/DC.dart';
import './EventTemplate.dart';
import './PropertyTemplate.dart';
import './FunctionTemplate.dart';
import '../StorageMode.dart';
import 'ArgumentTemplate.dart';
import 'TemplateDataType.dart';
import 'ConstantTemplate.dart';
import 'TemplateType.dart';
class TypeTemplate {
late Guid _classId;
Guid? _parentId = null;
late String _className;
List<MemberTemplate> _members = [];
List<FunctionTemplate> _functions = [];
List<EventTemplate> _events = [];
List<PropertyTemplate> _properties = [];
List<ConstantTemplate> _constants = [];
late int _version;
//bool isReady;
@ -39,22 +44,14 @@ class TypeTemplate {
TemplateType get type => _templateType;
Guid? get parentId => _parentId;
Type? _definedType;
Type? get definedType => _definedType;
/*
MemberTemplate getMemberTemplate(MemberInfo member)
{
if (member is MethodInfo)
return getFunctionTemplate(member.Name);
else if (member is EventInfo)
return getEventTemplate(member.Name);
else if (member is PropertyInfo)
return getPropertyTemplate(member.Name);
else
return null;
}
*/
Type? get parentDefinedType => _parentDefinedType;
Type? _parentDefinedType;
//@TODO: implement
static List<TypeTemplate> getDependencies(TypeTemplate template) => [];
@ -89,6 +86,16 @@ class TypeTemplate {
return null;
}
ConstantTemplate? getConstantByIndex(int index) {
for (var i in _constants) if (i.index == index) return i;
return null;
}
ConstantTemplate? getConstantByName(String constantName) {
for (var i in _constants) if (i.name == constantName) return i;
return null;
}
static Guid getTypeGuid(String typeName) {
var tn = DC.stringToBytes(typeName);
var hash = SHA256.compute(tn).clip(0, 16);
@ -107,20 +114,30 @@ class TypeTemplate {
List<PropertyTemplate> get properties => _properties;
List<ConstantTemplate> get constants => _constants;
TypeTemplate.fromType(Type type, [bool addToWarehouse = false]) {
// debugging print("FromType ${type.toString()}");
var instance = Warehouse.createInstance(type);
if (instance is DistributedResource)
TemplateDescriber describer;
if (instance is DistributedResource) {
_templateType = TemplateType.Wrapper;
else if (instance is IResource)
describer = instance.template;
} else if (instance is IResource) {
_templateType = TemplateType.Resource;
else if (instance is IRecord)
describer = instance.template;
} else if (instance is IRecord) {
_templateType = TemplateType.Record;
else
describer = instance.template;
} else if (instance is IEnum) {
_templateType = TemplateType.Enum;
describer = instance.template;
} else
throw new Exception(
"Type must implement IResource, IRecord or inherit from DistributedResource.");
"Type must implement IResource, IRecord, IEnum or a subtype of DistributedResource.");
// if (instance is IRecord)
// _templateType = TemplateType.Record;
@ -129,8 +146,6 @@ class TypeTemplate {
// else
// throw new Exception("Type is neither a resource nor a record.");
TemplateDescriber describer = instance.template;
_definedType = type;
_className = describer.nameSpace;
@ -143,6 +158,23 @@ class TypeTemplate {
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>;
@ -152,10 +184,11 @@ class TypeTemplate {
this,
i,
pi.name,
TemplateDataType.fromType(pi.type, pi.isArray),
false,
RepresentationType.fromType(pi.type) ?? RepresentationType.Dynamic,
pi.readAnnotation,
pi.writeAnnotation,
0);
false);
properties.add(pt);
}
}
@ -167,16 +200,24 @@ class TypeTemplate {
var fi = funcs[i];
List<ArgumentTemplate> args = fi.argsType
.asMap()
.entries
.map((arg) => ArgumentTemplate(
arg.name, TemplateDataType.fromType(arg.type, arg.isArray)))
arg.value.name,
RepresentationType.fromType(arg.value.type) ??
RepresentationType.Dynamic,
arg.value.optional,
arg.key))
.toList();
var ft = FunctionTemplate(
this,
i,
fi.name,
false,
args,
TemplateDataType.fromType(fi.returnType, fi.isArray),
RepresentationType.fromType(fi.returnType) ??
RepresentationType.Void,
fi.annotation);
functions.add(ft);
@ -192,7 +233,8 @@ class TypeTemplate {
this,
i,
ei.name,
TemplateDataType.fromType(ei.type, ei.isArray),
false,
RepresentationType.fromType(ei.type) ?? RepresentationType.Dynamic,
ei.annotation,
ei.listenable);
@ -204,8 +246,10 @@ class TypeTemplate {
events.forEach(_members.add);
// append slots
functions.forEach(_members.add);
// append properties
// append properties
properties.forEach(_members.add);
// append constants
constants.forEach(_members.add);
// bake it binarily
var b = BinaryList()
@ -440,22 +484,29 @@ class TypeTemplate {
// cool Dart feature
contentLength ??= data.length;
int ends = offset + contentLength;
//int ends = offset + contentLength;
int oOffset = offset;
//int oOffset = offset;
// start parsing...
//var od = new TypeTemplate();
_content = data.clip(offset, contentLength);
_templateType = TemplateType.values[data.getUint8(offset++)];
var hasParent = (data.getUint8(offset) & 0x80) > 0;
_templateType = TemplateType.values[data.getUint8(offset++) & 0xF];
_classId = data.getGuid(offset);
offset += 16;
_className = data.getString(offset + 1, data[offset]);
offset += data[offset] + 1;
if (hasParent) {
_parentId = data.getGuid(offset);
offset += 16;
}
_version = data.getInt32(offset);
offset += 4;
@ -465,9 +516,11 @@ class TypeTemplate {
var functionIndex = 0;
var propertyIndex = 0;
var eventIndex = 0;
var constantIndex = 0;
for (int i = 0; i < methodsCount; i++) {
var type = data[offset] >> 5;
var inherited = (data[offset] & 0x80) > 0;
var type = (data[offset] >> 5) & 0x3;
if (type == 0) // function
{
@ -477,7 +530,7 @@ class TypeTemplate {
var name = data.getString(offset + 1, data[offset]);
offset += data[offset] + 1;
var dt = TemplateDataType.parse(data, offset);
var dt = RepresentationType.parse(data, offset);
offset += dt.size;
// arguments count
@ -485,7 +538,7 @@ class TypeTemplate {
List<ArgumentTemplate> arguments = [];
for (var a = 0; a < argsCount; a++) {
var art = ArgumentTemplate.parse(data, offset);
var art = ArgumentTemplate.parse(data, offset, a);
arguments.add(art.value);
offset += art.size;
}
@ -498,8 +551,8 @@ class TypeTemplate {
offset += cs;
}
var ft = new FunctionTemplate(
this, functionIndex++, name, arguments, dt.value, expansion);
var ft = new FunctionTemplate(this, functionIndex++, name, inherited,
arguments, dt.type, expansion);
_functions.add(ft);
} else if (type == 1) // property
@ -514,7 +567,7 @@ class TypeTemplate {
offset += data[offset] + 1;
var dt = TemplateDataType.parse(data, offset);
var dt = RepresentationType.parse(data, offset);
offset += dt.size;
@ -534,14 +587,8 @@ class TypeTemplate {
offset += cs;
}
var pt = new PropertyTemplate(
this,
propertyIndex++,
name,
dt.value,
readExpansion,
writeExpansion,
recordable ? StorageMode.Recordable : StorageMode.Volatile);
var pt = new PropertyTemplate(this, propertyIndex++, name, inherited,
dt.type, readExpansion, writeExpansion, recordable);
_properties.add(pt);
} else if (type == 2) // Event
@ -553,7 +600,7 @@ class TypeTemplate {
var name = data.getString(offset + 1, data[offset]);
offset += data[offset] + 1;
var dt = TemplateDataType.parse(data, offset);
var dt = RepresentationType.parse(data, offset);
offset += dt.size;
@ -565,10 +612,37 @@ class TypeTemplate {
offset += cs;
}
var et = new EventTemplate(
this, eventIndex++, name, dt.value, expansion, listenable);
var et = new EventTemplate(this, eventIndex++, name, inherited, dt.type,
expansion, listenable);
_events.add(et);
} else if (type == 3) {
String? expansion = null;
var hasExpansion = ((data[offset++] & 0x10) == 0x10);
var name = data.getString(offset + 1, data[offset]);
offset += data[offset] + 1;
var dt = RepresentationType.parse(data, offset);
offset += dt.size;
var parsed = Codec.parse(data, offset, null);
offset += parsed.size;
if (hasExpansion) // expansion ?
{
var cs = data.getUint32(offset);
offset += 4;
expansion = data.getString(offset, cs);
offset += cs;
}
var ct = new ConstantTemplate(this, constantIndex++, name, inherited,
dt.type, parsed.reply.result, expansion);
_constants.add(ct);
}
}
@ -578,5 +652,7 @@ class TypeTemplate {
for (int i = 0; i < _functions.length; i++) _members.add(_functions[i]);
// append properties
for (int i = 0; i < _properties.length; i++) _members.add(_properties[i]);
// append constant
for (int i = 0; i < _constants.length; i++) _members.add(_constants[i]);
}
}

View File

@ -22,6 +22,16 @@ SOFTWARE.
*/
import '../Data/IntType.dart';
import '../Data/TransmissionType.dart';
import '../Data/RepresentationType.dart';
import '../Data/Record.dart';
import '../Core/Tuple.dart';
import '../Data/IRecord.dart';
import '../Core/AsyncException.dart';
import '../Core/ErrorType.dart';
import '../Core/ExceptionCode.dart';
@ -32,7 +42,6 @@ import 'Template/TemplateType.dart';
import 'Template/TypeTemplate.dart';
import '../Data/Guid.dart';
import '../Data/KeyList.dart';
import '../Data/Structure.dart';
import '../Security/Permissions/IPermissionsManager.dart';
import 'IResource.dart';
import 'Instance.dart';
@ -52,7 +61,7 @@ class Warehouse {
static KeyList<TemplateType, KeyList<Guid, TypeTemplate>> _templates =
_initTemplates(); //
static _initTemplates() {
static KeyList<TemplateType, KeyList<Guid, TypeTemplate>> _initTemplates() {
var rt = new KeyList<TemplateType, KeyList<Guid, TypeTemplate>>();
rt.add(TemplateType.Unspecified, new KeyList<Guid, TypeTemplate>());
@ -427,8 +436,7 @@ class Warehouse {
resource.instance = new Instance(
resourceCounter++, name, resource, store, customTemplate, age);
if (attributes != null)
resource.instance?.setAttributes(Structure.fromMap(attributes));
if (attributes != null) resource.instance?.setAttributes(attributes);
if (manager != null) resource.instance?.managers.add(manager);
@ -482,12 +490,14 @@ class Warehouse {
return rt;
}
static KeyList<Type, FactoryEntry> get typesFactory => _factory;
static T createInstance<T>(Type type) {
return _factory[type]?.instanceCreator.call();
return _factory[type]?.instanceCreator.call() as T;
}
static List<T> createArray<T>(Type type) {
return _factory[type]?.arrayCreator.call();
return _factory[type]?.arrayCreator.call() as List<T>;
}
static AsyncReply<T> newResource<T extends IResource>(String name,
@ -495,17 +505,17 @@ class Warehouse {
IResource? parent = null,
IPermissionsManager? manager = null,
Map<String, dynamic>? attributes = null,
properties = null]) {
Map<String, dynamic>? properties = null]) {
if (_factory[T] == null)
throw Exception("No Instance Creator was found for type ${T}");
var resource = _factory[T]?.instanceCreator.call();
var resource = _factory[T]?.instanceCreator.call() as T;
if (properties != null) {
dynamic d = resource;
for (var i = 0; i < properties.length; i++)
d[properties.keys.elementAt(i)] = properties.at(i);
d[properties.keys.elementAt(i)] = properties.values.elementAt(i);
//setProperty(resource, properties.keys.elementAt(i), properties.at(i));
}
@ -669,17 +679,112 @@ class Warehouse {
return rt;
}
static defineCreator(
Type type, Function instanceCreator, Function arrayCreator) {
_factory.add(type, FactoryEntry(type, instanceCreator, arrayCreator));
static List<FactoryEntry> _getTypeEntries<T>(
Function instanceCreator, RepresentationType representationType) {
return [
FactoryEntry<T>(instanceCreator, representationType),
FactoryEntry<T?>(instanceCreator, representationType.toNullable()),
FactoryEntry<List<T>>(
() => <T>[],
RepresentationType(RepresentationTypeIdentifier.TypedList, false,
null, [representationType])),
FactoryEntry<List<T>?>(
() => <T>[],
RepresentationType(RepresentationTypeIdentifier.TypedList, true, null,
[representationType])),
FactoryEntry<List<T?>>(
() => <T?>[],
RepresentationType(RepresentationTypeIdentifier.TypedList, false,
null, [representationType.toNullable()])),
FactoryEntry<List<T?>?>(
() => <T?>[],
RepresentationType(RepresentationTypeIdentifier.TypedList, true, null,
[representationType.toNullable()])),
];
}
static void defineType<T>(
Function instanceCreator, RepresentationType representationType) {
var entries = _getTypeEntries<T>(instanceCreator, representationType);
entries.forEach((e) {
_factory.add(e.type, e);
});
}
static KeyList<Type, FactoryEntry> _getBuiltInTypes() {
var rt = KeyList<Type, FactoryEntry>();
rt.add(
DistributedConnection,
FactoryEntry(DistributedConnection, () => DistributedConnection(),
() => DistributedConnection()));
var types = <FactoryEntry>[
FactoryEntry<DistributedConnection>(
() => DistributedConnection(), RepresentationType.Void)
];
types
..addAll(_getTypeEntries<Int8>(() => 0,
RepresentationType(RepresentationTypeIdentifier.Int8, false)))
..addAll(_getTypeEntries<UInt8>(() => 0,
RepresentationType(RepresentationTypeIdentifier.UInt8, false)))
..addAll(_getTypeEntries<Int16>(() => 0,
RepresentationType(RepresentationTypeIdentifier.Int16, false)))
..addAll(_getTypeEntries<UInt16>(() => 0,
RepresentationType(RepresentationTypeIdentifier.UInt16, false)))
..addAll(_getTypeEntries<Int32>(() => 0,
RepresentationType(RepresentationTypeIdentifier.Int32, false)))
..addAll(_getTypeEntries<UInt32>(() => 0,
RepresentationType(RepresentationTypeIdentifier.UInt32, false)))
..addAll(_getTypeEntries<int>(() => 0,
RepresentationType(RepresentationTypeIdentifier.Int64, false)))
..addAll(_getTypeEntries<bool>(() => false,
RepresentationType(RepresentationTypeIdentifier.Bool, false)))
..addAll(_getTypeEntries<double>(() => 0.0,
RepresentationType(RepresentationTypeIdentifier.Float64, false)))
..addAll(_getTypeEntries<String>(() => "",
RepresentationType(RepresentationTypeIdentifier.String, false)))
..addAll(_getTypeEntries<DateTime>(() => DateTime.now(),
RepresentationType(RepresentationTypeIdentifier.DateTime, false)))
..addAll(_getTypeEntries<Record>(() => Record(),
RepresentationType(RepresentationTypeIdentifier.Record, false)))
..addAll(_getTypeEntries<IResource>(() => null,
RepresentationType(RepresentationTypeIdentifier.Resource, false)))
..addAll(_getTypeEntries<List>(() => [],
RepresentationType(RepresentationTypeIdentifier.List, false)))
..addAll(_getTypeEntries<Map>(() => Map(),
RepresentationType(RepresentationTypeIdentifier.Map, false)))
..addAll(_getTypeEntries<Map<String, dynamic>>(
() => Map<String, dynamic>,
RepresentationType(
RepresentationTypeIdentifier.TypedMap, false, null, [
RepresentationType(RepresentationTypeIdentifier.String, false),
RepresentationType.Dynamic
])))
..addAll(_getTypeEntries<Map<int, dynamic>>(
() => Map<int, dynamic>(),
RepresentationType(
RepresentationTypeIdentifier.TypedMap, false, null, [
RepresentationType(RepresentationTypeIdentifier.Int64, false),
RepresentationType.Dynamic
])))
..addAll(_getTypeEntries<Map<Int32, dynamic>>(
() => Map<Int32, dynamic>(),
RepresentationType(
RepresentationTypeIdentifier.TypedMap, false, null, [
RepresentationType(RepresentationTypeIdentifier.Int32, false),
RepresentationType.Dynamic
])))
..addAll(_getTypeEntries<Map<UInt8, dynamic>>(
() => Map<UInt8, dynamic>(),
RepresentationType(
RepresentationTypeIdentifier.TypedMap, false, null, [
RepresentationType(RepresentationTypeIdentifier.UInt8, false),
RepresentationType.Dynamic
])))
..addAll(
_getTypeEntries<dynamic>(() => Object(), RepresentationType.Dynamic));
types.forEach((element) {
rt.add(element.type, element);
});
return rt;
}
}