public FieldDeclaration AddField(string name)
 {
     FieldDeclaration fd = new FieldDeclaration(
         Conformer.ToCapitalized(name),
         this,
         new TypeTypeDeclaration(typeof(int))
         );
     fd.Attributes = MemberAttributes.Public;
     this.fields.Add(fd);
     return fd;
 }
 public PropertyDeclaration AddProperty(
     FieldDeclaration f,
     bool hasGet,
     bool hasSet,
     bool checkNonNull
     )
 {
     return this.AddProperty(f, f.Name, hasGet, hasSet, checkNonNull);
 }
        public PropertyDeclaration AddProperty(
            FieldDeclaration f,
            string name,
            bool hasGet,
            bool hasSet,
            bool checkNonNull
            )
        {
            PropertyDeclaration p = this.AddProperty(
                f.Type,
                name);
            if (hasGet)
            {
                p.Get.Return(Expr.This.Field(f));
            }
            if (hasSet)
            {
                if (checkNonNull)
                {
                    ConditionStatement ifnull = Stm.If(Expr.Value.Identity(Expr.Null));
                    p.Set.Add(ifnull);
                    ifnull.TrueStatements.Add(
                        Stm.Throw(typeof(ArgumentNullException))
                        );
                    p.SetExceptions.Add(new ThrowedExceptionDeclaration(
                        typeof(ArgumentNullException),
                        "value is a null reference"
                        ));
                }
                p.Set.Add(
                    Stm.Assign(
                    Expr.This.Field(f),
                    Expr.Value
                    )
                    );
            }

            return p;
        }
        public FieldDeclaration AddField(ITypeDeclaration type, string name)
        {
            if (type==null)
                throw new ArgumentNullException("type");
            if (name==null)
                throw new ArgumentNullException("name");
            if (this.fields.Contains(name))
                throw new ArgumentException("field already existing in class");

            FieldDeclaration f = new FieldDeclaration(this.Conformer.ToCamel(name),this,type);
            this.fields.Add(f);
            return f;
        }
 /// <summary>
 /// Determines whether this StringFieldDeclarationDictionary contains a specific value.
 /// </summary>
 /// <param name="value">
 /// The FieldDeclaration value to locate in this StringFieldDeclarationDictionary.
 /// </param>
 /// <returns>
 /// true if this StringFieldDeclarationDictionary contains an element with the specified value;
 /// otherwise, false.
 /// </returns>
 public virtual bool ContainsValue(FieldDeclaration value)
 {
     return ContainsKey(value.Name);
 }
 /// <summary>
 /// Adds an element with the specified key and value to this StringFieldDeclarationDictionary.
 /// </summary>
 /// <param name="key">
 /// The String key of the element to add.
 /// </param>
 /// <param name="value">
 /// The FieldDeclaration value of the element to add.
 /// </param>
 public virtual void Add(FieldDeclaration value)
 {
     this.Dictionary.Add(value.Name, value);
 }
Exemple #7
0
        private void AddEnumerator(ClassDeclaration c, FieldDeclaration data, MethodDeclaration close)
        {
            c.Interfaces.Add(typeof(IEnumerable));
            // create subclass
            ClassDeclaration en = c.AddClass("Enumerator");
            // add wrapped field
            FieldDeclaration wrapped = en.AddField(
                c,"wrapped"
                );

            ITypeDeclaration enumeratorType = new TypeTypeDeclaration(typeof(IEnumerator));
            ITypeDeclaration disposableType = new TypeTypeDeclaration(typeof(IDisposable));

            // add IEnumerator
            en.Interfaces.Add(enumeratorType);
            en.Interfaces.Add(disposableType);

            // add constructor
            ConstructorDeclaration cs = en.AddConstructor();
            ParameterDeclaration collection = cs.Signature.Parameters.Add(c,"collection",true);
            cs.Body.AddAssign(Expr.This.Field(wrapped),Expr.Arg(collection));

            // add current
            PropertyDeclaration current = en.AddProperty(data.Type,"Current");
            current.Get.Return(
                Expr.This.Field(wrapped).Prop("Data")
                );

            // add explicit interface implementation
            PropertyDeclaration currentEn = en.AddProperty(typeof(Object),"Current");
            currentEn.Get.Return(Expr.This.Prop(current));
            currentEn.PrivateImplementationType = enumeratorType;

            // add reset
            MethodDeclaration reset = en.AddMethod("Reset");
            reset.ImplementationTypes.Add( wrapped.Type );
            reset.Body.Add( Stm.Throw(typeof(InvalidOperationException),Expr.Prim("Not supported")));

            // add movenext
            MethodDeclaration movenext = en.AddMethod("MoveNext");
            movenext.ImplementationTypes.Add(wrapped.Type );
            movenext.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));
            movenext.Body.Return( Expr.This.Field(wrapped).Method("Read").Invoke());

            // add dispose
            MethodDeclaration disposeEn  = en.AddMethod("Dispose");
            disposeEn.ImplementationTypes.Add( disposableType );
            disposeEn.Body.Add(
                Expr.This.Field(wrapped).Method(close).Invoke()
                );
            disposeEn.Body.AddAssign( Expr.This.Field(wrapped),Expr.Null );

            // add get enuemrator
            MethodDeclaration geten = c.AddMethod("GetEnumerator");
            geten.Signature.ReturnType = en;
            geten.Body.Return(Expr.New(en,Expr.This));

            MethodDeclaration igeten = c.AddMethod("GetEnumerator");
            igeten.PrivateImplementationType = new TypeTypeDeclaration(typeof(IEnumerable));
            igeten.Signature.ReturnType = new TypeTypeDeclaration(typeof(IEnumerator));
            igeten.Body.Return(Expr.This.Method("GetEnumerator").Invoke());
        }