为自定义C ++类的Visual Studio实现自定义向导 [英] Implementing a custom wizard for Visual Studio for custom C++ classes

查看:138
本文介绍了为自定义C ++类的Visual Studio实现自定义向导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使新的C ++类符合某些相当挑剔的编码约定(前期:我无法讨论这些...),我正在考虑一种为新的C ++类生成存根的方法.当前,每个人都在进行复制粘贴,经常丢失一些细节.使用的IDE是MS Visual Studio 2005,但我认为在2008年和2010年这些主题没有太大变化.

in order to make new C++ classes conform to some rather picky coding conventions (upfront: I am not in the position to discuss these...), I was thinking about a way of generating stubs for new C++ classes. Currently, everyone is doing copy-paste, regularly missing some detail. The IDE in use is MS Visual Studio 2005, but I think there has not been much of a change in 2008 and 2010 regarding these topics.

我的第一个想法是实现一个命令行脚本来做到这一点,这很简单.另外,我考虑过使用默认的VS扩展机制来实现更好的IDE集成.因此,在过滤器(解决方案资源管理器)上选择 Add-> New Item ... 时,这将钩住一些自定义内容.

My first idea was to implement a command line script to do this, which would be fairly straight forward to do. Alternatively, I thought about using a default VS extension mechanism for better IDE integration. So, this would be hooking in some custom stuff when selecting Add->New Item... on a filter (Solution Explorer).

经过一番调查,我发现有一个易于使用的模板机制,不幸的是,该机制不适用于C ++(

After some investigation, I found out there is an easy-to-use templating mechanism, which unfortunately does not work for C++ (http://msdn.microsoft.com/en-us/library/6db0hwky%28v=vs.80%29.aspx). For C++, it seems like you have to implement a custom wizard instead, incorporating html for the layout and javascript for the logic.

关于自定义向导方法,我得出的结论是,这需要一定的努力(至少对我而言)才能完成. MSDN在此主题上不是很详细.我在网上发现了一些演练,它们仅处理项目的自定义向导( Add-> New Project ... 而不是 Add-> New Item ... em>).

Regarding the custom wizard approach, I've come to the conclusion that this would require some effort (at least for me) to get this done. MSDN is not very detailed on this topic. I've found some walkthroughs in the web, which are dealing with custom wizards for projects only (Add->New Project... instead of Add->New Item...).

因此,这是qn:有人这样做或类似吗?它(更好的IDE集成)值得(为自定义向导实现提供详细信息)值得付出的努力,还是您建议使用命令行工具呢?我想,在当前项目中超过两天的工作无法获得回报.

So, here's the qn: Anyone having done this or something similar? Is it (better IDE integration) worth the effort (coping with the details of custom wizard implementation), or would you suggest the go for the command line tool instead? More than two days of work would not pay off in the current project, I guess.

如果您认为自定义向导很棒,也许您可​​以给出一些入门提示.另外,也许还有其他我没有想到的选择. VS Add-Ins似乎在此方面居于首位,并进行了调整,例如 VC \ vcprojectitems \ newc ++ file.cpp 不会执行此工作.

If you think custom wizards are great, maybe you can give some hints to get started. Also, maybe there are alternatives I did not come up with. VS Add-Ins seemed to be over the top for this, and adapting e.g. VC\vcprojectitems\newc++file.cpp will not do the job.

在此先感谢您和最诚挚的问候... 杰布

Thanks in advance and best regards... Jerb

推荐答案

我已经使用自定义向导完成了类似的操作.

I have done something similar using a custom wizard.

出于我的目的,我只想向每个类中注入一些简单的宏,以将其插入静态工厂对象中.

For my purposes I just wanted to inject some simple macros into each class to insert it into a static factory object.

我不需要修改实际的向导,只需复制默认的通用类向导并修改代码生成javascript.

I didn't need to modify the actual wizard, just copied the default generic class wizard and modified the code generation javascript.

文档是关于该主题的垃圾,但是这里是开始使用javascript的好地方(这是您将获得最大功能的地方):

The documentation is rubbish on the topic, but here is a good place to start for the javascript (that is where you are going to get the most power):

http://msdn.microsoft.com/en-us/library/t41260xs(v = VS.71).aspx

默认的类向导javascript位于此处:

The default class wizard javascript is located here:

C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ VCWizards \ CodeWiz \ Generic \ Class \ Scripts \ 1033 \ default.js

C:\Program Files\Microsoft Visual Studio 10.0\VC\VCWizards\CodeWiz\Generic\Class\Scripts\1033\default.js


复制项目

复制此文件比我想象的要难,您需要复制并粘贴


Duping the Project

Duplicating this is trickier than I thought, you need to copy and paste the

Microsoft Visual Studio 10.0 \ VC \ VCWizards \ CodeWiz \ Generic \

Microsoft Visual Studio 10.0\VC\VCWizards\CodeWiz\Generic\

文件夹,然后转到:

C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ VCAddClass \ Generic

C:\Program Files\Microsoft Visual Studio 10.0\VC\VCAddClass\Generic

复制该文件夹并修改其中的Generic.vsdir以指向.. \ Simple.vsz(VCAddClass文件夹中的文件)的复制.

Dupe this folder and modify the Generic.vsdir inside it to point to a dupe of ..\Simple.vsz (a file in the VCAddClass folder).

Simple.vsz文件指向您一开始所复制的VCWizards中"Generic"文件夹的位置,因此将新的simple.vsz指向该位置.

The Simple.vsz file points back to the location of the "Generic" folder in VCWizards you duped at the start, so point your new simple.vsz at that.

对于实际的代码生成,它并不是那么困难.为了更快地开始工作,这是default.js的一般工作方式:

As for the actual code generation, its not all that difficult to pick up. To get things started faster, here is the General way the default.js works:

向导完成后,代码生成方法将从以下位置启动:

Once the wizard is finished, the code gen method kicks off from:

function OnFinish(selProj, selObj)

selProj(据我所知)是EnvDTE.Project的实例

selProj is (as far as I can tell) an instance of EnvDTE.Project

http://msdn.microsoft.com/en-us/library/envdte.project .aspx

从向导中获取信息似乎基于:

Getting information from the wizard seems to be based around:

wizard.FindSymbol("CLASS_NAME")

真正的魔力开始在selProj.CodeModel对象上发生

The real magic starts to happen on the selProj.CodeModel object

oCM.AddClass(strClassName, strHeader, vsCMAddPositionEnd, "", "", vsCMAccessDefault);

似乎这些方法向文件中添加任何内容的约定只是简单地将文件修改为单个操作,因为它需要文件路径作为其参数.

It seems the convention for these methods to add anything to a file simply modifies the file as a single action, as it requires the file path as its paramter.

这将返回CodeClass实例,并且可以通过其方法添加到该实例,例如:

This returns a CodeClass instance and can be added to by its methods like:

AddAttribute
AddFunction
...

如果您正在寻找非常严格的代码格式(或者在我的情况下,插入不适合常规代码语法的宏,则这是一个很大的限制).

This is quite restricting if you are looking for very strict code formatting (or in my case inserting macros that don't fit normal code syntax.

解决此问题的简单方法是,只为需要使用EditPoint界面完全控制的部分自己构建一个字符串.

The simple way around this is to just build a string on your own for the parts that you need full control over using the EditPoint interface.

EditPoint是代码文件中的一个位置,您可以在其中调用以下方法:

An EditPoint is a location inside a code file to which you can call methods like:

EditPoint.Insert(string)
Editpoint.InsertFromFile(path)

要在要插入代码的位置获取一个EditPoint,只需使用代码生成文件中现有项之一(如类或构造函数)的位置,并使用.StartPointOf或.EndPointOf和操作参数.

To get an EditPoint in a location where you would want to insert code, simply use the location of one of the existing items in the code gen file (like class or constructor) and get a TextPoint using .StartPointOf or .EndPointOf and manipulating the parameters.

一旦有了TextPoint,就可以像下面这样创建一个EditPoint:

Once you have a TextPoint you can create an EditPoint like so:

newclass.EndPointOf(vsCMPartBody).CreateEditPoint().Insert("\nprivate:\n  REGISTER_TYPE_MEMBER("+strClassName+");\n");

要在.cpp文件中获取TextPoint,请执行以下操作:

To get a TextPoint inside the .cpp file instead:

oConstructor.StartPointOf(vsCMPartWhole,vsCMWhereDefinition).CreateEditPoint().Insert("REGISTER_TYPE_BODY_ID("+strClassName+",REPLACE_ID);\n\n\n");

这使您能够通过JScript字符串操作执行所需的任何操作,只要您可以通过向导找到所需的输入数据(我尚未深入研究)

This gives you the power to do anything you want via JScript string manipulation as long as you can find the input data you need via the wizard (which I have not yet delved into)

这篇关于为自定义C ++类的Visual Studio实现自定义向导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆