Example #1
0
        static CSProperty PublicGetPubPrivSetBacking(CSType type, string name, bool declareField, bool setIsPublic, string backingFieldName = null)
        {
            if (!declareField && backingFieldName == null)
            {
                throw new ArgumentException("declareField must be true if there is no supplied field name", nameof(declareField));
            }
            backingFieldName = backingFieldName ?? MassageName(Exceptions.ThrowOnNull(name, nameof(name)));


            CSIdentifier backingIdent = new CSIdentifier(backingFieldName);
            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (new ICodeElement [] { CSReturn.ReturnLine(backingIdent) }, false, true);
            LineCodeElementCollection <ICodeElement> setCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSAssignment.Assign(backingFieldName, new CSIdentifier("value"))
            }, false, true);
            CSProperty prop = new CSProperty(type, CSMethodKind.None, new CSIdentifier(name), CSVisibility.Public,
                                             new CSCodeBlock(getCode),
                                             (setIsPublic ? CSVisibility.Public : CSVisibility.Private), new CSCodeBlock(setCode));

            if (declareField)
            {
                prop.Insert(0, CSFieldDeclaration.FieldLine(type, backingFieldName));
            }
            return(prop);
        }
        public CSMethod(CSVisibility vis, CSMethodKind kind, CSType type, CSIdentifier name,
                        CSParameterList parms, CSBaseExpression[] baseOrThisCallParms, bool callsBase, CSCodeBlock body, bool isSealed = false)
        {
            GenericParameters  = new CSGenericTypeDeclarationCollection();
            GenericConstraints = new CSGenericConstraintCollection();
            Visibility         = vis;
            Kind       = kind;
            Type       = type;       // no throw on null - could be constructor
            Name       = Exceptions.ThrowOnNull(name, nameof(name));
            Parameters = Exceptions.ThrowOnNull(parms, nameof(parms));
            CallsBase  = callsBase;
            BaseOrThisCallParameters = baseOrThisCallParms;

            Body     = body;         // can be null
            IsSealed = isSealed;

            LineCodeElementCollection <ICodeElement> lc = new LineCodeElementCollection <ICodeElement> (new ICodeElement [0], false, true);

            if (vis != CSVisibility.None)
            {
                lc.And(new SimpleElememt(VisibilityToString(vis))).And(SimpleElememt.Spacer);
            }

            if (isSealed)
            {
                lc.And(new SimpleElememt("sealed")).And(SimpleElememt.Spacer);
            }

            lc.And(new SimpleElememt(MethodKindToString(kind))).And(SimpleElememt.Spacer);

            if (type != null)
            {
                lc.And(type).And(SimpleElememt.Spacer);
            }

            lc.And(name).And(GenericParameters).And(new SimpleElememt("(")).And(parms).And(new SimpleElememt(")")).And(GenericConstraints);
            if (body == null)
            {
                if (!(kind == CSMethodKind.StaticExtern || kind == CSMethodKind.Interface))
                {
                    throw new ArgumentException("Method body is only optional when method kind kind is either StaticExtern or Interface",
                                                nameof(body));
                }
                lc.Add(new SimpleElememt(";"));
            }
            Add(lc);
            if (BaseOrThisCallParameters != null)
            {
                Add(new CSFunctionCall(CallsBase ? ": base" : ": this", false, BaseOrThisCallParameters));
            }
            if (body != null)
            {
                Add(body);
            }
        }
Example #3
0
        static LineCodeElementCollection <ICodeElement> MakeEtter(CSVisibility vis, string getset,
                                                                  bool unifiedVis, bool moreRestrictiveVis)
        {
            LineCodeElementCollection <ICodeElement> getLine = new LineCodeElementCollection <ICodeElement> (null, false, true);

            if (!unifiedVis && vis != CSVisibility.None && moreRestrictiveVis)
            {
                getLine.And(new SimpleElememt(CSMethod.VisibilityToString(vis))).And(SimpleElememt.Spacer);
            }
            return(getLine.And(new SimpleElememt(getset, false)));
        }
Example #4
0
        public void NSObjectSubclassableMethodTest3()
        {
            string swiftCode =
                "import Foundation\n" +
                "@objc\n" +
                "open class Subclassable3 : NSObject {\n" +
                "   public override init () { }\n" +
                "   open var returnsTrue:Bool {\n" +
                "       get { return true\n } " +
                "   }\n" +
                "}\n" +
                "public func callIt (a: Subclassable3) -> Bool {\n" +
                "    return a.returnsTrue\n" +
                "}\n";


            var theSub = new CSClass(CSVisibility.Public, "TheSub3");
            var ctor   = new CSMethod(CSVisibility.Public, CSMethodKind.None, null, theSub.Name,
                                      new CSParameterList(), new CSBaseExpression [0], true, new CSCodeBlock());

            theSub.Constructors.Add(ctor);
            theSub.Inheritance.Add(new CSIdentifier("Subclassable3"));

            var theBody = new CSCodeBlock();

            theBody.Add(CSReturn.ReturnLine(CSConstant.Val(false)));

            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(CSConstant.Val(false))
            }, false, true);
            CSProperty returnsFalse = new CSProperty(CSSimpleType.Bool, CSMethodKind.Override, new CSIdentifier("ReturnsTrue"),
                                                     CSVisibility.Public, new CSCodeBlock(getCode),
                                                     CSVisibility.Public, null);

            theSub.Properties.Add(returnsFalse);


            var callingCode = new CodeElementCollection <ICodeElement> ();
            var objID       = new CSIdentifier("subTest");
            var objDecl     = CSVariableDeclaration.VarLine(CSSimpleType.Var, objID, new CSFunctionCall("TheSub3", true));
            var call        = CSFunctionCall.ConsoleWriteLine(objID.Dot((CSIdentifier)"ReturnsTrue"));
            var call2       = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.CallIt", objID));

            callingCode.Add(objDecl);
            callingCode.Add(call);
            callingCode.Add(call2);

            TestRunning.TestAndExecute(swiftCode, callingCode, "False\nFalse\n", otherClass: theSub, platform: PlatformName.macOS);
        }
Example #5
0
        public static CSProperty PublicGetBacking(CSType type, CSIdentifier name, CSIdentifier backingFieldName,
                                                  bool includeBackingFieldDeclaration = false, CSMethodKind methodKind = CSMethodKind.None)
        {
            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(Exceptions.ThrowOnNull(backingFieldName, nameof(backingFieldName)))
            }, false, true);
            CSProperty prop = new CSProperty(type, methodKind, Exceptions.ThrowOnNull(name, nameof(name)),
                                             CSVisibility.Public, new CSCodeBlock(getCode),
                                             CSVisibility.Public, null);

            if (includeBackingFieldDeclaration)
            {
                prop.Insert(0, CSFieldDeclaration.FieldLine(type, backingFieldName));
            }
            return(prop);
        }
Example #6
0
        CSProperty(CSType type, CSMethodKind kind, CSIdentifier name,
                   CSVisibility getVis, CSCodeBlock getter,
                   CSVisibility setVis, CSCodeBlock setter, CSParameterList parms)
        {
            bool unifiedVis = getVis == setVis;

            IndexerParameters = parms;

            LineCodeElementCollection <ICodeElement> decl = new LineCodeElementCollection <ICodeElement> (null, false, true);

            GetterVisibility = getVis;
            SetterVisibility = setVis;
            CSVisibility bestVis = (CSVisibility)Math.Min((int)getVis, (int)setVis);

            decl.And(new SimpleElememt(CSMethod.VisibilityToString(bestVis))).And(SimpleElememt.Spacer);
            if (kind != CSMethodKind.None)
            {
                decl.And(new SimpleElememt(CSMethod.MethodKindToString(kind))).And(SimpleElememt.Spacer);
            }

            PropType = type;
            Name     = name;

            decl.And(Exceptions.ThrowOnNull(type, "type")).And(SimpleElememt.Spacer)
            .And(Exceptions.ThrowOnNull(name, nameof(name)));
            if (parms != null)
            {
                decl.And(new SimpleElememt("[", true)).And(parms).And(new SimpleElememt("]"));
            }
            Add(decl);


            CSCodeBlock cb = new CSCodeBlock(null);

            if (getter != null)
            {
                Getter = getter;
                LineCodeElementCollection <ICodeElement> getLine = MakeEtter(getVis, "get", unifiedVis, getVis > setVis);
                cb.Add(getLine);
                if (getter.Count() == 0)
                {
                    getLine.Add(new SimpleElememt(";"));
                }
                else
                {
                    cb.Add(getter);
                }
            }
            if (setter != null)
            {
                Setter = setter;
                LineCodeElementCollection <ICodeElement> setLine = MakeEtter(setVis, "set", unifiedVis, setVis > getVis);
                cb.Add(setLine);
                if (setter.Count() == 0)
                {
                    setLine.Add(new SimpleElememt(";"));
                }
                else
                {
                    cb.Add(setter);
                }
            }

            Add(cb);
        }