Ejemplo n.º 1
0
 public void HashFindTree()
 {
     HashDelegate = NDomain.Random().UnsafeFunc <string, int>(BTFTemplate.GetHashBTFScript(ScriptDict) + "return default;");
     foreach (var item in Dict)
     {
         Assert.Equal(item.Value, HashDelegate(item.Key));
     }
 }
Ejemplo n.º 2
0
 public override string ScriptValueAction(IDictionary <TValue, string> dict)
 {
     return(BTFTemplate.GetHashBTFScript(dict));
 }
Ejemplo n.º 3
0
 public override string ScriptKeyAction(IDictionary <TKey, string> dict, string paramName)
 {
     return(BTFTemplate.GetHashBTFScript(dict, paramName));
 }
Ejemplo n.º 4
0
        public static Type InitType(Type type, FindTreeType kind = FindTreeType.Hash)
        {
            bool isStatic = (type.IsSealed && type.IsAbstract);
            Type callType = typeof(DictBase);


            StringBuilder body  = new StringBuilder();
            var           cache = NBuildInfo.GetInfos(type);

            var setByObjectCache     = new Dictionary <string, string>();
            var getByObjectCache     = new Dictionary <string, string>();
            var getByStrongTypeCache = new Dictionary <string, string>();

            foreach (var item in cache)
            {
                var    info   = item.Value;
                string caller = "Instance";
                if (info != null)
                {
                    if (info.IsStatic)
                    {
                        caller = type.GetDevelopName();
                    }


                    if (info.CanWrite)
                    {
                        setByObjectCache[info.MemberName] = $"{caller}.{info.MemberName} = ({info.MemberTypeName})value;";
                    }

                    if (info.CanRead)
                    {
                        getByObjectCache[info.MemberName]     = $"return {caller}.{info.MemberName};";
                        getByStrongTypeCache[info.MemberName] = $"return (T)(object)({caller}.{info.MemberName});";
                    }
                }
            }

            string setObjectBody     = default;
            string getObjectBody     = default;
            string getStrongTypeBody = default;

            switch (kind)
            {
            case FindTreeType.Fuzzy:
                setObjectBody     = BTFTemplate.GetFuzzyPointBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetFuzzyPointBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetFuzzyPointBTFScript(getByStrongTypeCache, "name");
                break;

            case FindTreeType.Hash:
                setObjectBody     = BTFTemplate.GetHashBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetHashBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetHashBTFScript(getByStrongTypeCache, "name");
                break;

            case FindTreeType.Precision:
                setObjectBody     = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetGroupPrecisionPointBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByStrongTypeCache, "name");
                break;

            default:
                break;
            }


            body.AppendLine("public unsafe override void Set(string name,object value){");
            body.AppendLine(setObjectBody);
            body.Append('}');


            body.AppendLine("public unsafe override T Get<T>(string name){");
            body.AppendLine(getStrongTypeBody);
            body.Append("return default;}");


            body.AppendLine("public unsafe override object GetObject(string name){");
            body.AppendLine(getObjectBody);
            body.Append("return default;}");


            if (!isStatic)
            {
                callType = typeof(DictBase <>).With(type);
                body.Append($@"public override void New(){{ Instance = new {type.GetDevelopName()}();}}");
            }

            Type tempClass = NClass.UseDomain(type.GetDomain())
                             .Public()
                             .Using(type)
                             .Using("System")
                             .Using("NCaller")
                             .UseRandomName()
                             .Namespace("NCallerDynamic")
                             .Inheritance(callType)
                             .Body(body.ToString())
                             .GetType();


            return(tempClass);
        }
Ejemplo n.º 5
0
        public static Type InitType(Type type, AlgorithmKind kind = AlgorithmKind.Hash)
        {
            var isStatic = type.IsSealed && type.IsAbstract;
            var callType = typeof(DictBase);

            var body                       = new StringBuilder();
            var setByObjectCache           = new Dictionary <string, string>();
            var getByObjectCache           = new Dictionary <string, string>();
            var getByStrongTypeCache       = new Dictionary <string, string>();
            var getByLeoMembersCache       = new Dictionary <string, LeoMember>();
            var getByLeoMembersScriptCache = new Dictionary <string, string>();

            var getByReadOnlyStaticScriptBuilder  = new StringBuilder();
            var getByReadOnlySettingScriptBuilder = new StringBuilder();
            var getByInternalNamesScriptBuilder   = new StringBuilder();

            #region Field

            var fields = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (var field in fields)
            {
                if (field.IsSpecialName || field.Name.Contains("<"))
                {
                    continue;
                }

                var caller = "Instance";

                if (field.IsStatic)
                {
                    caller = type.GetDevelopName();
                }

                var fieldName = field.Name;
                var fieldType = field.FieldType.GetDevelopName();

                //set
                if (!field.IsLiteral)
                {
                    var fieldScript = $"{caller}.{fieldName}";

                    if (field.IsInitOnly)
                    {
                        fieldScript = fieldScript.ReadonlyScript();
                    }

                    setByObjectCache[fieldName] = $"{fieldScript} = ({fieldType})value;";
                }


                //get
                getByObjectCache[fieldName]     = $"return {caller}.{fieldName};";
                getByStrongTypeCache[fieldName] = $"return (T)(object)({caller}.{fieldName});";

                //member metadata
                getByLeoMembersCache[fieldName]       = field;
                getByLeoMembersScriptCache[fieldName] = $"return __metadata_LeoMember_{fieldName};";
                getByReadOnlyStaticScriptBuilder.AppendLine($@"private static readonly LeoMember __metadata_LeoMember_{fieldName};");
                getByInternalNamesScriptBuilder.Append($@"""{fieldName}"",");
                getByReadOnlySettingScriptBuilder.Append($"__metadata_LeoMember_{fieldName}".ReadonlyScript());
                getByReadOnlySettingScriptBuilder.Append($@" = leoMembersCache[""{fieldName}""];");
            }

            #endregion

            #region Property

            var props = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (var property in props)
            {
                var method = property.CanRead ? property.GetGetMethod(true) : property.GetSetMethod(true);

                var caller = "Instance";

                if (method.IsStatic)
                {
                    caller = type.GetDevelopName();
                }

                var propertyName   = property.Name;
                var propertyType   = property.PropertyType.GetDevelopName();
                var propertyScript = $"{caller}.{propertyName}";

                //set
                if (property.CanWrite)
                {
                    setByObjectCache[propertyName] = $"{propertyScript} = ({propertyType})value;";
                }


                //get
                if (property.CanRead)
                {
                    getByObjectCache[propertyName]     = $"return {caller}.{propertyName};";
                    getByStrongTypeCache[propertyName] = $"return (T)(object)({caller}.{propertyName});";
                }

                //member metadata
                getByLeoMembersCache[propertyName]       = property;
                getByLeoMembersScriptCache[propertyName] = $"return __metadata_LeoMember_{propertyName};";
                getByReadOnlyStaticScriptBuilder.AppendLine($@"private static readonly LeoMember __metadata_LeoMember_{propertyName};");
                getByInternalNamesScriptBuilder.Append($@"""{propertyName}"",");
                getByReadOnlySettingScriptBuilder.Append($"__metadata_LeoMember_{propertyName}".ReadonlyScript());
                getByReadOnlySettingScriptBuilder.Append($@" = leoMembersCache[""{propertyName}""];");
            }

            #endregion

            string setObjectBody     = default;
            string getObjectBody     = default;
            string getStrongTypeBody = default;
            string getLeoMemberBody  = default;

            switch (kind)
            {
            case AlgorithmKind.Fuzzy:
                setObjectBody     = BTFTemplate.GetGroupFuzzyPointBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetGroupFuzzyPointBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetGroupFuzzyPointBTFScript(getByStrongTypeCache, "name");
                getLeoMemberBody  = BTFTemplate.GetGroupFuzzyPointBTFScript(getByLeoMembersScriptCache, "name");
                break;

            case AlgorithmKind.Hash:
                setObjectBody     = BTFTemplate.GetHashBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetHashBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetHashBTFScript(getByStrongTypeCache, "name");
                getLeoMemberBody  = BTFTemplate.GetHashBTFScript(getByLeoMembersScriptCache, "name");
                break;

            case AlgorithmKind.Precision:
                setObjectBody     = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "name");
                getObjectBody     = BTFTemplate.GetGroupPrecisionPointBTFScript(getByObjectCache, "name");
                getStrongTypeBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByStrongTypeCache, "name");
                getLeoMemberBody  = BTFTemplate.GetGroupPrecisionPointBTFScript(getByLeoMembersScriptCache, "name");
                break;
            }


            //To add readonly metadata (LeoMember) properties.
            body.AppendLine(getByReadOnlyStaticScriptBuilder.ToString());


            body.AppendLine("public unsafe override void Set(string name,object value){");
            body.AppendLine(setObjectBody);
            body.Append('}');

#if NET5_0
            body.AppendLine("[SkipLocalsInit]");
#endif
            body.AppendLine("public unsafe override T Get<T>(string name){");
            body.AppendLine(getStrongTypeBody);
            body.Append("return default;}");

#if NET5_0
            body.AppendLine("[SkipLocalsInit]");
#endif
            body.AppendLine("public unsafe override object GetObject(string name){");
            body.AppendLine(getObjectBody);
            body.Append("return default;}");


            body.AppendLine("public unsafe override LeoMember GetMember(string name){");
            body.AppendLine(getLeoMemberBody);
            body.Append("return default;}");


            body.AppendLine("protected override HashSet<string> InternalMemberNames { get; } = new HashSet<string>(){");
            body.AppendLine(getByInternalNamesScriptBuilder.ToString());
            body.Append("};");


            body.AppendLine("public static void InitMetadataMapping(Dictionary<string, LeoMember> leoMembersCache){");
            body.AppendLine(getByReadOnlySettingScriptBuilder.ToString());
            body.Append('}');


            if (!isStatic)
            {
                callType = typeof(DictBase <>).With(type);
                body.Append($@"public override void New(){{ Instance = new {type.GetDevelopName()}();}}");
            }
            else
            {
                body.Append($@"public override void SetObjInstance(object obj){{ }}");
            }


            var tempClass = NClass.UseDomain(type.GetDomain())
                            .Public()
                            .Using(type)
                            .AllowPrivate(type.Assembly)
                            .Namespace("NMS.Leo.NCallerDynamic")
                            .Inheritance(callType)
                            .Body(body.ToString())
                            .GetType();

            InitMetadataMappingCaller(tempClass)(getByLeoMembersCache);

            return(tempClass);
        }
Ejemplo n.º 6
0
 public static HDC <P, T, S> operator |(IDictionary <T, String> dict, HDC <P, T, S> template)
 {
     template.FindContent = BTFTemplate.GetHashBTFScript(dict);
     return(template);
 }