Пример #1
0
        public bool CreateSynchClass()
        {
            CodeClass synchClass = _cc.AddClass("Synch" + _cc.Name, -1, 0,
                                                0, EnvDTE.vsCMAccess.vsCMAccessPrivate);

            synchClass.AddBase(_cc, -1);

            //member variables
            synchClass.AddVariable("_root", "System.Object", -1, EnvDTE.vsCMAccess.vsCMAccessPrivate, null);
            synchClass.AddVariable("_parent", _cc.FullName, -1, EnvDTE.vsCMAccess.vsCMAccessPrivate, null);

            //constructor - add function can't handle this at the moment
            EditPoint classEndPt = synchClass.EndPoint.CreateEditPoint();

            classEndPt.StartOfLine();
            classEndPt.Insert("\n");

            EditPoint editPt = synchClass.StartPoint.CreateEditPoint();

            editPt.LineDown(3);
            editPt.EndOfLine();
            editPt.Insert("\ninternal " + synchClass.Name + "(" + _cc.Name +
                          " parent){\n_parent = parent;\n_root = parent.SyncRoot;\n}\n");
            editPt.MoveToPoint(synchClass.StartPoint);
            editPt.SmartFormat(synchClass.EndPoint);

            //functions, properties and indexers
            for (CodeType ct = (CodeType)_cc; ct != null;)
            {
                if (!AddMemberElementsFromType(ct, synchClass))
                {
                    return(false);
                }
                if (ct.Bases.Count != 0)
                {
                    ct = (CodeType)(ct.Bases.Item(1));
                    if (ct.Name == "Object")
                    {
                        break;
                    }
                }
            }

            synchClass.StartPoint.CreateEditPoint().SmartFormat(synchClass.EndPoint);

            return(true);
        }
Пример #2
0
        protected bool AddSynchWrapperMember(CodeClass synch, CodeFunction cf)
        {
            if (cf != null && (cf.FunctionKind & FunctionsThatCantBeAnnotatedAsVirtual) == 0 &&
                cf.CanOverride == true && cf.IsShared == false)
            {
                //add prototype and parameters
                CodeFunction synchFunction = synch.AddFunction(cf.Name, cf.FunctionKind, cf.Type, -1, cf.Access, null);
                foreach (CodeParameter param in cf.Parameters)
                {
                    synchFunction.AddParameter(param.Name, param.Type, -1);
                }
                synchFunction.CanOverride = true;
                EditPoint  replaceVirtual = synchFunction.StartPoint.CreateEditPoint();
                TextRanges tr             = null;
                replaceVirtual.ReplacePattern(synchFunction.EndPoint, "virtual", "override",
                                              (int)EnvDTE.vsFindOptions.vsFindOptionsMatchWholeWord, ref tr);

                //remove default return
                EditPoint editPt = synchFunction.EndPoint.CreateEditPoint();
                editPt.LineUp(1);
                editPt.StartOfLine();
                string returnType = cf.Type.AsString;
                if (returnType != "void")
                {
                    EditPoint startOfLastLine = synchFunction.EndPoint.CreateEditPoint();
                    startOfLastLine.LineUp(1);
                    startOfLastLine.EndOfLine();
                    editPt.Delete(startOfLastLine);
                }

                //generate method body
                System.Text.StringBuilder methodBody = new System.Text.StringBuilder(100);
                if (returnType != "void")
                {
                    methodBody.Append(cf.Type.AsString + " ret;\n");
                }
                methodBody.Append(
                    "System.Threading.Monitor.Enter(_root);" +
                    "\ntry{");
                if (returnType != "void")
                {
                    methodBody.Append("\nret = _parent." + cf.Name + "(");
                }
                else
                {
                    methodBody.Append("\n_parent." + cf.Name + "(");
                }
                bool first = true;
                foreach (CodeParameter p in cf.Parameters)
                {
                    if (!first)
                    {
                        methodBody.Append(", ");
                    }
                    first = false;
                    int typeSpaceLocation = p.Type.AsString.IndexOf(' ');
                    if (typeSpaceLocation != -1)                    //append out or ref to parameter
                    {
                        methodBody.Append(p.Type.AsString.Substring(0, typeSpaceLocation + 1));
                    }
                    methodBody.Append(p.Name);
                }
                methodBody.Append(");");
                methodBody.Append(
                    "\n}" +
                    "\nfinally{System.Threading.Monitor.Exit(_root);}");
                if (returnType != "void")
                {
                    methodBody.Append("\nreturn ret;");
                }

                //add new body to method
                editPt.Insert(methodBody.ToString());
                editPt.MoveToPoint(synchFunction.StartPoint);
                editPt.SmartFormat(synchFunction.EndPoint);
            }
            return(true);
        }