指针成员的 C++/CLI 包装器 - C++ 单例实例



我正在为以下本机 c++ 类完成我的 c++/CLI 包装器:

#ifndef __TV3DENGINE_H__
#define __TV3DENGINE_H__
#pragma once
#include "TV3DMoteur.h"
#include "Input.h"
#include "Area.h"
#include <vcclr.h>
class Engine3D
{
public:
     CLTV3DMoteur* clTV3D;
     CLInput* clInput;
     CLArea* clArea;
     CLGlobalVar * clGlobalVar;
     Engine3D();
     ~Engine3D();
     void Setup(HWND TVScreenHWND, string PathString);
     void UpdateLoop();
     void Cleanup();
     bool AppStillIdle();
     CLTV3DMoteur* GetTV3D();
     CLInput* GetInput();
     CLArea* GetArea();
     CLGlobalVar * GetGlobalVar();
};
#endif

Engine3D 的实际构造函数是:

Engine3D::Engine3D()
{
    clTV3D = CLTV3DMoteur::getInstance();
    clInput = CLInput::getInstance();
    clArea = CLArea::getInstance();
    clGlobalVar = CLGlobalVar::getInstance();
}

这是实际的包装器:

#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclrmarshal_cppstd.h>

public ref class Engine3DWrapper {
    Engine3D* m_nativeClass;
public:
    Engine3DWrapper() { m_nativeClass = new Engine3D(); }
    ~Engine3DWrapper() { delete m_nativeClass; }
    void Setup(System::IntPtr tvscreen, System::String^ AppPath) {
        System::String^ managedPath = AppPath;
        m_nativeClass->Setup((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(managedPath));
    }
    void UpdateLoop() {
        m_nativeClass->UpdateLoop();
    }
    void Cleanup() {
        m_nativeClass->Cleanup();
    }
    bool AppStillIdle() {
        return(m_nativeClass->AppStillIdle());
    }
protected:
    !Engine3DWrapper() { delete m_nativeClass; }
};
#endif
我的

问题是我如何修改我的包装器,以便我可以访问,例如,Engine3DWrapper->clGlobalVar->BLABLABLA(),其中 BLABLABLA 将是 CLGlobalVar c++ singleton 中定义的所有不同方法?

我尝试通过这种技术:

 property String ^Name
  {
    String ^get()
    {
      return gcnew String(_stu->getName());
    }
  }

但这似乎是不可能的,因为我不需要返回定义的类型。

感谢您的帮助。

问题解决了。以下是按照 Rufflewind 建议更正的包装器:

#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclrmarshal_cppstd.h>

public ref class Engine3DWrapper {
    Engine3D* m_nativeClass;
public:
    Engine3DWrapper(System::IntPtr tvscreen, System::String^ AppPath)
    { 
        m_nativeClass = new Engine3D((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(AppPath));
        m_TV3D = m_nativeClass->GetTV3D();
        m_Input = m_nativeClass->GetInput();
        m_Area = m_nativeClass->GetArea();
        m_GlobalVar = m_nativeClass->GetGlobalVar();
    }
    ~Engine3DWrapper() { 
        delete m_nativeClass; 
    }
    void UpdateLoop() {
        m_nativeClass->UpdateLoop();
    }
    void Cleanup() {
        m_nativeClass->Cleanup();
    }
    bool AppStillIdle() {
        return(m_nativeClass->AppStillIdle());
    }
    CLTV3DMoteur* m_TV3D;
    CLInput* m_Input;
    CLArea* m_Area;
    CLGlobalVar* m_GlobalVar;
protected:
    !Engine3DWrapper() { delete m_nativeClass; }
};
#endif

在本机类中使用简单的 Get 方法:

CLGlobalVar *Engine3D::GetGlobalVar()
{
    clGlobalVar = CLGlobalVar::getInstance();
    return(clGlobalVar);
}

感谢您的帮助!

最新更新