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:
2021-07-21 12:18:01 +03:00
parent 737397da11
commit 6be04c39ed
27 changed files with 659 additions and 513 deletions

View File

@ -0,0 +1,7 @@
class FactoryEntry {
final Type type;
final Function() instanceCreator;
final Function() arrayCreator;
FactoryEntry(this.type, this.instanceCreator, this.arrayCreator);
}

View File

@ -22,7 +22,7 @@ SOFTWARE.
*/
import 'package:esiur/src/Resource/Template/TemplateDescriber.dart';
import './Template/TemplateDescriber.dart';
import '../Resource/Template/TemplateDescriber.dart';
import '../Core/IDestructible.dart';

View File

@ -29,6 +29,6 @@ class MemberTemplate
DC compose()
{
// return DC.ToBytes(Name);
return DC.stringToBytes(_name);
}
}

View File

@ -10,7 +10,7 @@ import '../StorageMode.dart';
class PropertyTemplate extends MemberTemplate {
TemplateDataType valueType;
int permission;
int permission = 0;
int storage;

View File

@ -26,7 +26,7 @@ class TemplateDataType {
TemplateDataType.fromType(type, bool isArray) {
int dt;
if (type == null)
if (type == null || type == dynamic)
dt = DataType.Void;
else if (type is int) {
dt = type;
@ -50,7 +50,7 @@ class TemplateDataType {
dt = DataType.Int64;
else if (type == Float)
dt = DataType.Float32;
else if (type == Double)
else if (type == Double || type == double)
dt = DataType.Float64;
else if (type == String)
dt = DataType.String;

View File

@ -5,9 +5,10 @@ class TemplateDescriber {
final List<Evt> events;
final List<Func> functions;
final String nameSpace;
final int version;
TemplateDescriber(this.nameSpace,
{this.properties, this.functions, this.events});
{this.properties, this.functions, this.events, this.version = 0});
}
// class Property<T> {
@ -48,7 +49,8 @@ class Prop {
final bool isArray;
final String readAnnotation;
final String writeAnnotation;
Prop(this.name, this.type, this.isArray, [this.readAnnotation = null, this.writeAnnotation = null]);
Prop(this.name, this.type, this.isArray,
[this.readAnnotation = null, this.writeAnnotation = null]);
}
class Evt {
@ -58,7 +60,8 @@ class Evt {
final bool isArray;
final String annotation;
Evt(this.name, this.type, this.isArray, [this.listenable = false, this.annotation]);
Evt(this.name, this.type, this.isArray,
[this.listenable = false, this.annotation]);
}
class Func {
@ -68,7 +71,7 @@ class Func {
final bool isArray;
final String annotation;
Func(this.name, this.returnType, this.argsType, this.isArray,
Func(this.name, this.returnType, this.isArray, this.argsType,
[this.annotation = null]);
}

View File

@ -1,5 +1,7 @@
import 'dart:ffi';
import '../../Net/IIP/DistributedResource.dart';
import '../../Data/BinaryList.dart';
import '../../Security/Integrity/SHA256.dart';
@ -55,8 +57,7 @@ class TypeTemplate {
*/
//@TODO: implement
static List<TypeTemplate> getDependencies(TypeTemplate template) =>
[];
static List<TypeTemplate> getDependencies(TypeTemplate template) => [];
EventTemplate getEventTemplateByName(String eventName) {
for (var i in _events) if (i.name == eventName) return i;
@ -106,15 +107,27 @@ class TypeTemplate {
List<PropertyTemplate> get properties => _properties;
TypeTemplate.fromType(Type type, [bool addToWarehouse, bool isWrapper]) {
TypeTemplate.fromType(Type type, [bool addToWarehouse = false]) {
// debugging print("FromType ${type.toString()}");
var instance = Warehouse.createInstance(type);
if (instance is IRecord)
_templateType = TemplateType.Record;
if (instance is DistributedResource)
_templateType = TemplateType.Wrapper;
else if (instance is IResource)
_templateType = TemplateType.Resource;
else if (instance is IRecord)
_templateType = TemplateType.Record;
else
throw new Exception("Type is neither a resource nor a record.");
throw new Exception(
"Type must implement IResource, IRecord or inherit from 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;
@ -125,47 +138,59 @@ class TypeTemplate {
// set guid
_classId = getTypeGuid(_className);
_version = describer.version;
if (addToWarehouse) Warehouse.putTemplate(this);
// _templates.add(template.classId, template);
for (var i = 0; i < describer.properties.length; i++) {
var pi = describer.properties[i];
var pt = PropertyTemplate(
this,
i,
pi.name,
TemplateDataType.fromType(pi.type, pi.isArray),
pi.readAnnotation,
pi.writeAnnotation,
0);
properties.add(pt);
}
if (describer.properties != null)
for (var i = 0; i < describer.properties.length; i++) {
var pi = describer.properties[i];
var pt = PropertyTemplate(
this,
i,
pi.name,
TemplateDataType.fromType(pi.type, pi.isArray),
pi.readAnnotation,
pi.writeAnnotation,
0);
properties.add(pt);
}
for (var i = 0; i < describer.functions.length; i++) {
var fi = describer.functions[i];
if (describer.functions != null)
for (var i = 0; i < describer.functions.length; i++) {
var fi = describer.functions[i];
List<ArgumentTemplate> args = fi.argsType.map((arg) => ArgumentTemplate(
arg.name, TemplateDataType.fromType(arg.type, arg.isArray)));
List<ArgumentTemplate> args = fi.argsType
.map((arg) => ArgumentTemplate(
arg.name, TemplateDataType.fromType(arg.type, arg.isArray)))
.toList();
var ft = FunctionTemplate(this, i, fi.name, args,
TemplateDataType.fromType(fi.returnType, fi.isArray), fi.annotation);
var ft = FunctionTemplate(
this,
i,
fi.name,
args,
TemplateDataType.fromType(fi.returnType, fi.isArray),
fi.annotation);
functions.add(ft);
}
functions.add(ft);
}
for (var i = 0; i < describer.events.length; i++) {
var ei = describer.events[i];
if (describer.events != null)
for (var i = 0; i < describer.events.length; i++) {
var ei = describer.events[i];
var et = new EventTemplate(
this,
i,
ei.name,
TemplateDataType.fromType(ei.type, ei.isArray),
ei.annotation,
ei.listenable);
var et = new EventTemplate(
this,
i,
ei.name,
TemplateDataType.fromType(ei.type, ei.isArray),
ei.annotation,
ei.listenable);
events.add(et);
}
events.add(et);
}
// append signals
events.forEach(_members.add);

View File

@ -23,6 +23,7 @@ SOFTWARE.
*/
import '../Data/AutoList.dart';
import 'FactoryEntry.dart';
import 'Template/TemplateType.dart';
import 'Template/TypeTemplate.dart';
import '../Data/Guid.dart';
@ -45,7 +46,6 @@ class Warehouse {
static Map<int, IResource> _resources = new Map<int, IResource>();
static int resourceCounter = 0;
static KeyList<TemplateType, KeyList<Guid, TypeTemplate>> _templates =
_initTemplates(); //
@ -60,13 +60,11 @@ class Warehouse {
return rt;
}
static KeyList<Type, Function()> _factory = _getBuiltInTypes();
static KeyList<Type, FactoryEntry> _factory = _getBuiltInTypes();
static KeyList<String, AsyncReply<IStore> Function(String, dynamic)>
protocols = _getSupportedProtocols();
static bool _warehouseIsOpen = false;
static final _urlRegex = RegExp(r'^(?:([^\s|:]*):\/\/([^\/]*)\/?(.*))');
@ -470,7 +468,11 @@ class Warehouse {
}
static T createInstance<T>(Type T) {
return _factory[T].call();
return _factory[T].instanceCreator.call();
}
static List<T> createArray<T>(Type T) {
return _factory[T].arrayCreator.call();
}
static AsyncReply<T> newResource<T extends IResource>(String name,
@ -479,7 +481,7 @@ class Warehouse {
IPermissionsManager manager = null,
attributes = null,
properties = null]) {
var resource = _factory[T].call();
var resource = _factory[T].instanceCreator.call();
if (properties != null) {
dynamic d = resource;
@ -525,8 +527,9 @@ class Warehouse {
static TypeTemplate getTemplateByType(Type type) {
// loaded ?
for (var tmps in _templates.values)
for (var tmp in tmps.values)
if (tmp.className == type.toString()) return tmp;
for (var tmp in tmps.values) if (tmp.definedType == type) return tmp;
//if (tmp.className == type.toString()) return tmp;
var template = new TypeTemplate.fromType(type, true);
@ -562,28 +565,31 @@ class Warehouse {
/// </summary>
/// <param name="className">Class name.</param>
/// <returns>Resource template.</returns>
static TypeTemplate getTemplateByClassName(String className, [TemplateType templateType = TemplateType.Unspecified]) {
static TypeTemplate getTemplateByClassName(String className,
[TemplateType templateType = TemplateType.Unspecified]) {
if (templateType == TemplateType.Unspecified) {
// look in resources
var template = _templates[TemplateType.Resource]
.values
.firstWhere((x) => x.className == className);
if (template != null) return template;
if (templateType == TemplateType.Unspecified)
{
// look in resources
var template = _templates[TemplateType.Resource].values.firstWhere((x) => x.className == className);
if (template != null)
return template;
// look in records
template = _templates[TemplateType.Record]
.values
.firstWhere((x) => x.className == className);
if (template != null) return template;
// look in records
template = _templates[TemplateType.Record].values.firstWhere((x) => x.className == className);
if (template != null)
return template;
// look in wrappers
template = _templates[TemplateType.Wrapper].values.firstWhere((x) => x.className == className);
return template;
}
else
{
return _templates[templateType].values.firstWhere((x) => x.className == className);
}
// look in wrappers
template = _templates[TemplateType.Wrapper]
.values
.firstWhere((x) => x.className == className);
return template;
} else {
return _templates[templateType]
.values
.firstWhere((x) => x.className == className);
}
}
static bool remove(IResource resource) {
@ -625,9 +631,17 @@ class Warehouse {
return rt;
}
static KeyList<Type, Function()> _getBuiltInTypes() {
var rt = KeyList<Type, Function()>();
rt.add(DistributedConnection, () => DistributedConnection());
static defineCreator(
Type type, Function instanceCreator, Function arrayCreator) {
_factory.add(type, FactoryEntry(type, instanceCreator, arrayCreator));
}
static KeyList<Type, FactoryEntry> _getBuiltInTypes() {
var rt = KeyList<Type, FactoryEntry>();
rt.add(
DistributedConnection,
FactoryEntry(DistributedConnection, () => DistributedConnection(),
() => DistributedConnection()));
return rt;
}
}