protected override void AddPropertyField(DefField field)
        {
            string ptype = GetCLRTypeName(field);
            string pname = GetClassName() + "::" + (field.HasAttribute <RenameAttribute>() ? field.GetAttribute <RenameAttribute> ().Name : field.Name);

            if (field.IsNativeArray)
            {
                if (field.Type.HasAttribute <NativeValueContainerAttribute>() ||
                    (field.Type.IsValueType && !field.Type.HasWrapType(WrapTypes.NativePtrValueType)))
                {
                    DefParam tmpParam = new DefParam(field, field.Name + "_array");
                    switch (field.PassedByType)
                    {
                    case PassedByType.Value:
                        tmpParam.PassedByType = PassedByType.Pointer;
                        break;

                    case PassedByType.Pointer:
                        tmpParam.PassedByType = PassedByType.PointerPointer;
                        break;

                    default:
                        throw new Exception("Unexpected");
                    }

                    ptype = GetCLRTypeName(tmpParam);
                    string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), tmpParam);

                    _sb.AppendLine(ptype + " " + pname + "::get()");
                    _sb.AppendLine("{");
                    _sb.AppendLine("\treturn " + managedType + ";");
                    _sb.AppendLine("}");
                }
                else
                {
                    string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field) + "[index]", field);

                    _sb.AppendLine(ptype + " " + pname + "::get(int index)");
                    _sb.AppendLine("{");
                    _sb.AppendLine("\tif (index < 0 || index >= " + field.ArraySize + ") throw gcnew IndexOutOfRangeException();");
                    _sb.AppendLine("\treturn " + managedType + ";");
                    _sb.AppendLine("}");
                    _sb.AppendLine("void " + pname + "::set(int index, " + ptype + " value )");
                    _sb.AppendLine("{");
                    _sb.IncreaseIndent();
                    _sb.AppendLine("if (index < 0 || index >= " + field.ArraySize + ") throw gcnew IndexOutOfRangeException();");
                    string param = AddParameterConversion(new DefParam(field, "value"));
                    _sb.AppendLine(GetNativeInvokationTarget(field) + "[index] = " + param + ";");
                    _sb.DecreaseIndent();
                    _sb.AppendLine("}");
                }
            }
            else if (_cachedMembers.Contains(field))
            {
                string managedType;
                if (field.Type.IsSTLContainer)
                {
                    managedType = GetNativeInvokationTarget(field);
                }
                else
                {
                    managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), field);
                }
                string priv = NameToPrivate(field);

                _sb.AppendLine(ptype + " " + pname + "::get()");
                _sb.AppendLine("{");
                if (!field.IsStatic)
                {
                    _sb.AppendLine("\treturn ( CLR_NULL == " + priv + " ) ? (" + priv + " = " + managedType + ") : " + priv + ";");
                }
                else
                {
                    _sb.AppendLine("\treturn " + priv + ";");
                }
                _sb.AppendLine("}");
            }
            else
            {
                string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), field);

                _sb.AppendLine(ptype + " " + pname + "::get()");
                _sb.AppendLine("{");
                _sb.AppendLine("\treturn " + managedType + ";");
                _sb.AppendLine("}");

                if ( // SharedPtrs can be copied by value. Let all be copied by value just to be sure (field.PassedByType == PassedByType.Pointer || field.Type.IsValueType)
                    !IsReadOnly && !field.Type.HasAttribute <ReadOnlyForFieldsAttribute>() &&
                    !field.IsConst)
                {
                    _sb.AppendLine("void " + pname + "::set( " + ptype + " value )");
                    _sb.AppendLine("{");
                    _sb.IncreaseIndent();
                    string param = AddParameterConversion(new DefParam(field, "value"));
                    _sb.AppendLine(GetNativeInvokationTarget(field) + " = " + param + ";");
                    _sb.DecreaseIndent();
                    _sb.AppendLine("}");
                }
            }
        }
示例#2
0
        protected override void AddPropertyField(DefField field)
        {
            //TODO comments for fields
            //AddComments(field);

            string ptype;

            if (field.IsNativeArray)
            {
                if (field.Type.HasAttribute <NativeValueContainerAttribute>() ||
                    (field.Type.IsValueType && !field.Type.HasWrapType(WrapTypes.NativePtrValueType)))
                {
                    DefParam tmpParam = new DefParam(field, field.Name);
                    switch (field.PassedByType)
                    {
                    case PassedByType.Value:
                        tmpParam.PassedByType = PassedByType.Pointer;
                        break;

                    case PassedByType.Pointer:
                        tmpParam.PassedByType = PassedByType.PointerPointer;
                        break;

                    default:
                        throw new Exception("Unexpected");
                    }

                    ptype = tmpParam.CLRTypeName;
                    _sb.AppendIndent("");
                    if (field.IsStatic)
                    {
                        _sb.Append("static ");
                    }
                    _sb.AppendFormat("property {0} {1}\n", ptype, field.Name);
                    _sb.AppendLine("{");

                    _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":");
                    _sb.AppendLine("\t" + ptype + " get();");

                    _sb.AppendLine("}");
                }
                else
                {
                    ptype = field.CLRTypeName;
                    _sb.AppendIndent("");
                    if (field.IsStatic)
                    {
                        _sb.Append("static ");
                    }
                    _sb.AppendFormat("property {0} {1}[int]\n", ptype, field.Name);
                    _sb.AppendLine("{");

                    _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":");
                    _sb.AppendLine("\t" + ptype + " get(int index);");
                    _sb.AppendLine("\tvoid set(int index, " + ptype + " value);");

                    _sb.AppendLine("}");
                }
            }
            else if (_cachedMembers.Contains(field))
            {
                ptype = field.CLRTypeName;
                _sb.AppendIndent("");
                if (field.IsStatic)
                {
                    _sb.Append("static ");
                }
                _sb.AppendFormat("property {0} {1}\n", ptype, field.Name);
                _sb.AppendLine("{");

                _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":");
                _sb.AppendLine("\t" + ptype + " get();");

                _sb.AppendLine("}");
            }
            else
            {
                ptype = GetCLRTypeName(field);
                _sb.AppendIndent("");
                if (field.IsStatic)
                {
                    _sb.Append("static ");
                }
                if (field.HasAttribute <RenameAttribute> ())
                {
                    _sb.AppendFormat("property {0} {1}\n", ptype, field.GetAttribute <RenameAttribute> ().Name);
                }
                else
                {
                    _sb.AppendFormat("property {0} {1}\n", ptype, field.Name);
                }
                _sb.AppendLine("{");

                _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":");
                _sb.AppendLine("\t" + ptype + " get();");

                if ( // SharedPtrs can be copied by value. Let all be copied by value just to be sure (field.PassedByType == PassedByType.Pointer || field.Type.IsValueType)
                    !IsReadOnly && !field.Type.HasAttribute <ReadOnlyForFieldsAttribute>() &&
                    !field.IsConst)
                {
                    _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":");
                    _sb.AppendLine("\tvoid set(" + ptype + " value);");
                }

                _sb.AppendLine("}");
            }
        }