Skip to content

Commit

Permalink
Native C/C++ struct feature #2 now is part of common developments of …
Browse files Browse the repository at this point in the history
…Conari:

not so bad... so it makes sense to continue improve :)

+IConfig.CacheDLR to cache dynamic types & BReader to get values from byte-sequence.

also fixed Compiler Warning (level 1) C4190
  • Loading branch information
3F committed Aug 12, 2016
1 parent 2d0c276 commit 4b17e28
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 142 deletions.
1 change: 1 addition & 0 deletions Conari/Conari.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="Core\ILoader.cs" />
<Compile Include="Core\Loader.cs" />
<Compile Include="ConariL.cs" />
<Compile Include="Native\Core\BReader.cs" />
<Compile Include="Native\Core\Field.cs" />
<Compile Include="Native\Core\BType.cs" />
<Compile Include="Native\Core\Raw.cs" />
Expand Down
6 changes: 4 additions & 2 deletions Conari/ConariL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public dynamic DLR
/// <param name="prefix">Optional prefix to use via `bind&lt;&gt;`</param>
public ConariL(IConfig cfg, CallingConvention conv, string prefix = null)
{
DLR = new ProviderDLR(this);

Prefix = prefix;
Convention = conv;
init(cfg);
Expand Down Expand Up @@ -97,6 +95,10 @@ protected void init(IConfig cfg)
throw new ArgumentException("Configuration cannot be null.");
}

DLR = new ProviderDLR(this) {
Cache = cfg.CacheDLR
};

if(cfg.LazyLoading) {
Library = new Link(cfg.LibName);
}
Expand Down
14 changes: 12 additions & 2 deletions Conari/Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,31 @@ public bool LazyLoading
set;
}

/// <summary>
/// To cache dynamic types.
/// </summary>
public bool CacheDLR
{
get;
set;
}

public static explicit operator String(Config cfg)
{
return cfg.LibName;
}

public static explicit operator Config(String lib)
{
return new Config() { LibName = lib };
return new Config(lib);
}

/// <param name="lib">The library.</param>
public Config(string lib)
: this()
{
LibName = lib;
LibName = lib;
CacheDLR = true;
}
}
}
5 changes: 5 additions & 0 deletions Conari/Core/IConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ public interface IConfig
/// To load library only when it required.
/// </summary>
bool LazyLoading { get; set; }

/// <summary>
/// To cache dynamic types.
/// </summary>
bool CacheDLR { get; set; }
}
}
4 changes: 2 additions & 2 deletions Conari/Core/ProviderDLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public bool Cache

/// <summary>
/// Magic methods. Invoking.
/// <<![CDATA[
/// </summary>
/// <![CDATA[
/// `[result =] name<return_type>([{argument_types}])`
/// ]]>
/// </summary>
/// <param name="binder"></param>
/// <param name="args"></param>
/// <param name="result"></param>
Expand Down
211 changes: 211 additions & 0 deletions Conari/Native/Core/BReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Denis Kuzmin <[email protected]>
*
* 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.
*/

using System;

namespace net.r_eg.Conari.Native.Core
{
public sealed class BReader
{
private byte[] data;
private int offset;

/// <summary>
/// To get manually the value from byte-sequence.
/// </summary>
/// <param name="type">The type of result value.</param>
/// <param name="tsize">Actual size of value in byte-sequence.</param>
/// <param name="offset">Offset at left.</param>
/// <param name="data"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static dynamic GetValue(Type type, int tsize, ref int offset, ref byte[] data)
{
// the System.IO.BinaryReader as variant, but we will use own implementation ...

byte[] val = range(ref data, offset, tsize);
offset += tsize;

if(type == typeof(Byte)) {
return (Byte)val[0];
}

if(type == typeof(SByte)) {
return (SByte)val[0];
}

if(type == typeof(Int16)) {
return BitConverter.ToInt16(val, 0);
}

if(type == typeof(UInt16)) {
return BitConverter.ToUInt16(val, 0);
}

if(type == typeof(Int32)) {
return BitConverter.ToInt32(val, 0);
}

if(type == typeof(UInt32)) {
return BitConverter.ToUInt32(val, 0);
}

if(type == typeof(Int64)) {
return BitConverter.ToInt64(val, 0);
}

if(type == typeof(UInt64)) {
return BitConverter.ToUInt64(val, 0);
}

if(type == typeof(IntPtr))
{
if(IntPtr.Size == sizeof(Int64)) {
return BitConverter.ToInt64(val, 0);
}
return BitConverter.ToInt32(val, 0);
}

if(type == typeof(UIntPtr))
{
if(UIntPtr.Size == sizeof(UInt64)) {
return BitConverter.ToUInt64(val, 0);
}
return BitConverter.ToUInt32(val, 0);
}

if(type == typeof(Single)) {
return BitConverter.ToSingle(val, 0);
}

if(type == typeof(Double)) {
return BitConverter.ToDouble(val, 0);
}

if(type == typeof(Boolean)) {
return BitConverter.ToBoolean(val, 0);
}

if(type == typeof(Char)) {
return BitConverter.ToChar(val, 0);
}

if(type == typeof(String))
{
//return new Types.CharPtr((IntPtr)field.value);

if(IntPtr.Size == sizeof(Int64)) {
return BitConverter.ToInt64(val, 0);
}
return BitConverter.ToInt32(val, 0);
}

throw new NotSupportedException($"The type `{type}` is not supported.");
}

/// <summary>
/// Try to get the next value in byte-sequence.
/// </summary>
/// <param name="type">The type of value.</param>
/// <param name="tsize">Actual size of value.</param>
/// <returns></returns>
public bool tryNext(Type type, int tsize, out dynamic value)
{
if(offset + tsize > data.Length) {
value = null;
return false;
}

value = GetValue(type, tsize, ref offset, ref data);
return true;
}

/// <summary>
/// Gets next value in byte-sequence.
/// </summary>
/// <param name="type"></param>
/// <param name="tsize"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public dynamic next(Type type, int tsize)
{
dynamic value;
if(tryNext(type, tsize, out value)) {
return value;
}

throw new IndexOutOfRangeException(
$"The type `{type}` with selected size '{tsize}' cannot be read from byte-sequence. Offset: {offset}"
);
}

/// <summary>
/// Alias to `next(Type type, int tsize)`
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tsize"></param>
/// <returns></returns>
public dynamic next<T>(int tsize)
{
return next(typeof(T), tsize);
}

///// <summary>
///// Gets next value in byte-sequence.
///// </summary>
///// <param name="type">The type of value.</param>
///// <returns></returns>
//public dynamic next(Type type)
//{
// return next(type, NativeData.SizeOf(type));
//}

/// <summary>
/// To reset position.
/// </summary>
/// <param name="offset"></param>
public void reset(int offset = 0)
{
this.offset = offset;
}

public BReader(byte[] raw, int offset = 0)
{
data = raw;
this.offset = offset;
}

private static byte[] range(ref byte[] data, int offset, int len)
{
int idx = 0;
byte[] ret = new byte[Math.Max(0, len)];

len = offset + len;
while(offset < len) {
ret[idx++] = data[offset++];
}
return ret;
}
}
}
Loading

0 comments on commit 4b17e28

Please sign in to comment.