Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ CLI writes managed Scene classes

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "C++ CLI how to write managed Scene class". In daily operation, I believe many people have doubts about how C++ CLI writes managed Scene class. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "C++ CLI how to write managed Scene class". Next, please follow the editor to study!

The basic idea is to separate the bottom 3D part from the upper GUI graphical interface part. The original way is to write a C++ class, then define some interfaces, then write a Wrapper with C++/CLI, and then call it with C #. In fact, this practice is not very good, it adds a lot of work, and there is inevitably a lot of repetitive assignment code when writing Wrapper.

The second idea is to start writing directly with C++/CLI, merging the Native part with the Managed part. Of course, C++/CLI has some restrictions, can not directly nest unmanaged classes in managed classes, can only have unmanaged class pointers, and so on. The biggest disadvantage caused by this limitation is that the smart pointer in osg::ref_ptr, that is, OpenSceneGraph, cannot be used because it is a type and cannot be directly embedded in a managed class, so syntax similar to the following is incorrect:

Ref class ManagedClass {osg::ref_ptr node;}

Of course, it is correct to write like this:

Ref class ManagedClass {osg::Node* node;}

But this loses the protection of smart pointers and is easy to cause memory leaks, so it is urgent to write a smart pointer to replace osg::ref_ptr, but basically keep the function unchanged. The reference classes of OpenSceneGraph inherit from osg::Object, while osg::Object inherits from osg::Reference. So these reference classes have ref () and unref () methods to add and decrease ReferenceCount, and when ReferenceCount=0, it automatically delete.

Referring to osg::ref_ptr and removing the less commonly used parts of this class, I wrote a smart_ptr class to complete the task of smart pointers:

/ /! OpenSceneGraph managed smart_ptr. Template public ref class smart_ptr {public: typedef T element_type; smart_ptr (): _ ptr (0) {} smart_ptr (T* ptr): _ ptr (ptr) {if (_ ptr) _ ptr- > ref ();} smart_ptr (const smart_ptr% rp): _ ptr (rp._ptr) {if (_ ptr) _ ptr- > ref () } ~ smart_ptr () {if (_ ptr) _ ptr- > unref (); _ ptr= 0;} smart_ptr% operator = (const smart_ptr% rp) {if (_ ptr==rp._ptr) return * this; T* tmp_ptr = _ ptr; _ ptr= rp._ptr; if (_ ptr) _ ptr- > ref (); / / unref second to prevent any deletion of any object which might / / be referenced by the other object. I.e rp is child of the / / original _ ptr. If (tmp_ptr) tmp_ptr- > unref (); return * this;} inline smart_ptr% operator = (T* ptr) {if (_ ptr==ptr) return * this; T* tmp_ptr = _ ptr; _ ptr= ptr; if (_ ptr) _ ptr- > ref (); / / unref second to prevent any deletion of any object which might / / be referenced by the other object. I.e rp is child of the / / original _ ptr. If (tmp_ptr) tmp_ptr- > unref (); return * this;} / / T% operator* () {return * _ ptr;} T* operator- > () {return _ ptr;} T* get () {return _ ptr;} bool operator! () {return _ ptr==0;} / / not required bool valid () {return _ ptrackers 0;} T* release () {T* tmp=_ptr If (_ ptr) _ ptr- > unref_nodelete (); _ ptr=0; return tmp;} private: T* _ ptr;}

After all this, you can finally use smart pointers among managed classes:

Public ref class Scene {protected: smart_ptr gc; smart_ptr root; smart_ptr viewer; smart_ptr camera;....

After overcoming the obstacle of smart pointer, there are still many problems to be solved. Common classes like osg::Vec3 can only be overridden to facilitate invocation. Functions like lookup node FindNode:

Ref class NodeFound {public: string ^ name; smart_ptr osgNode;}; Node Foundation ^ FindNode (string ^ name) {FindNodeVisitor findNodeVisitor; findNodeVisitor.name=MarshalString (name); root- > accept (findNodeVisitor); if (findNodeVisitor.node==NULL) throw gcnew Exceptions::NodeNotFoundExpection (); Node Foundation ^ nodeFound=gcnew NodeFound (); nodeFound- > name=name; nodeFound- > osgNode=findNodeVisitor.node; return nodeFound; return nullptr;}

Only a new structure can be defined as the return value, otherwise the C # language cannot be used because it cannot parse a smart pointer. Maybe there are other ways to use it, such as using IntPtr, but it may become dangerous without the protection of smart pointers.

At this point, the study on "C++ CLI how to write managed Scene classes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 231

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report