让 C++ 共享公共类的最佳方式还有鲁比?

发布于 2024-09-03 12:08:02 字数 1933 浏览 7 评论 0原文

我目前正在进行一个项目,我们的一个团队正在设计一款游戏,我们所有人都精通 Ruby,我们中的一些人(但不是全部)精通 C++。 最初我们用 ruby​​ 制作后端,但为了提高速度,我们将其移植到 c++。后端的C++端口与原始Ruby代码具有完全相同的功能和算法。 然而,我们仍然有一堆 ruby​​ 代码可以做有用的事情,但我们不想将其全部移植,因此我们希望继续使用 ruby​​ 代码并从 c++ 类获取数据。这不现实吗? 我们的第一个想法是我们可以将一些数据结构保存在 XML 或 Redis 之类的文件中并调用它,但一些开发人员不喜欢这个想法。 我们不需要在代码的不同部分之间传递任何特别复杂的数据结构,只需要元组、字符串和整数。 有什么方法可以集成 ruby​​ 代码,以便它可以本地调用 c++ 东西吗? 我们需要嵌入代码吗?我们需要做一个 ruby​​ 扩展吗?如果是这样,您可以建议什么好的资源/教程吗?

例如,假设我们在 C++ 后端中有一些这样的代码:

class The_game{
    private:
        bool printinfo; //print the player diagnostic info at the beginning if true
        int numplayers;
        std::vector<Player*> players;
        string current_action;
        int action_is_on; // the index of the player in the players array that the action is now on

        //more code here

    public:
        Table(std::vector<Player *> in_players, std::vector<Statistics *> player_stats ,const int in_numplayers);
        ~Table();
        void play_game();
        History actions_history;

};

class History{
    private:
    int action_sequence_number;
    std::vector<Action*> recent_actions;

    public:
    void print_history();
    void add_action(Action* the_action_to_be_added);
    int get_action_sequence_number(){ return action_sequence_number;}
    bool history_actions_are_equal();
    int last_action_size(int street,int number_of_actions_ago);
    History();
    ~History();
};

有没有办法通过 ruby​​ 中的 The_game 对象本地调用 actions_history 中的某些内容? (原始 ruby​​ 代码中的对象都具有相同的名称和功能)

我的意思是:

class MyRubyClass
  def method1(arg1)
    puts arg1
    self.f() # ... but still available
    puts cpp_method.the_current_game.actions_history.get_action_sequence_number()
  end
  # Constructor:
  def initialize(arg)
    puts "In constructor with arg #{arg}"
    #get the c++ object here and call it cpp_method
  end
end

这可能吗?如有任何意见或建议,我们将不胜感激。

I am currently working on a project where a team of us are designing a game, all of us are proficient in ruby and some (but not all) of us are proficient in c++.
Initially we made the backend in ruby but we ported it to c++ for more speed. The c++ port of the backend has exactly the same features and algorithms as the original ruby code.
However we still have a bunch of code in ruby that does useful things but we would rather not have to port it all, so we want to keep using the ruby code and get data from the c++ classes. Is this unrealistic?
Our first thought was that we could save some of the data structures in something like XML or redis and call that, but some of the developers don't like that idea.
We don't need any particularly complex data structures to be passed between the different parts of the code, just tuples, strings and ints.
Is there any way of integrating the ruby code so that it can call the c++ stuff natively?
Will we need to embed code? Will we have to make a ruby extension? If so are there any good resources/tutorials you could suggest?

For example say we have some code like this in the c++ backend:

class The_game{
    private:
        bool printinfo; //print the player diagnostic info at the beginning if true
        int numplayers;
        std::vector<Player*> players;
        string current_action;
        int action_is_on; // the index of the player in the players array that the action is now on

        //more code here

    public:
        Table(std::vector<Player *> in_players, std::vector<Statistics *> player_stats ,const int in_numplayers);
        ~Table();
        void play_game();
        History actions_history;

};

class History{
    private:
    int action_sequence_number;
    std::vector<Action*> recent_actions;

    public:
    void print_history();
    void add_action(Action* the_action_to_be_added);
    int get_action_sequence_number(){ return action_sequence_number;}
    bool history_actions_are_equal();
    int last_action_size(int street,int number_of_actions_ago);
    History();
    ~History();
};

Is there any way to natively call something in the actions_history via The_game object in ruby? (The objects in the original ruby code all had the same names and functionality)

By this I mean:

class MyRubyClass
  def method1(arg1)
    puts arg1
    self.f() # ... but still available
    puts cpp_method.the_current_game.actions_history.get_action_sequence_number()
  end
  # Constructor:
  def initialize(arg)
    puts "In constructor with arg #{arg}"
    #get the c++ object here and call it cpp_method
  end
end

Is this possible? Any advice or suggestions are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-09-10 12:08:02

您可以使用 Ruby C API 创建与 C++ 类接口的扩展或 SWIG创建 C++ 类的包装器

You could use the Ruby C API to create an extension that interfaces with the C++ class or SWIG to create a wrapper the C++ class.

绝情姑娘 2024-09-10 12:08:02

要创建 ruby​​ 扩展,您可能还需要查看:

For creating ruby extensions you also might want to have a look at:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文