javajava进制转换代码 转C++

移植Java代码到C++的心得 - Stephen_Liu - 博客园
深入浅出、事半功倍
posts - 180, comments - 685, trackbacks - 0, articles - 0
&&&&& 在实际的开发过程中,我们的项目或产品通常都会用到一些第三方开源组件或商业组件,以帮助开发人员实现一些特定的通用功能,如xml解析,正则表达式解析和数据压缩等。一般而言,只有在这两者均无法满足现有需求的情况下,我们才会考虑自行研发。因为适当的应用第三方组件可以保证开发人员能够将更多的精力投入到产品的核心算法和核心业务逻辑上,从而确保产品的开发周期和产品质量均能达到客户与公司满意的程度。&& && 相比于开源组件,很多商业组件都提供了更易于操作的用户界面,丰富的文档和示例代码,以及更灵活的接口和更多的功能。通常而言,一旦我们选定了某一商业组件,它所提供的功能就已经可以满足我们的需求了。然而对于开源组件却并非总是如此,我们偶尔会需要对其进行部分修改或对其接口进行二次封装,所幸的是,主体功能不会受到影响。众所周知,Java的开源社区极为活跃,提供了功能丰富的众多开源组件,相比于Java,C++的开源组件在数量上和功能丰富程度上都要相形见绌了。鉴于此,我们可以选择将已有的Java组件的部分或全部功能用C++重新实现,并形成自己的基础组件。和完全自行开发相比,这样的方式确实降低了一定的风险,但仍然是一条布满荆棘却又充满挑战的坎坷道路。然而在整个代码移植的过程中,却可以让开发者受益匪浅,下面列出主要几点:
&& && 1. 很多开源组件都是由编程经验丰富的开发者完成,其设计风格和实现技巧,以及对细节问题的处理方式都会给我们带来一定的帮助和启发。&& && 2. 纸上来得终觉浅,绝知此事要躬行。很多设计技巧只有在亲自编码或给自己写Test Case的时候才能真正体会出来。&& && 3. 可以更好的理解两种语言之间的差异,并充分挖掘他们的潜力,甄别各自的优缺点。这样在今后的开发中就可以更好的驾驭这两种语言了。&& && 4. 很多时候语言的差异确实影响了组件的设计,我们的目标是充分利用当前语言的特性来完成我们的设计与实现,而不是用一种通用的方式应对所有的语言,这样往往会给人一种橘生淮北的感觉。&& && 5. 在实现的过程中,可能因此而掌握一种算法或通用的业界标准。&& && 6. 如果有些算法或功能是自己擅长的,则可以利用这个机会将其嵌入到重新实现的组件中,这样不仅可以验证自己的想法,同时也可以提高新组件的性能和可扩展性。&& && 7. 有针对性的剔除部分冗余代码或功能,以提高新组件的效率和易用性。&& && 8. 相比于完全自行研发,由于已有的组件已经对很多边界问题进行了处理和分析,因此该类问题也会在新组件中得以解决,这样便可以极大的提高新组件的稳定程度,从而大大的缩短单元测试的周期。&& && 需要说明的是,这里列出的问题和解决方法仅仅为本人的实践经验,如您有更好的办法或遇到新的问题,本人诚挚的希望与您多多交流,共同提高。openoffice中一个将java代码转换为c++代码的例子
java代码如下:
//Listing 33 A
Code Example
// create new writer document and get text, then manipulate text
XComponent xWriterComponent = newDocComponent(&swriter&);
XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class,xWriterComponent);
// Access the text document&s multi service factory, which we will need for most of the
// following examples
mxDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface
(XMultiServiceFactory.class,xTextDocument);
XText xText = xTextDocument.getText();
// create a text cursor from the cells XText interface
XTextCursor xTextCursor = xText.createTextCursor();
// Get the property set of the cell&s TextCursor
XPropertySet xTextCursorProps = (XPropertySet)UnoRuntime.queryInterface
(XPropertySet.class,xTextCursor);
// Page Style name
String pageStyleName= xTextCursorProps.getPropertyValue(&PageStyleName&).toString();
// Get the StyleFamiliesSupplier interface of the document
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface
(XStyleFamiliesSupplier.class,xTextDocument);
// Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
// actual style families
XNameAccess xFamilies = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class,
xSupplier.getStyleFamilies());
// Access the &PageStyles& Family
XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface
(XNameContainer.class,xFamilies.getByName(&PageStyles&));
// Insert the newly created style into the PageStyles family
XStyle xStyle= (XStyle) UnoRuntime.queryInterface(XStyle.class,xFamily.getByName
(pageStyleName));
// Get the property set of the TextCursor
XPropertySet xStyleProps = (XPropertySet)UnoRuntime.queryInterface
(XPropertySet.class,xStyle);
xStyleProps.setPropertyValue(&LeftMargin&,new Short((short)1200));
xStyleProps.setPropertyValue(&RightMargin&,new Short((short)1200));
xStyleProps.setPropertyValue(&BottomMargin&,new Short((short)1200));
c++代码如下:
//Listing 34 The corresponding C++ Translation
// create new writer document and get text, then manipulate text
// Don&t forget to add : using namespace com::sun::star::
// Don&t forget to add : #include
// Don&t forget to add &com.sun.star.text.XTextDocument \& in the makefile
// Don&t forget to add : using namespace com::sun::star::
// Don&t forget to add : #include
// Don&t forget to add &com.sun.star.beans.XPropertySet \& in the makefile
// Don&t forget to add : using namespace com::sun::star::
// Don&t forget to add : #include
// Don&t forget to add &com.sun.star.style.XStyleFamiliesSupplier \& in the makefile
// Don&t forget to add : using namespace com::sun::star::
// Don&t forget to add : #include
// Don&t forget to add &com.sun.star.container.XNameContainer \& in the makefile
// Don&t forget to add : #include
// Don&t forget to add &com.sun.star.style.XStyle \& in the makefile
// the first line cannot be translated : already done in our main()
// XComponent xWriterComponent = newDocComponent(&swriter&);
Reference & XTextDocument & xTextDocument (xWriterComponent,UNO_QUERY);
// Access the text document&s multi service factory, which we will need for most of the
// following examples
Reference& XMultiServiceFactory & mxDocFactory(xTextDocument,UNO_QUERY);
Reference& XText & xText = xTextDocument-&getText();
// create a text cursor from the cells XText interface
Reference& XTextCursor & xTextCursor = xText-&createTextCursor();
// Get the property set of the cell&s TextCursor
Reference& XPropertySet & xTextCursorProps(xTextCursor,UNO_QUERY);
// Page Style name
//*** I add a intermediate variable because of Any type returned by getPropertyValue
Any pageStyleName2 = xTextCursorProps-&getPropertyValue
(OUString::createFromAscii(&PageStyleName&));
OUString pageStyleN
pageStyleName2 &&= pageStyleN
// Get the StyleFamiliesSupplier interface of the document
Reference& XStyleFamiliesSupplier & xSupplier(xTextDocument,UNO_QUERY);
// Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
// actual style families
Reference& XNameAccess & xFamilies(xSupplier-&getStyleFamilies(),UNO_QUERY);
// Access the &PageStyles& Family
Reference& XNameContainer & xFamily(xFamilies-&getByName
(OUString::createFromAscii(&PageStyles&)),UNO_QUERY);
// Insert the newly created style into the PageStyles family
Reference& XStyle & xStyle(xFamily-&getByName(pageStyleName),UNO_QUERY);
// Get the property set of the TextCursor
Reference& XPropertySet & xStyleProps(xStyle,UNO_QUERY);
Any lm, rm,
lm&&=(short)1200; rm&&=(short)1200; bm&&=(short)1200;
xStyleProps-&setPropertyValue(OUString::createFromAscii(&LeftMargin&),lm);
xStyleProps-&setPropertyValue(OUString::createFromAscii(&RightMargin&),rm);
xStyleProps-&setPropertyValue(OUString::createFromAscii(&BottomMargin&),bm);
因为对any类型的理解不深入,我不知道如何直接使用any变量,所以我用了三个临时any变量,见最后三行
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。&&&&int&i&=&-1;
class&B&extends&A
&&&&void&printSuperI()
&&&&&&&&System.out.println(super.i);
但是在C++中,貌似没有此类访问符。。。
能否在C++中访问基类中的数据呢?当然能够,最简单的方式莫过于在基类中定义Get/Set等属性函数,在继承类中使用即可。
问题是,变量太多,定义n多的Get/Set看着有些烦人。
呵呵,其实可以采用一种不是很正规的方式来直接访问基类中的数据(注意:在真实的项目中不建议使用此类方法,这个方法仅仅用于对C++二进制布局的了解+测试而已。我目前已经用vc8、gcc测试通过)
我们知道,在C++中,基类和继承类对象其实享用的同一块内存空间。即this指针所指位置。我们的所有数据都在this指针所指位置上,所有数据按照内存正向规则向上占用各自的位置。
例子代码:
&&&&int&i,j;
&&&&&&&&i&=&-1;
&&&&&&&&j&=&-2;
class&B&:&public&A
&&&&void&printSuperI()
&&&&&&&&//&此this指针及指向A::i的内存位置
&&&&&&&&int&superI&=&*(int&*)(this);&&&
&&&&&&&&//&此this+4指针及指向A::j的内存位置(sizeof(int)&==&4)
&&&&&&&&//&注意:此时必须知道C++的内存对齐方案,当然你可以通过命令要求按照自己的规则进行内存对齐。
&&&&&&&&int&superJ&=&*(int&*)((char*)this+4);&&
&&&&&&&&printf("SuperI=%d,&superJ=%d\n",superI,superJ);
问题:一旦在基类中出现了虚拟函数,此时的this指针不再指向数据的第一个变量,而是指向vptr。
在此种情况下需要给this指针加上一个vptr的指针变量位置即可。
&&&&int&i;
&&&&&&&&i&=&-1;
&&&&virtual&void&foo()
&&&&&&&&printf("Foo\n");
class&B&:&public&A
&&&&void&printSuperI()
&&&&&&&&int&superI&=&*(int&*)((char*)this+4);
&&&&&&&&printf("SuperI=%d\n",superI);
既然知道了vptr的位置,那我们可以直接手工打造函数地址来调用它(我只使用vc8调用通过,gcc调用报错。。。)
vc的c++虚拟指针手工调用
&&&&int&i;
&&&&char&*p;
&&&&&&&&p&=&"5678";
&&&&&&&&i&=&-1;
&&&&virtual&void&foo(const&char&*s)
&&&&&&&&printf("Foo,&%s&:&%s\n",s,&p);
&&&&virtual&void&foo1(const&char&*s)
&&&&&&&&printf("Foo1,&%s\n",s);
typedef&void&(A::*foo_fun)(const&char&*p);
class&B&:&public&A
&&&&void&printSuperI()
&&&&&&&&unsigned&int&*
&&&&&&&&int&superI&=&*(int&*)((char*)this+4);
&&&&&&&&printf("SuperI=%d\n",superI);
&&&&&&&&vptr&=&(unsigned&int&*)this;
&&&&&&&&foo_fun&fun_ptr&=&*(foo_fun*)(*vptr);
&&&&&&&&(this-&*fun_ptr)("1234");
&&&&&&&&foo_fun&fun_ptr1&=&*(foo_fun*)((*vptr)+4);
&&&&&&&&(this-&*fun_ptr1)("abcd");第一讲 C++程序员Java速成(一)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
喜欢此文档的还喜欢
第一讲 C++程序员Java速成(一)
j​a​v​a​速​成
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
大小:319.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
.有木有高手帮忙将一段java代码转成oc可以用的(oc,c,c++),感谢
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 java二维码生成代码 的文章

 

随机推荐