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:
2019-08-07 17:12:20 +03:00
parent b796bf9436
commit 1bf0bc32ae
80 changed files with 394 additions and 13 deletions

200
lib/src/Data/AutoList.dart Normal file
View File

@ -0,0 +1,200 @@
import '../Core/IDestructible.dart';
import 'Codec.dart';
import 'dart:collection';
class AutoList<T, ST> extends IDestructible with IterableMixin
{
List<T> _list = new List<T>();
ST _state;
bool _removableList;
sort(Function comparer)
{
_list.sort(comparer);
}
Iterator<T> get iterator => _list.iterator;
/// <summary>
/// Convert AutoList to array
/// </summary>
/// <returns>Array</returns>
//List<T> toList()
//{
// list.OrderBy()
// return _list;
//}
/// Create a new instance of AutoList
/// <param name="state">State object to be included when an event is raised.</param>
AutoList([ST state, List<T> values])
{
this._state = state;
this._removableList = Codec.implementsInterface<T, IDestructible>();
if (values != null)
addRange(values);
register("modified");
register("added");
register("removed");
register("cleared");
}
/// <summary>
/// Synchronization lock of the list
/// </summary>
//public object SyncRoot
//{
// get
// {
// return syncRoot;
// }
//}
/// <summary>
/// First item in the list
/// </summary>
//T first()
//{
// return _list.first;
//}
operator [](index)
{
return _list[index];
}
operator []=(index, value)
{
var oldValue = _list[index];
if (_removableList)
{
if (oldValue != null)
(oldValue as IDestructible).off("destroy", _itemDestroyed);
if (value != null)
value.on("destroy", _itemDestroyed);
}
//lock (syncRoot)
_list[index] = value;
emitArgs("modified", [_state, index, oldValue, value]);
}
/// <summary>
/// Add item to the list
/// </summary>
add(T value)
{
if (_removableList)
if (value != null)
(value as IDestructible).on("destroy", _itemDestroyed);
// lock (syncRoot)
_list.add(value);
emitArgs("add",[_state, value]);
}
/// <summary>
/// Add an array of items to the list
/// </summary>
addRange(List<T> values)
{
values.forEach((x)=>add(x));
}
_itemDestroyed(T sender)
{
remove(sender);
}
/// <summary>
/// Clear the list
/// </summary>
clear()
{
if (_removableList)
_list.forEach((x)=>(x as IDestructible)?.off("destroy", _itemDestroyed));
// lock (syncRoot)
_list.clear();
emitArgs("cleared", [_state]);
}
/// <summary>
/// Remove an item from the list
/// <param name="value">Item to remove</param>
/// </summary>
remove(T value)
{
if (!_list.contains(value))
return;
if (_removableList)
if (value != null)
(value as IDestructible).off("destroy", _itemDestroyed);
//lock (syncRoot)
_list.remove(value);
emitArgs("removed", [_state, value]);
}
/// <summary>
/// Number of items in the list
/// </summary>
get count => _list.length;
get length => _list.length;
/// <summary>
/// Check if an item exists in the list
/// </summary>
/// <param name="value">Item to check if exists</param>
//contains(T value) => _list.contains(value);
/// <summary>
/// Check if any item of the given array is in the list
/// </summary>
/// <param name="values">Array of items</param>
containsAny(values)
{
if (values is List<T>)
{
for(var v in values)
{
if (_list.contains(v))
return true;
}
}
else if (values is AutoList<T, ST>)
{
for(var v in values._list)
{
if (_list.contains(v))
return true;
}
}
return false;
}
@override
void destroy() {
clear();
}
}

View File

@ -0,0 +1,550 @@
/*
* Copyright (c) 2019 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 26/07/2019.
*/
import '../Core/AsyncReply.dart';
import 'dart:typed_data';
import 'DC.dart';
import 'DataType.dart';
import 'Guid.dart';
class BinaryList
{
var _list = new List<int>();
int get length => _list.length;
BinaryList addDateTime(DateTime value)
{
_list.addAll(DC.dateTimeToBytes(value));
return this;
}
BinaryList insertDateTime(int position, DateTime value)
{
_list.insertAll(position, DC.dateTimeToBytes(value));
return this;
}
BinaryList addDateTimeArray(List<DateTime> value)
{
_list.addAll(DC.dateTimeArrayToBytes(value));
return this;
}
BinaryList insertDateTimeArray(int position, List<DateTime> value)
{
_list.insertAll(position, DC.dateTimeArrayToBytes(value));
return this;
}
BinaryList addGuid(Guid value)
{
_list.addAll(DC.guidToBytes(value));
return this;
}
BinaryList insertGuid(int position, Guid value)
{
_list.insertAll(position, DC.guidToBytes(value));
return this;
}
BinaryList addGuidArray(List<Guid> value)
{
_list.addAll(DC.guidArrayToBytes(value));
return this;
}
BinaryList insertGuidArray(int position, List<Guid> value)
{
_list.insertAll(position, DC.guidArrayToBytes(value));
return this;
}
BinaryList addUint8Array(Uint8List value)
{
_list.addAll(value);
return this;
}
BinaryList addDC(DC value)
{
_list.addAll(value.toArray());
return this;
}
BinaryList insertUint8Array(int position, Uint8List value)
{
_list.insertAll(position, value);
return this;
}
/*
BinaryList addHex(String value)
{
return this.addUint8Array(DC.fromHex(value, null));
}
BinaryList insertHex(int position, String value)
{
return this.insertUint8Array(position, DC.fromHex(value, null));
}
*/
BinaryList addString(String value)
{
_list.addAll(DC.stringToBytes(value));
return this;
}
BinaryList insertString(int position, String value)
{
_list.insertAll(position, DC.stringToBytes(value));
return this;
}
BinaryList addStringArray(List<String> value)
{
_list.addAll(DC.stringArrayToBytes(value));
return this;
}
BinaryList insertStringArray(int position, List<String> value)
{
_list.insertAll(position, DC.stringArrayToBytes(value));
return this;
}
BinaryList insertUint8(int position, int value)
{
_list.insert(position, value);
return this;
}
BinaryList addUint8(int value)
{
_list.add(value);
return this;
}
BinaryList addInt8(int value)
{
_list.add(value);
return this;
}
BinaryList insertInt8(int position, int value)
{
_list.insert(position, value);
return this;
}
BinaryList addInt8Array(Int8List value)
{
_list.addAll(DC.int8ArrayToBytes(value));
return this;
}
BinaryList insertInt8Array(int position, Int8List value)
{
_list.insertAll(position, DC.int8ArrayToBytes(value));
return this;
}
BinaryList addChar(int value)
{
_list.addAll(DC.charToBytes(value));
return this;
}
BinaryList InsertChar(int position, int value)
{
_list.insertAll(position, DC.charToBytes(value));
return this;
}
BinaryList addCharArray(Uint16List value)
{
_list.addAll(DC.charArrayToBytes(value));
return this;
}
BinaryList InsertCharArray(int position, Uint16List value)
{
_list.insertAll(position, DC.charArrayToBytes(value));
return this;
}
BinaryList addBoolean(bool value)
{
_list.addAll(DC.boolToBytes(value));
return this;
}
BinaryList insertBoolean(int position, bool value)
{
_list.insertAll(position, DC.boolToBytes(value));
return this;
}
BinaryList addBooleanArray(List<bool> value)
{
_list.addAll(DC.boolToBytes(value));
return this;
}
BinaryList insertBooleanArray(int position, List<bool> value)
{
_list.insertAll(position, DC.boolToBytes(value));
return this;
}
BinaryList addUint16(int value)
{
_list.addAll(DC.uint16ToBytes(value));
return this;
}
BinaryList insertUint16(int position, int value)
{
_list.insertAll(position, DC.uint16ToBytes(value));
return this;
}
BinaryList addUint16Array(Uint16List value)
{
_list.addAll(DC.uint16ArrayToBytes(value));
return this;
}
BinaryList insertUint16Array(int position, Uint16List value)
{
_list.insertAll(position, DC.uint16ArrayToBytes(value));
return this;
}
BinaryList addInt16(int value)
{
_list.addAll(DC.int16ToBytes(value));
return this;
}
BinaryList insertInt16(int position, int value)
{
_list.insertAll(position, DC.int16ToBytes(value));
return this;
}
BinaryList addInt16Array(Int16List value)
{
_list.addAll(DC.int16ArrayToBytes(value));
return this;
}
BinaryList insertInt16Array(int position, Int16List value)
{
_list.insertAll(position, DC.int16ArrayToBytes(value));
return this;
}
BinaryList addUint32(int value)
{
_list.addAll(DC.uint32ToBytes(value));
return this;
}
BinaryList insertUint32(int position, int value)
{
_list.insertAll(position, DC.uint32ToBytes(value));
return this;
}
BinaryList addUint32Array(Uint32List value)
{
_list.addAll(DC.uint32ArrayToBytes(value));
return this;
}
BinaryList InsertUint32Array(int position, Uint32List value)
{
_list.insertAll(position, DC.uint32ArrayToBytes(value));
return this;
}
BinaryList addInt32(int value)
{
_list.addAll(DC.int32ToBytes(value));
return this;
}
BinaryList insertInt32(int position, int value)
{
_list.insertAll(position, DC.int32ToBytes(value));
return this;
}
BinaryList addInt32Array(Int32List value)
{
_list.addAll(DC.int32ArrayToBytes(value));
return this;
}
BinaryList insertInt32Array(int position, Int32List value)
{
_list.insertAll(position, DC.int32ArrayToBytes(value));
return this;
}
BinaryList addUint64(int value)
{
_list.addAll(DC.uint64ToBytes(value));
return this;
}
BinaryList insertUint64(int position, int value)
{
_list.insertAll(position, DC.uint64ToBytes(value));
return this;
}
BinaryList addUint64Array(Uint64List value)
{
_list.addAll(DC.uint64ArrayToBytes(value));
return this;
}
BinaryList InsertUint64Array(int position, Uint64List value)
{
_list.insertAll(position, DC.uint64ArrayToBytes(value));
return this;
}
BinaryList addInt64(int value)
{
_list.addAll(DC.int64ToBytes(value));
return this;
}
BinaryList insertInt64(int position, int value)
{
_list.insertAll(position, DC.int64ToBytes(value));
return this;
}
BinaryList addInt64Array(Int64List value)
{
_list.addAll(DC.int64ArrayToBytes(value));
return this;
}
BinaryList insertInt64Array(int position, Int64List value)
{
_list.insertAll(position, DC.int64ArrayToBytes(value));
return this;
}
BinaryList addFloat32(double value)
{
_list.addAll(DC.float32ToBytes(value));
return this;
}
BinaryList insertFloat32(int position, double value)
{
_list.insertAll(position, DC.float32ToBytes(value));
return this;
}
BinaryList addFloat32Array(Float32List value)
{
_list.addAll(DC.float32ArrayToBytes(value));
return this;
}
BinaryList insertFloat32Array(int position, Float32List value)
{
_list.insertAll(position, DC.float32ArrayToBytes(value));
return this;
}
BinaryList addFloat64(double value)
{
_list.addAll(DC.float64ToBytes(value));
return this;
}
BinaryList insertFloat64(int position, double value)
{
_list.insertAll(position, DC.float64ToBytes(value));
return this;
}
BinaryList addFloat64Array(Float64List value)
{
_list.addAll(DC.float64ArrayToBytes(value));
return this;
}
BinaryList insertFloat64Array(int position, Float64List value)
{
_list.insertAll(position, DC.float64ArrayToBytes(value));
return this;
}
BinaryList add(type, value)
{
switch (type)
{
case DataType.Bool:
addBoolean(value);
return this;
case DataType.BoolArray:
addBooleanArray(value);
return this;
case DataType.UInt8:
addUint8(value);
return this;
case DataType.UInt8Array:
addUint8Array(value);
return this;
case DataType.Int8:
addInt8(value);
return this;
case DataType.Int8Array:
addInt8Array(value);
return this;
case DataType.Char:
addChar(value);
return this;
case DataType.CharArray:
addCharArray(value);
return this;
case DataType.UInt16:
addUint16(value);
return this;
case DataType.UInt16Array:
addUint16Array(value);
return this;
case DataType.Int16:
addInt16(value);
return this;
case DataType.Int16Array:
addInt16Array(value);
return this;
case DataType.UInt32:
addUint32(value);
return this;
case DataType.UInt32Array:
addUint32Array(value);
return this;
case DataType.Int32:
addInt32(value);
return this;
case DataType.Int32Array:
addInt32Array(value);
return this;
case DataType.UInt64:
addUint64(value);
return this;
case DataType.UInt64Array:
addUint64Array(value);
return this;
case DataType.Int64:
addInt64(value);
return this;
case DataType.Int64Array:
addInt64Array(value);
return this;
case DataType.Float32:
addFloat32(value);
return this;
case DataType.Float32Array:
addFloat32Array(value);
return this;
case DataType.Float64:
addFloat64(value);
return this;
case DataType.Float64Array:
addFloat64Array(value);
return this;
case DataType.String:
addString(value);
return this;
case DataType.StringArray:
addStringArray(value);
return this;
case DataType.DateTime:
addDateTime(value);
return this;
case DataType.DateTimeArray:
addDateTimeArray(value);
return this;
default:
throw new Exception("Not Implemented " + type.ToString());
//return this;
}
}
/// <summary>
/// Convert the _list to an array of bytes
/// </summary>
/// <returns>Bytes array</returns>
Uint8List toArray()
{
return Uint8List.fromList(_list);
}
DC toDC()
{
return new DC.fromUint8Array(toArray());
}
AsyncReply<dynamic> done()
{
return null;
//
}
}

1074
lib/src/Data/Codec.dart Normal file

File diff suppressed because it is too large Load Diff

643
lib/src/Data/DC.dart Normal file
View File

@ -0,0 +1,643 @@
/*
* Copyright (c) 2019 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import 'dart:typed_data';
import 'dart:convert';
import 'BinaryList.dart';
import 'dart:collection';
import 'Guid.dart';
/**
* Created by Ahmed Zamil on 6/10/2019.
*/
const UNIX_EPOCH = 621355968000000000;
const TWO_PWR_32 = (1 << 16) * (1 << 16);
class DC with IterableMixin<int>
{
Uint8List _data;
ByteData _dv;
DC(int length)
{
_data = new Uint8List(length);
_dv = ByteData.view(_data.buffer);
}
DC.fromUint8Array(Uint8List array)
{
_data = array;
_dv = ByteData.view(_data.buffer);
}
DC.fromList(List<int> list)
{
_data = Uint8List.fromList(list);
_dv = ByteData.view(_data.buffer);
}
operator [](index) => _data[index];
operator []=(index,value) => _data[index] = value;
int get length => _data.length;
Iterator<int> get iterator => _data.iterator;
static DC hexToBytes(String value)
{
// convert hex to Uint8Array
var rt = new DC(value.length~/2);
for(var i = 0; i < rt.length; i++)
rt[i] = int.parse(value.substring(i*2, 2), radix: 16);
return rt;
}
static DC boolToBytes(value)
{
var rt = new DC(1);
rt.setBoolean(0, value);
return rt;
}
static DC guidToBytes(Guid value)
{
var rt = new DC(16);
rt.setGuid(0, value);
return rt;
}
static DC guidArrayToBytes(List<Guid> value)
{
var rt = new DC(value.length * 16);
for(var i = 0; i < value.length; i++)
rt.setGuid(i * 16, value[i]);
return rt;
}
static DC boolArrayToBytes(List<bool> value)
{
var rt = new DC(value.length);
for(var i = 0; i < value.length; i++)
rt[i] = value[i] ? 1 : 0;
return rt;
}
static DC int8ToBytes(value)
{
var rt = new DC(1);
rt.setInt8(0, value);
return rt;
}
static DC int8ArrayToBytes(Int8List value)
{
var rt = new DC(value.length);
for(var i = 0; i < value.length; i++)
rt.setInt8(i, value[i]);
return rt;
}
static DC uint8ToBytes(value)
{
var rt = new DC(1);
rt.setUint8(0, value);
return rt;
}
static DC uint8ArrayToBytes(Uint8List value)
{
var rt = new DC(value.length);
for(var i = 0; i < value.length; i++)
rt.setUint8(i, value[i]);
return rt;
}
static DC charToBytes(int value)
{
var rt = new DC(2);
rt.setChar(0, value);
return rt;
}
static DC charArrayToBytes(Uint16List value)
{
var rt = new DC(value.length * 2);
for(var i = 0; i < value.length; i++)
rt.setChar(i*2, value[i]);
return rt;
}
static DC int16ToBytes(int value)
{
var rt = new DC(2);
rt.setInt16(0, value);
return rt;
}
static DC int16ArrayToBytes(List<int> value)
{
var rt = new DC(value.length * 2);
for(var i = 0; i < value.length; i++)
rt.setInt16(i*2, value[i]);
return rt;
}
static DC uint16ToBytes(int value)
{
var rt = new DC(2);
rt.setUint16(0, value);
return rt;
}
static DC uint16ArrayToBytes(Uint16List value)
{
var rt = new DC(value.length * 2);
for(var i = 0; i < value.length; i++)
rt.setUint16(i*2, value[i]);
return rt;
}
static DC int32ToBytes(int value)
{
var rt = new DC(4);
rt.setInt32(0, value);
return rt;
}
static DC int32ArrayToBytes(Int32List value)
{
var rt = new DC(value.length * 4);
for(var i = 0; i < value.length; i++)
rt.setInt32(i*4, value[i]);
return rt;
}
static DC uint32ToBytes(int value)
{
var rt = new DC(4);
rt.setUint32(0, value);
return rt;
}
static DC uint32ArrayToBytes(Uint32List value)
{
var rt = new DC(value.length * 4);
for(var i = 0; i < value.length; i++)
rt.setUint32(i*4, value[i]);
return rt;
}
static DC float32ToBytes(double value)
{
var rt = new DC(4);
rt.setFloat32(0, value);
return rt;
}
static DC float32ArrayToBytes(Float32List value)
{
var rt = new DC(value.length * 4);
for(var i = 0; i < value.length; i++)
rt.setFloat32(i*4, value[i]);
return rt;
}
static DC int64ToBytes(int value)
{
var rt = new DC(8);
rt.setInt64(0, value);
return rt;
}
static DC int64ArrayToBytes(Int64List value)
{
var rt = new DC(value.length * 8);
for(var i = 0; i < value.length; i++)
rt.setInt64(i*8, value[i]);
return rt;
}
static DC uint64ToBytes(int value)
{
var rt = new DC(8);
rt.setUint64(0, value);
return rt;
}
static DC uint64ArrayToBytes(Uint64List value)
{
var rt = new DC(value.length * 8);
for(var i = 0; i < value.length; i++)
rt.setUint64(i*8, value[i]);
return rt;
}
static DC float64ToBytes(double value)
{
var rt = new DC(8);
rt.setFloat64(0, value);
return rt;
}
static DC float64ArrayToBytes(Float64List value)
{
var rt = new DC(value.length * 8);
for(var i = 0; i < value.length; i++)
rt.setFloat64(i*8, value[i]);
return rt;
}
static DC dateTimeToBytes(DateTime value)
{
var rt = new DC(8);
rt.setDateTime(0, value);
return rt;
}
static DC dateTimeArrayToBytes(List<DateTime> value)
{
var rt = new DC(value.length * 8);
for(var i = 0; i < value.length; i++)
rt.setDateTime(i*8, value[i]);
return rt;
}
static DC stringToBytes(String value)
{
var bytes = utf8.encode(value);
var rt = new DC.fromList(bytes);
return rt;
}
static DC stringArrayToBytes(List<String> value)
{
var list = new BinaryList();
for(var i = 0; i < value.length; i++)
{
var s = DC.stringToBytes(value[i]);
list.addUint32(s.length).addUint8Array(s.toArray());
}
return list.toDC();
}
DC append(DC src, int offset, int length)
{
//if (!(src is DC))
// src = new DC(src);
var appendix = src.clip(offset, length);
var rt = new DC(this.length + appendix.length);
rt.set(this, 0);
rt.set(appendix, this.length);
this._data = rt._data;
this._dv = rt._dv;
return this;
}
set(DC dc, int offset)
{
_data.setRange(offset, offset + dc.length, dc._data);
}
static combine(a, aOffset, aLength, b, bOffset, bLength)
{
if (!(a is DC))
a = new DC(a);
if (!(b is DC))
b = new DC(b);
a = a.clip(aOffset, aLength);
b = b.clip(bOffset, bLength);
var rt = new DC(a.length + b.length);
rt.set(a, 0);
rt.set(b, a.length);
return rt;
}
DC clip(offset, length)
{
return DC.fromUint8Array(Uint8List.fromList(_data.getRange(offset, offset + length).toList()));
}
getInt8(int offset)
{
return _dv.getInt8(offset);
}
getUint8(int offset)
{
return _data[offset];// this.dv.getUint8(offset);
}
getInt16(int offset)
{
return _dv.getInt16(offset);
}
getUint16(int offset)
{
return _dv.getUint16(offset);
}
getInt32(int offset)
{
return _dv.getInt32(offset);
}
getUint32(int offset)
{
return _dv.getUint32(offset);
}
getFloat32(int offset)
{
return _dv.getFloat32(offset);
}
getFloat64(int offset)
{
return _dv.getFloat64(offset);
}
setInt8(int offset, int value)
{
return _dv.setInt8(offset, value);
}
setUint8(int offset, int value)
{
return _dv.setUint8(offset, value);
}
setInt16(int offset, int value)
{
return _dv.setInt16(offset, value);
}
setUint16(int offset, int value)
{
return _dv.setUint16(offset, value);
}
setInt32(int offset, int value)
{
return _dv.setInt32(offset, value);
}
setUint32(int offset, int value)
{
return _dv.setUint32(offset, value);
}
setFloat32(int offset, double value)
{
return _dv.setFloat32(offset, value);
}
setFloat64(int offset, double value)
{
return _dv.setFloat64(offset, value);
}
Int8List getInt8Array(int offset, int length)
{
return _data.buffer.asInt8List(offset, length);
}
Uint8List getUint8Array(int offset, int length)
{
return _data.buffer.asUint8List(offset, length);
}
Int16List getInt16Array(int offset, int length)
{
return _data.buffer.asInt16List(offset, length);
}
Uint16List getUint16Array(int offset, int length)
{
return _data.buffer.asUint16List(offset, length);
}
Int32List getInt32Array(int offset, int length)
{
return _data.buffer.asInt32List(offset, length);
}
Uint32List getUint32Array(int offset, int length)
{
return _data.buffer.asUint32List(offset, length);
}
Float32List getFloat32Array(int offset, int length)
{
return _data.buffer.asFloat32List(offset, length);
}
Float64List getFloat64Array(int offset, int length)
{
return _data.buffer.asFloat64List(offset, length);
}
Int64List getInt64Array(int offset, int length)
{
return _data.buffer.asInt64List(offset, length);
}
Uint64List getUint64Array(int offset, int length)
{
return _data.buffer.asUint64List(offset, length);
}
bool getBoolean(int offset)
{
return this.getUint8(offset) > 0;
}
setBoolean(int offset, bool value)
{
this.setUint8(offset, value ? 1: 0);
}
List<bool> getBooleanArray(int offset, int length)
{
var rt = new List<bool>();
for(var i = 0; i < length; i++)
rt.add(this.getBoolean(offset+i));
return rt;
}
String getChar(int offset)
{
return String.fromCharCode(this.getUint16(offset));
}
setChar(int offset, int value)
{
this.setUint16(offset, value); //value.codeUnitAt(0));
}
List<String> getCharArray(int offset, int length)
{
var rt = new List<String>();
for(var i = 0; i < length; i+=2)
rt.add(this.getChar(offset+i));
return rt;
}
String getHex(offset, length)
{
var rt = "";
for(var i = offset; i < offset + length; i++) {
var h = this[i].toString(16);
rt += h.length == 1 ? "0" + h : h;
}
return rt;
}
/*
List<T> toList<T>(offset, length)
{
var rt = new List<T>();
for(var i = 0; i < length; i++)
rt[i] = _data[offset+i] as T;
return rt;
}*/
Uint8List toArray() => _data;
String getString(offset, length)
{
var bytes = clip(offset, length)._data;// toList(offset, length);
var str = utf8.decode(bytes);
return str;
}
List<String> getStringArray(offset, length)
{
var rt = List<String>();
var i = 0;
while (i < length)
{
var cl = this.getUint32(offset + i);
i += 4;
rt.add(this.getString(offset + i, cl));
i += cl;
}
return rt;
}
getInt64(offset)
{
return _dv.getUint64(offset);
}
getUint64(offset)
{
return _dv.getInt64(offset);
}
void setInt64(offset, value)
{
_dv.setInt64(offset, value);
}
void setUint64(offset, value)
{
_dv.setUint64(offset, value);
}
setDateTime(offset, value)
{
// Unix Epoch
var ticks = 621355968000000000 + (value.getTime() * 10000);
this.setUint64(offset, ticks);
}
DateTime getDateTime(int offset)
{
var ticks = this.getUint64(offset);
// there are 10,000 ticks in a millisecond
return DateTime.fromMillisecondsSinceEpoch((ticks-UNIX_EPOCH)~/10000);
}
List<DateTime> getDateTimeArray(int offset, int length)
{
var rt = new List<DateTime>();
for(var i = 0; i < length; i+=8)
rt.add(this.getDateTime(offset+i));
return rt;
}
Guid getGuid(int offset)
{
return new Guid(this.clip(offset, 16));
}
setGuid(int offset, Guid guid)
{
set(guid.value, offset);
}
List<Guid> getGuidArray(int offset, int length)
{
var rt = [];
for(var i = 0; i < length; i+=16)
rt.add(this.getGuid(offset+i));
return rt;
}
bool sequenceEqual(ar)
{
if (ar.length != this.length)
return false;
else
{
for(var i = 0; i < this.length; i++)
if (ar[i] != this[i])
return false;
}
return true;
}
}

115
lib/src/Data/DataType.dart Normal file
View File

@ -0,0 +1,115 @@
/*
Copyright (c) 2019 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class DataType
{
static const int Void = 0x0,
//Variant,
Bool = 1,
Int8 = 2,
UInt8 = 3,
Char = 4,
Int16 = 5,
UInt16 = 6,
Int32 = 7,
UInt32 = 8,
Int64 = 9,
UInt64 = 0xA,
Float32 = 0xB,
Float64 = 0xC,
Decimal = 0xD,
DateTime = 0xE,
Resource = 0xF,
DistributedResource = 0x10,
ResourceLink = 0x11,
String = 0x12,
Structure = 0x13,
//Stream,
//Array = 0x80,
VarArray = 0x80,
BoolArray = 0x81,
UInt8Array = 0x82,
Int8Array = 0x83,
CharArray = 0x84,
Int16Array = 0x85,
UInt16Array = 0x86,
Int32Array = 0x87,
UInt32Array = 0x88,
Int64Array = 0x89,
UInt64Array = 0x8A,
Float32Array = 0x8B,
Float64Array = 0x8C,
DecimalArray = 0x8D,
DateTimeArray = 0x8E,
ResourceArray = 0x8F,
DistributedResourceArray = 0x90,
ResourceLinkArray = 0x91,
StringArray = 0x92,
StructureArray = 0x93,
NotModified = 0x7F,
Unspecified = 0xFF;
static bool isArray(int type)
{
return ((type & 0x80) == 0x80) && (type != NotModified);
}
static int getElementType(int type)
{
return type & 0x7F;
}
static int size(int type)
{
switch (type)
{
case DataType.Void:
case DataType.NotModified:
return 0;
case DataType.Bool:
case DataType.UInt8:
case DataType.Int8:
return 1;
case DataType.Char:
case DataType.UInt16:
case DataType.Int16:
return 2;
case DataType.Int32:
case DataType.UInt32:
case DataType.Float32:
case DataType.Resource:
return 4;
case DataType.Int64:
case DataType.UInt64:
case DataType.Float64:
case DataType.DateTime:
return 8;
case DataType.DistributedResource:
return 4;
default:
return -1;
}
}
}

18
lib/src/Data/Guid.dart Normal file
View File

@ -0,0 +1,18 @@
import 'DC.dart';
class Guid
{
DC _data;
Guid(DC data)
{
_data = data;
}
DC get value => _data;
@override
String toString() {
return _data.getString(0, _data.length);
}
}

156
lib/src/Data/KeyList.dart Normal file
View File

@ -0,0 +1,156 @@
/*
Copyright (c) 2019 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import '../Core/IEventHandler.dart';
import '../Core/IDestructible.dart';
import 'dart:collection';
import 'Codec.dart';
class KeyList<KT, T> extends IEventHandler with MapMixin<KT, T>
{
dynamic owner;
Map<KT, T> _map = new Map<KT, T>();
Iterator<KT> get iterator => _map.keys.iterator;
Iterable<KT> get keys => _map.keys;
Iterable<T> get values => _map.values;
operator[](index) => _map[index];
operator []= (index, value) => add(index, value);
at(int index) => _map.values.elementAt(index);
bool _removableList;
T take(KT key)
{
if (_map.containsKey(key))
{
var v = _map[key];
remove(key);
return v;
}
else
return null;
}
List<T> toArray() => _map.values.toList();
void add(KT key, T value)
{
if (_removableList)
if (value != null)
(value as IDestructible).on("destroy", _itemDestroyed);
if (_map.containsKey(key))
{
var oldValue = _map[key];
if (_removableList)
if (oldValue != null)
(oldValue as IDestructible).off("destroy", _itemDestroyed);
_map[key] = value;
emitArgs("modified", [key, oldValue, value, this]);
}
else
{
_map[key] = value;
emitArgs("add", [value, this]);
}
}
_itemDestroyed(T sender)
{
removeValue(sender);
}
removeValue(T value)
{
var toRemove = new List<KT>();
for (var k in _map.keys)
if (_map[k] == value)
toRemove.add(k);
for (var k in toRemove)
remove(k);
}
clear()
{
if (_removableList)
for (var v in _map.values)
(v as IDestructible)?.off("destroy", _itemDestroyed);
_map.clear();
emitArgs("cleared", [this]);
}
T remove(key)
{
if (!_map.containsKey(key))
return null;
var value = _map[key];
if (_removableList)
(value as IDestructible)?.off("destroy", _itemDestroyed);
_map.remove(key);
emitArgs("removed", [key, value, this]);
return value;
}
int get count => _map.length;
bool contains(KT key) => _map.containsKey(key);
KeyList([owner = null])
{
_removableList = Codec.implementsInterface<T, IDestructible>();
this.owner = owner;
}
}

View File

@ -0,0 +1,4 @@
class NotModified
{
}

View File

@ -0,0 +1,23 @@
class PropertyValue
{
/// <summary>
/// Get or set the value.
/// </summary>
dynamic value;
/// <summary>
/// Get or set date of modification or occurrence.
/// </summary>
DateTime date;
/// <summary>
/// Get or set property age.
/// </summary>
int age;
/// <summary>
/// Create an instance of PropertyValue.
/// </summary>
/// <param name="value">Value.</param>
/// <param name="age">Age.</param>
/// <param name="date">Date.</param>
PropertyValue(this.value, this.age, this.date);
}

View File

@ -0,0 +1,7 @@
class ResourceComparisonResult
{
static const Null = 0;
static const Distributed = 1;
static const Local = 2;
static const Same = 3;
}

View File

@ -0,0 +1,4 @@
class SizeObject
{
int size;
}

View File

@ -0,0 +1,59 @@
/*
Copyright (c) 2019 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'dart:collection';
class Structure with MapMixin<String, Object>//, IterableMixin<String>
{
Map<String, Object> _map = new Map<String, Object>();
Iterator<String> get iterator => _map.keys.iterator;
Iterable<String> get keys => _map.keys;
operator[](index) => _map[index];
operator []= (index, value) => _map[index] = value;
remove(key) => _map.remove(key);
clear() => _map.clear();
at(int index) => _map.values.elementAt(index);
List<String> getKeys() => _map.keys.toList();
Structure.fromMap(Map map)
{
for(var i in map.keys)
_map[i.toString()] = map[i];
}
Structure()
{
// do nothing
}
}

View File

@ -0,0 +1,8 @@
class StructureComparisonResult
{
static const int Null = 0;
static const int Structure = 1;
static const int StructureSameKeys = 2;
static const int StructureSameTypes = 3;
static const int Same = 4;
}

View File

@ -0,0 +1,8 @@
class StructureMetadata
{
List<String> keys;
List<int> types;
//const StructureMetadata(this.keys, this.types);
}

View File

@ -0,0 +1,4 @@
class ValueObject
{
dynamic value;
}